opstr/ops/
split_by_whitespaces.rs1use crate::config::Configuration;
2use crate::errors::LibError;
3use crate::input::Args;
4use crate::ops::traits;
5use crate::output::{Output, OutputValue};
6use crate::range;
7
8pub struct SplitByWhitespaces {}
9
10impl traits::Op for SplitByWhitespaces {
11 fn name() -> &'static str { "split-by-whitespaces" }
12 fn usage() -> &'static str { "<#1 string to-split>" }
13 fn description() -> &'static str { "split string #1 by any character of Unicode category Whitespace" }
14 fn acceptable_number_of_arguments() -> range::Range { range::Range::IndexIndex(1, 1) }
15
16 fn priority(args: &Args, _conf: &Configuration) -> Result<f32, LibError> {
17 let string: &str = args.get(0)?.try_into()?;
18 let length = string.chars().count();
19 let ws_count = string.split_whitespace().count();
20
21 Ok(ws_count as f32 / length as f32)
22 }
23
24 fn run(args: &Args, _conf: &Configuration) -> Result<Output, LibError> {
25 let string: &str = args.get(0)?.try_into()?;
26 let result = string.split_whitespace();
27 Ok(Output::HomogeneousList {
28 data: result.map(OutputValue::from_str).collect::<Vec<OutputValue>>(),
29 notes: vec![],
30 })
31 }
32}