Rename read to parse

This commit is contained in:
Jonathan Turner 2019-11-10 11:26:44 +13:00
parent 8855c54391
commit bab58576b4
3 changed files with 23 additions and 23 deletions

View File

@ -138,8 +138,8 @@ name = "nu_plugin_edit"
path = "src/plugins/edit.rs" path = "src/plugins/edit.rs"
[[bin]] [[bin]]
name = "nu_plugin_read" name = "nu_plugin_parse"
path = "src/plugins/read.rs" path = "src/plugins/parse.rs"
[[bin]] [[bin]]
name = "nu_plugin_str" name = "nu_plugin_str"

View File

@ -10,19 +10,19 @@ use nom::{
use regex::Regex; use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
enum ReadCommand { enum ParseCommand {
Text(String), Text(String),
Column(String), Column(String),
} }
fn read(input: &str) -> IResult<&str, Vec<ReadCommand>> { fn parse(input: &str) -> IResult<&str, Vec<ParseCommand>> {
let mut output = vec![]; let mut output = vec![];
let mut loop_input = input; let mut loop_input = input;
loop { loop {
let (input, before) = take_while(|c| c != '{')(loop_input)?; let (input, before) = take_while(|c| c != '{')(loop_input)?;
if before.len() > 0 { if before.len() > 0 {
output.push(ReadCommand::Text(before.to_string())); output.push(ParseCommand::Text(before.to_string()));
} }
if input != "" { if input != "" {
// Look for column as we're now at one // Look for column as we're now at one
@ -30,7 +30,7 @@ fn read(input: &str) -> IResult<&str, Vec<ReadCommand>> {
let (input, column) = take_while(|c| c != '}')(input)?; let (input, column) = take_while(|c| c != '}')(input)?;
let (input, _) = tag("}")(input)?; let (input, _) = tag("}")(input)?;
output.push(ReadCommand::Column(column.to_string())); output.push(ParseCommand::Column(column.to_string()));
loop_input = input; loop_input = input;
} else { } else {
loop_input = input; loop_input = input;
@ -43,12 +43,12 @@ fn read(input: &str) -> IResult<&str, Vec<ReadCommand>> {
Ok((loop_input, output)) Ok((loop_input, output))
} }
fn column_names(commands: &[ReadCommand]) -> Vec<String> { fn column_names(commands: &[ParseCommand]) -> Vec<String> {
let mut output = vec![]; let mut output = vec![];
for command in commands { for command in commands {
match command { match command {
ReadCommand::Column(c) => { ParseCommand::Column(c) => {
output.push(c.clone()); output.push(c.clone());
} }
_ => {} _ => {}
@ -58,15 +58,15 @@ fn column_names(commands: &[ReadCommand]) -> Vec<String> {
output output
} }
fn build_regex(commands: &[ReadCommand]) -> String { fn build_regex(commands: &[ParseCommand]) -> String {
let mut output = String::new(); let mut output = String::new();
for command in commands { for command in commands {
match command { match command {
ReadCommand::Text(s) => { ParseCommand::Text(s) => {
output.push_str(&s.replace("(", "\\(")); output.push_str(&s.replace("(", "\\("));
} }
ReadCommand::Column(_) => { ParseCommand::Column(_) => {
output.push_str("(.*)"); output.push_str("(.*)");
} }
} }
@ -74,23 +74,23 @@ fn build_regex(commands: &[ReadCommand]) -> String {
return output; return output;
} }
struct Read { struct Parse {
regex: Regex, regex: Regex,
column_names: Vec<String>, column_names: Vec<String>,
} }
impl Read { impl Parse {
fn new() -> Self { fn new() -> Self {
Read { Parse {
regex: Regex::new("").unwrap(), regex: Regex::new("").unwrap(),
column_names: vec![], column_names: vec![],
} }
} }
} }
impl Plugin for Read { impl Plugin for Parse {
fn config(&mut self) -> Result<Signature, ShellError> { fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("read") Ok(Signature::build("parse")
.desc("Parse columns from string data using a simple pattern") .desc("Parse columns from string data using a simple pattern")
.required( .required(
"pattern", "pattern",
@ -107,17 +107,17 @@ impl Plugin for Read {
.. ..
} => { } => {
//self.pattern = s.clone(); //self.pattern = s.clone();
let read_pattern = read(&pattern).unwrap(); let parse_pattern = parse(&pattern).unwrap();
let read_regex = build_regex(&read_pattern.1); 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, .. } => { Tagged { tag, .. } => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Unrecognized type in params", "Unrecognized type in params",
"value", "expected a string",
tag, tag,
)); ));
} }
@ -152,5 +152,5 @@ impl Plugin for Read {
} }
fn main() { fn main() {
serve_plugin(&mut Read::new()); serve_plugin(&mut Parse::new());
} }

View File

@ -62,7 +62,7 @@ fn read_plugin() {
cwd: "tests/fixtures/formats", h::pipeline( cwd: "tests/fixtures/formats", h::pipeline(
r#" r#"
open fileA.txt open fileA.txt
| read "{Name}={Value}" | parse "{Name}={Value}"
| nth 1 | nth 1
| get Value | get Value
| echo $it | echo $it