2022-10-29 23:29:46 +02:00
|
|
|
use crate::input_handler::{operate, CmdArgument};
|
2022-08-23 15:53:14 +02:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::{Call, CellPath},
|
|
|
|
engine::{Command, EngineState, Stack},
|
2022-10-29 23:29:46 +02:00
|
|
|
levenshtein_distance, Category, Example, PipelineData, ShellError, Signature, Span,
|
2022-08-23 15:53:14 +02:00
|
|
|
SyntaxShape, Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
2022-10-29 23:29:46 +02:00
|
|
|
struct Arguments {
|
|
|
|
compare_string: String,
|
|
|
|
cell_paths: Option<Vec<CellPath>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CmdArgument for Arguments {
|
|
|
|
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
|
|
|
self.cell_paths.take()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 15:53:14 +02:00
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"str distance"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("str distance")
|
|
|
|
.required(
|
|
|
|
"compare-string",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the first string to compare",
|
|
|
|
)
|
|
|
|
.rest(
|
|
|
|
"rest",
|
|
|
|
SyntaxShape::CellPath,
|
|
|
|
"optionally check if string contains pattern by column paths",
|
|
|
|
)
|
|
|
|
.category(Category::Strings)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-09-07 10:16:55 +02:00
|
|
|
"compare two strings and return the edit distance/levenshtein distance"
|
2022-08-23 15:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-08-24 11:49:03 +02:00
|
|
|
vec!["edit", "match", "score", "levenshtein"]
|
2022-08-23 15:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-10-29 23:29:46 +02:00
|
|
|
let compare_string: String = call.req(engine_state, stack, 0)?;
|
|
|
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
|
|
|
|
let cell_paths = (!cell_paths.is_empty()).then(|| cell_paths);
|
|
|
|
let args = Arguments {
|
|
|
|
compare_string,
|
|
|
|
cell_paths,
|
|
|
|
};
|
|
|
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
2022-08-23 15:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "get the edit distance between two strings",
|
|
|
|
example: "'nushell' | str distance 'nutshell'",
|
|
|
|
result: Some(Value::Record {
|
|
|
|
cols: vec!["distance".to_string()],
|
|
|
|
vals: vec![Value::Int {
|
|
|
|
val: 1,
|
|
|
|
span: Span::test_data(),
|
|
|
|
}],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 23:29:46 +02:00
|
|
|
fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|
|
|
let compare_string = &args.compare_string;
|
2022-08-23 15:53:14 +02:00
|
|
|
match &input {
|
|
|
|
Value::String { val, .. } => {
|
|
|
|
let distance = levenshtein_distance(val, compare_string);
|
|
|
|
Value::Record {
|
|
|
|
cols: vec!["distance".to_string()],
|
|
|
|
vals: vec![Value::Int {
|
|
|
|
val: distance as i64,
|
|
|
|
span: head,
|
|
|
|
}],
|
|
|
|
span: head,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => Value::Error {
|
|
|
|
error: ShellError::UnsupportedInput(
|
|
|
|
format!(
|
|
|
|
"Input's type is {}. This command only works with strings.",
|
|
|
|
other.get_type()
|
|
|
|
),
|
|
|
|
head,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|