Merge pull request #944 from jonathandturner/read_to_parse

Read to parse
This commit is contained in:
Jonathan Turner 2019-11-09 15:07:40 -08:00 committed by GitHub
commit d32c9ce1b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 24 deletions

View File

@ -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"

View File

@ -295,7 +295,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| from-xml | Parse text as .xml and create a table |
| from-yaml | Parse text as a .yaml/.yml and create a table |
| lines | Split single string into rows, one per line |
| read pattern | Convert text to a table by matching the given pattern |
| parse pattern | Convert text to a table by matching the given pattern |
| size | Gather word count statistics on the text |
| split-column sep ...column-names | Split row contents across multiple columns via the separator, optionally give the columns names |
| split-row sep | Split row contents over multiple rows via the separator |

View File

@ -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<ReadCommand>> {
fn parse(input: &str) -> IResult<&str, Vec<ParseCommand>> {
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<ReadCommand>> {
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<ReadCommand>> {
Ok((loop_input, output))
}
fn column_names(commands: &[ReadCommand]) -> Vec<String> {
fn column_names(commands: &[ParseCommand]) -> Vec<String> {
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<String> {
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<String>,
}
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<Signature, ShellError> {
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());
}

View File

@ -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