Skip to main content

basic_parser/
basic_parser.rs

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