AtParser

Struct AtParser 

Source
pub struct AtParser<'a, T>
where T: AtContext,
{ pub commands: &'a mut [(&'static str, &'a mut T)], }
Expand description

The main AT command parser Generic over T which must implement AtContext trait

Fields§

§commands: &'a mut [(&'static str, &'a mut T)]

Array of registered commands with their name and handler

Implementations§

Source§

impl<'a, T> AtParser<'a, T>
where T: AtContext,

Source

pub fn new() -> Self

Create a new empty parser

Examples found in repository?
examples/basic_parser.rs (line 49)
40fn main() {
41    println!("=== AtParser Example ===\n");
42
43    // Create command instances
44    let mut cmd1 = TestCommand { value: 0 };
45    let mut cmd2 = TestCommand { value: 5 };
46    let mut cmd3 = TestCommand { value: 10 };
47
48    // Create parser instance
49    let mut parser = AtParser::new();
50    
51    // Register commands
52    let commands: &mut [(&str, &mut TestCommand)] = &mut [
53        ("AT+CMD1", &mut cmd1),
54        ("AT+CMD2", &mut cmd2),
55        ("AT+CMD3", &mut cmd3),
56    ];
57
58    parser.set_commands(commands);
59
60    println!("Registered commands: AT+CMD1, AT+CMD2, AT+CMD3\n");
61
62    // Test cases
63    let test_cases = vec![
64        ("AT+CMD1", "Execute CMD1"),
65        ("AT+CMD1?", "Query CMD1 value"),
66        ("AT+CMD1=?", "Test CMD1"),
67        ("AT+CMD1=42", "Set CMD1 to 42"),
68        ("AT+CMD1?", "Query CMD1 after set"),
69        ("AT+CMD2", "Execute CMD2"),
70        ("AT+CMD2?", "Query CMD2 value"),
71        ("AT+CMD3=100", "Set CMD3 to 100"),
72        ("AT+CMD3?", "Query CMD3"),
73        ("AT+UNKNOWN", "Unknown command error"),
74        ("AT+CMD1=abc", "Invalid argument error"),
75    ];
76
77    for (cmd, description) in test_cases {
78        println!("Test: {}", description);
79        println!("  Command: {}", cmd);
80        match parser.execute(cmd) {
81            Ok(response) => println!("  Response: {}", response),
82            Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
83            Err(AtError::NotSupported) => println!("  Error: Not supported"),
84            Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
85        }
86        println!();
87    }
88
89    println!("=== Example completed ===");
90}
Source

pub fn set_commands(&mut self, commands: &'a mut [(&'static str, &'a mut T)])

Register commands that this parser will handle

Examples found in repository?
examples/basic_parser.rs (line 58)
40fn main() {
41    println!("=== AtParser Example ===\n");
42
43    // Create command instances
44    let mut cmd1 = TestCommand { value: 0 };
45    let mut cmd2 = TestCommand { value: 5 };
46    let mut cmd3 = TestCommand { value: 10 };
47
48    // Create parser instance
49    let mut parser = AtParser::new();
50    
51    // Register commands
52    let commands: &mut [(&str, &mut TestCommand)] = &mut [
53        ("AT+CMD1", &mut cmd1),
54        ("AT+CMD2", &mut cmd2),
55        ("AT+CMD3", &mut cmd3),
56    ];
57
58    parser.set_commands(commands);
59
60    println!("Registered commands: AT+CMD1, AT+CMD2, AT+CMD3\n");
61
62    // Test cases
63    let test_cases = vec![
64        ("AT+CMD1", "Execute CMD1"),
65        ("AT+CMD1?", "Query CMD1 value"),
66        ("AT+CMD1=?", "Test CMD1"),
67        ("AT+CMD1=42", "Set CMD1 to 42"),
68        ("AT+CMD1?", "Query CMD1 after set"),
69        ("AT+CMD2", "Execute CMD2"),
70        ("AT+CMD2?", "Query CMD2 value"),
71        ("AT+CMD3=100", "Set CMD3 to 100"),
72        ("AT+CMD3?", "Query CMD3"),
73        ("AT+UNKNOWN", "Unknown command error"),
74        ("AT+CMD1=abc", "Invalid argument error"),
75    ];
76
77    for (cmd, description) in test_cases {
78        println!("Test: {}", description);
79        println!("  Command: {}", cmd);
80        match parser.execute(cmd) {
81            Ok(response) => println!("  Response: {}", response),
82            Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
83            Err(AtError::NotSupported) => println!("  Error: Not supported"),
84            Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
85        }
86        println!();
87    }
88
89    println!("=== Example completed ===");
90}
Source

pub fn execute(&mut self, input: &str) -> AtResult<'static>

Parse and execute an AT command string

§Arguments
  • input - The raw AT command string (e.g., “AT+CMD?”)
§Returns
  • Ok(&str) - Success response from the command handler
  • Err(AtError) - Error if parsing fails or command is not found
Examples found in repository?
examples/basic_parser.rs (line 80)
40fn main() {
41    println!("=== AtParser Example ===\n");
42
43    // Create command instances
44    let mut cmd1 = TestCommand { value: 0 };
45    let mut cmd2 = TestCommand { value: 5 };
46    let mut cmd3 = TestCommand { value: 10 };
47
48    // Create parser instance
49    let mut parser = AtParser::new();
50    
51    // Register commands
52    let commands: &mut [(&str, &mut TestCommand)] = &mut [
53        ("AT+CMD1", &mut cmd1),
54        ("AT+CMD2", &mut cmd2),
55        ("AT+CMD3", &mut cmd3),
56    ];
57
58    parser.set_commands(commands);
59
60    println!("Registered commands: AT+CMD1, AT+CMD2, AT+CMD3\n");
61
62    // Test cases
63    let test_cases = vec![
64        ("AT+CMD1", "Execute CMD1"),
65        ("AT+CMD1?", "Query CMD1 value"),
66        ("AT+CMD1=?", "Test CMD1"),
67        ("AT+CMD1=42", "Set CMD1 to 42"),
68        ("AT+CMD1?", "Query CMD1 after set"),
69        ("AT+CMD2", "Execute CMD2"),
70        ("AT+CMD2?", "Query CMD2 value"),
71        ("AT+CMD3=100", "Set CMD3 to 100"),
72        ("AT+CMD3?", "Query CMD3"),
73        ("AT+UNKNOWN", "Unknown command error"),
74        ("AT+CMD1=abc", "Invalid argument error"),
75    ];
76
77    for (cmd, description) in test_cases {
78        println!("Test: {}", description);
79        println!("  Command: {}", cmd);
80        match parser.execute(cmd) {
81            Ok(response) => println!("  Response: {}", response),
82            Err(AtError::UnknownCommand) => println!("  Error: Unknown command"),
83            Err(AtError::NotSupported) => println!("  Error: Not supported"),
84            Err(AtError::InvalidArgs) => println!("  Error: Invalid arguments"),
85        }
86        println!();
87    }
88
89    println!("=== Example completed ===");
90}

Auto Trait Implementations§

§

impl<'a, T> Freeze for AtParser<'a, T>

§

impl<'a, T> RefUnwindSafe for AtParser<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for AtParser<'a, T>
where T: Send,

§

impl<'a, T> Sync for AtParser<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for AtParser<'a, T>

§

impl<'a, T> !UnwindSafe for AtParser<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.