You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
use crate::error::ShellError;
|
|
|
|
pub fn parse_line(line: &str) -> Result<(&str, Vec<String>), ShellError> {
|
|
let (keyword, unparsed_args) = line.split_once(' ').unwrap_or((line, ""));
|
|
|
|
if let Some(args) = shlex::split(unparsed_args) {
|
|
Ok((keyword, args))
|
|
} else {
|
|
Err(ShellError::MalformedArgs(unparsed_args.to_string()))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_parse_single() {
|
|
assert_eq!(parse_line("keyword"), Ok(("keyword", vec![])))
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_multiple() {
|
|
assert_eq!(
|
|
parse_line("keyword arg1 arg2"),
|
|
Ok(("keyword", vec!["arg1".to_string(), "arg2".to_string()]))
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_complex() {
|
|
assert_eq!(
|
|
parse_line("keyword 'this is arg 1' arg2 \"arg3 \""),
|
|
Ok((
|
|
"keyword",
|
|
vec![
|
|
"this is arg 1".to_string(),
|
|
"arg2".to_string(),
|
|
"arg3 ".to_string()
|
|
]
|
|
))
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_malformed() {
|
|
assert_eq!(
|
|
parse_line("keyword \"malformed"),
|
|
Err(ShellError::MalformedArgs("keyword \"malformed".to_string()))
|
|
)
|
|
}
|
|
}
|