Skip to main content

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 69)
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}
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 78)
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}
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 100)
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}

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> UnsafeUnpin 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.