|
@ -1,37 +1,11 @@ |
|
|
pub fn parse_line(line: &str) -> (&str, Vec<&str>) {
|
|
|
|
|
|
let tokens = tokenize(line);
|
|
|
|
|
|
|
|
|
use crate::error::ShellError;
|
|
|
|
|
|
|
|
|
let keyword = tokens[0];
|
|
|
|
|
|
let mut args: Vec<&str> = Vec::new();
|
|
|
|
|
|
|
|
|
pub fn parse_line(line: &str) -> Result<(&str, Vec<String>), ShellError> {
|
|
|
|
|
|
let (keyword, unparsed_args) = line.split_once(' ').unwrap_or((line, ""));
|
|
|
|
|
|
|
|
|
if tokens.len() > 1 {
|
|
|
|
|
|
args = tokens[1..].to_vec();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
(keyword, args)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn tokenize(line: &str) -> Vec<&str> {
|
|
|
|
|
|
line.trim().split(' ').collect::<Vec<&str>>()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_tokenize() {
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
tokenize("keyword arg1 arg2 arg3"),
|
|
|
|
|
|
vec!["keyword", "arg1", "arg2", "arg3"]
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_parse_line() {
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
parse_line("keyword arg1 arg2 arg3"),
|
|
|
|
|
|
("keyword", vec!["arg1", "arg2", "arg3"])
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
if let Some(args) = shlex::split(unparsed_args) {
|
|
|
|
|
|
Ok((keyword, args))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err(ShellError::MalformedArgs(unparsed_args.to_string()))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|