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 68)
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}
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 77)
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}
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 99)
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}

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.