basic_parser/
basic_parser.rs

1/***************************************************************************
2 *
3 * AT Command Parser
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19 
20//! Example using the AtParser with proper type handling
21
22use at_parser_rs::context::AtContext;
23use at_parser_rs::parser::AtParser;
24use at_parser_rs::{Args, AtError, AtResult};
25
26/// Simple command module for testing
27pub struct TestCommand {
28    pub value: u32,
29}
30
31impl AtContext for TestCommand {
32    fn exec(&self) -> AtResult<'static> {
33        Ok("Test command executed")
34    }
35
36    fn query(&mut self) -> AtResult<'static> {
37        // In a real scenario, you would format the value dynamically
38        // For this example, we use static strings
39        if self.value == 0 {
40            Ok("0")
41        } else if self.value < 10 {
42            Ok("1-9")
43        } else {
44            Ok("10+")
45        }
46    }
47
48    fn test(&mut self) -> AtResult<'static> {
49        Ok("Test: 0-100")
50    }
51
52    fn set(&mut self, args: Args) -> AtResult<'static> {
53        let val_str = args.get(0).ok_or(AtError::InvalidArgs)?;
54        self.value = val_str.parse().map_err(|_| AtError::InvalidArgs)?;
55        Ok("OK")
56    }
57}
58
59fn main() {
60    println!("=== AtParser Example ===\n");
61
62    // Create command instances
63    let mut cmd1 = TestCommand { value: 0 };
64    let mut cmd2 = TestCommand { value: 5 };
65    let mut cmd3 = TestCommand { value: 10 };
66
67    // Create parser instance
68    let mut parser = AtParser::new();
69    
70    // Register commands
71    let commands: &mut [(&str, &mut TestCommand)] = &mut [
72        ("AT+CMD1", &mut cmd1),
73        ("AT+CMD2", &mut cmd2),
74        ("AT+CMD3", &mut cmd3),
75    ];
76
77    parser.set_commands(commands);
78
79    println!("Registered commands: AT+CMD1, AT+CMD2, AT+CMD3\n");
80
81    // Test cases
82    let test_cases = vec![
83        ("AT+CMD1", "Execute CMD1"),
84        ("AT+CMD1?", "Query CMD1 value"),
85        ("AT+CMD1=?", "Test CMD1"),
86        ("AT+CMD1=42", "Set CMD1 to 42"),
87        ("AT+CMD1?", "Query CMD1 after set"),
88        ("AT+CMD2", "Execute CMD2"),
89        ("AT+CMD2?", "Query CMD2 value"),
90        ("AT+CMD3=100", "Set CMD3 to 100"),
91        ("AT+CMD3?", "Query CMD3"),
92        ("AT+UNKNOWN", "Unknown command error"),
93        ("AT+CMD1=abc", "Invalid argument error"),
94    ];
95
96    for (cmd, description) in test_cases {
97        println!("Test: {}", description);
98        println!("  Command: {}", cmd);
99        match parser.execute(cmd) {
100            Ok(response) => println!("  Response: {}", response),
101            Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
102            Err(AtError::NotSupported) => println!("  Error: Not supported"),
103            Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
104        }
105        println!();
106    }
107
108    println!("=== Example completed ===");
109}