From bab58576b4d7320c6b59f9234d4fe5764ad404de Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 10 Nov 2019 11:26:44 +1300 Subject: [PATCH] Rename read to parse --- Cargo.toml | 4 ++-- src/plugins/{read.rs => parse.rs} | 40 +++++++++++++++---------------- tests/tests.rs | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) rename src/plugins/{read.rs => parse.rs} (78%) diff --git a/Cargo.toml b/Cargo.toml index e5a453be7b..593d9d0467 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,8 +138,8 @@ name = "nu_plugin_edit" path = "src/plugins/edit.rs" [[bin]] -name = "nu_plugin_read" -path = "src/plugins/read.rs" +name = "nu_plugin_parse" +path = "src/plugins/parse.rs" [[bin]] name = "nu_plugin_str" diff --git a/src/plugins/read.rs b/src/plugins/parse.rs similarity index 78% rename from src/plugins/read.rs rename to src/plugins/parse.rs index de88946e91..97ece0693d 100644 --- a/src/plugins/read.rs +++ b/src/plugins/parse.rs @@ -10,19 +10,19 @@ use nom::{ use regex::Regex; #[derive(Debug)] -enum ReadCommand { +enum ParseCommand { Text(String), Column(String), } -fn read(input: &str) -> IResult<&str, Vec> { +fn parse(input: &str) -> IResult<&str, Vec> { let mut output = vec![]; let mut loop_input = input; loop { let (input, before) = take_while(|c| c != '{')(loop_input)?; if before.len() > 0 { - output.push(ReadCommand::Text(before.to_string())); + output.push(ParseCommand::Text(before.to_string())); } if input != "" { // Look for column as we're now at one @@ -30,7 +30,7 @@ fn read(input: &str) -> IResult<&str, Vec> { let (input, column) = take_while(|c| c != '}')(input)?; let (input, _) = tag("}")(input)?; - output.push(ReadCommand::Column(column.to_string())); + output.push(ParseCommand::Column(column.to_string())); loop_input = input; } else { loop_input = input; @@ -43,12 +43,12 @@ fn read(input: &str) -> IResult<&str, Vec> { Ok((loop_input, output)) } -fn column_names(commands: &[ReadCommand]) -> Vec { +fn column_names(commands: &[ParseCommand]) -> Vec { let mut output = vec![]; for command in commands { match command { - ReadCommand::Column(c) => { + ParseCommand::Column(c) => { output.push(c.clone()); } _ => {} @@ -58,15 +58,15 @@ fn column_names(commands: &[ReadCommand]) -> Vec { output } -fn build_regex(commands: &[ReadCommand]) -> String { +fn build_regex(commands: &[ParseCommand]) -> String { let mut output = String::new(); for command in commands { match command { - ReadCommand::Text(s) => { + ParseCommand::Text(s) => { output.push_str(&s.replace("(", "\\(")); } - ReadCommand::Column(_) => { + ParseCommand::Column(_) => { output.push_str("(.*)"); } } @@ -74,23 +74,23 @@ fn build_regex(commands: &[ReadCommand]) -> String { return output; } -struct Read { +struct Parse { regex: Regex, column_names: Vec, } -impl Read { +impl Parse { fn new() -> Self { - Read { + Parse { regex: Regex::new("").unwrap(), column_names: vec![], } } } -impl Plugin for Read { +impl Plugin for Parse { fn config(&mut self) -> Result { - Ok(Signature::build("read") + Ok(Signature::build("parse") .desc("Parse columns from string data using a simple pattern") .required( "pattern", @@ -107,17 +107,17 @@ impl Plugin for Read { .. } => { //self.pattern = s.clone(); - let read_pattern = read(&pattern).unwrap(); - let read_regex = build_regex(&read_pattern.1); + let parse_pattern = parse(&pattern).unwrap(); + let parse_regex = build_regex(&parse_pattern.1); - self.column_names = column_names(&read_pattern.1); + self.column_names = column_names(&parse_pattern.1); - self.regex = Regex::new(&read_regex).unwrap(); + self.regex = Regex::new(&parse_regex).unwrap(); } Tagged { tag, .. } => { return Err(ShellError::labeled_error( "Unrecognized type in params", - "value", + "expected a string", tag, )); } @@ -152,5 +152,5 @@ impl Plugin for Read { } fn main() { - serve_plugin(&mut Read::new()); + serve_plugin(&mut Parse::new()); } diff --git a/tests/tests.rs b/tests/tests.rs index caaeb2ac86..490dabefff 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -62,7 +62,7 @@ fn read_plugin() { cwd: "tests/fixtures/formats", h::pipeline( r#" open fileA.txt - | read "{Name}={Value}" + | parse "{Name}={Value}" | nth 1 | get Value | echo $it