Skip to main content

lattice_agent/
commands.rs

1//! Ex-command parsing helpers shared by every agent adapter.
2
3use lattice_grammar::args::Args;
4use lattice_grammar::error::{CommandError, GrammarResult};
5
6/// Reject any trailing characters; these commands take no arguments.
7pub fn parse_no_args(rest: &str, _bang: bool) -> GrammarResult<Args> {
8    if rest.trim().is_empty() {
9        Ok(Args::None)
10    } else {
11        Err(CommandError::BadArgs(
12            "trailing characters after command".into(),
13        ))
14    }
15}
16
17/// Take the rest of the line verbatim (trimmed) as a single string arg.
18pub fn parse_rest_as_text(rest: &str, _bang: bool) -> GrammarResult<Args> {
19    Ok(Args::String(rest.trim().to_string()))
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn no_args_accepts_empty_and_whitespace() {
28        assert_eq!(parse_no_args("", false).expect("empty is ok"), Args::None);
29        assert_eq!(
30            parse_no_args("   ", false).expect("whitespace is ok"),
31            Args::None
32        );
33    }
34
35    #[test]
36    fn no_args_rejects_trailing_characters() {
37        assert!(parse_no_args("junk", false).is_err());
38    }
39
40    #[test]
41    fn rest_as_text_trims_and_keeps_inner_spaces() {
42        assert_eq!(
43            parse_rest_as_text("  hello  world  ", false).expect("always ok"),
44            Args::String("hello  world".to_string())
45        );
46        assert_eq!(
47            parse_rest_as_text("", false).expect("always ok"),
48            Args::String(String::new())
49        );
50    }
51}