Updated Doc, error on bad input

This commit is contained in:
Flare576 2019-11-01 17:25:08 -05:00
parent cd058db046
commit 763bbe1c01
2 changed files with 30 additions and 38 deletions

View File

@ -26,13 +26,6 @@ Consumes either a single value or a table and converts the provided data to a st
0 │ X │ filesystem │ /home/tux/stuff/expr/stuff 0 │ X │ filesystem │ /home/tux/stuff/expr/stuff
1 │ │ filesystem │ / 1 │ │ filesystem │ /
━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> shells | str # --to-int
━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# │ │ name │ path
───┼───┼────────────┼────────────────────────────────
0 │ X │ filesystem │ /home/TUX/stuff/expr/stuff
1 │ │ filesystem │ /
━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> shells | str # --substring "21, 99" > shells | str # --substring "21, 99"
━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━┯━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# │ │ name │ path # │ │ name │ path
@ -47,4 +40,11 @@ Consumes either a single value or a table and converts the provided data to a st
0 │ X │ filesystem │ TUX/stuff/expr/stuff 0 │ X │ filesystem │ TUX/stuff/expr/stuff
1 │ │ filesystem │ 1 │ │ filesystem │
━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> echo "1, 2, 3" | split-row "," | str --to-int | sum
━━━━━━━━━
<value>
─────────
6
━━━━━━━━━
``` ```

View File

@ -9,7 +9,7 @@ enum Action {
Downcase, Downcase,
Upcase, Upcase,
ToInteger, ToInteger,
Substring(String), Substring(usize, usize),
} }
pub type ColumnPath = Vec<Tagged<String>>; pub type ColumnPath = Vec<Tagged<String>>;
@ -35,23 +35,14 @@ impl Str {
let applied = match self.action.as_ref() { let applied = match self.action.as_ref() {
Some(Action::Downcase) => Value::string(input.to_ascii_lowercase()), Some(Action::Downcase) => Value::string(input.to_ascii_lowercase()),
Some(Action::Upcase) => Value::string(input.to_ascii_uppercase()), Some(Action::Upcase) => Value::string(input.to_ascii_uppercase()),
Some(Action::Substring(s)) => { Some(Action::Substring(s, e)) => {
// Index operator isn't perfect: https://users.rust-lang.org/t/how-to-get-a-substring-of-a-string/1351 let end: usize = cmp::min(*e, input.len());
let no_spaces: String = s.chars().filter(|c| !c.is_whitespace()).collect(); let start: usize = *s;
let v: Vec<&str> = no_spaces.split(',').collect();
let start: usize = match v[0] {
"" => 0,
_ => v[0].parse().unwrap(),
};
let end: usize = match v[1] {
"" => input.len(),
_ => cmp::min(v[1].parse().unwrap(), input.len()),
};
if start > input.len() - 1 { if start > input.len() - 1 {
Value::string("") Value::string("")
} else if start > end {
Value::string(input)
} else { } else {
// Index operator isn't perfect:
// https://users.rust-lang.org/t/how-to-get-a-substring-of-a-string/1351
Value::string(&input[start..end]) Value::string(&input[start..end])
} }
} }
@ -103,9 +94,20 @@ impl Str {
} }
} }
fn for_substring(&mut self, start_end: String) { fn for_substring(&mut self, s: String) {
if self.permit() { let v: Vec<&str> = s.split(',').collect();
self.action = Some(Action::Substring(start_end)); let start: usize = match v[0] {
"" => 0,
_ => v[0].trim().parse().unwrap(),
};
let end: usize = match v[1] {
"" => usize::max_value().clone(),
_ => v[1].trim().parse().unwrap(),
};
if start > end {
self.log_error("End must be greater than or equal to Start");
} else if self.permit() {
self.action = Some(Action::Substring(start, end));
} else { } else {
self.log_error("can only apply one"); self.log_error("can only apply one");
} }
@ -688,7 +690,7 @@ mod tests {
} }
#[test] #[test]
fn str_plugin_applies_substring_returns_string_if_start_exceeds_end() { fn str_plugin_applies_substring_returns_error_if_start_exceeds_end() {
let mut plugin = Str::new(); let mut plugin = Str::new();
assert!(plugin assert!(plugin
@ -697,17 +699,7 @@ mod tests {
.with_named_parameter("substring", "3,1") .with_named_parameter("substring", "3,1")
.create() .create()
) )
.is_ok()); .is_err());
assert_eq!(plugin.error, Some("End must be greater than or equal to Start".to_string()));
let subject = unstructured_sample_record("0123456789");
let output = plugin.filter(subject).unwrap();
match output[0].as_ref().unwrap() {
ReturnSuccess::Value(Tagged {
item: Value::Primitive(Primitive::String(s)),
..
}) => assert_eq!(*s, String::from("0123456789")),
_ => {}
}
} }
} }