pub struct AtParser<'a, T, const SIZE: usize>{
pub commands: &'a mut [(&'static str, &'a mut T)],
}Expand description
The main AT command parser
Generic over T which must implement the AtContext<SIZE> trait,
and over the const SIZE which determines the response buffer size.
§Generic Design
The parser is generic over the command handler type T and response size SIZE to allow
compile-time type checking when all handlers are of the same type. This provides:
- Type safety: Compile-time verification of handler types
- Zero overhead: No dynamic dispatch when using concrete types
- Flexibility: Can be used with trait objects (
dyn AtContext<SIZE>) for mixed handler types
§Usage Patterns
§With trait objects (recommended for mixed types):
const SIZE: usize = 64;
let mut parser: AtParser<dyn AtContext<SIZE>, SIZE> = AtParser::new();
let commands: &mut [(&str, &mut dyn AtContext<SIZE>)] = &mut [
("AT+ECHO", &mut echo_handler),
("AT+RST", &mut reset_handler),
];
parser.set_commands(commands);§With concrete types (for homogeneous handlers):
const SIZE: usize = 64;
let mut parser: AtParser<MyHandler, SIZE> = AtParser::new();
let commands: &mut [(&str, &mut MyHandler)] = &mut [
("AT+CMD1", &mut handler1),
("AT+CMD2", &mut handler2),
];
parser.set_commands(commands);Fields§
§commands: &'a mut [(&'static str, &'a mut T)]Array of registered commands with their name and handler
Implementations§
Source§impl<'a, T, const SIZE: usize> AtParser<'a, T, SIZE>
impl<'a, T, const SIZE: usize> AtParser<'a, T, SIZE>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty parser
Examples found in repository?
examples/basic_parser.rs (line 75)
70pub extern "C" fn main() -> ! {
71 let mut cmd1 = TestCommand { value: 0 };
72 let mut cmd2 = TestCommand { value: 5 };
73 let mut cmd3 = TestCommand { value: 10 };
74
75 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
76
77 let commands: &mut [(&str, &mut TestCommand)] = &mut [
78 ("AT+CMD1", &mut cmd1),
79 ("AT+CMD2", &mut cmd2),
80 ("AT+CMD3", &mut cmd3),
81 ];
82 parser.set_commands(commands);
83
84 // Execute (no-op result, just exercising the API)
85 let _ = parser.execute("AT+CMD1");
86 let _ = parser.execute("AT+CMD1?");
87 let _ = parser.execute("AT+CMD1=?");
88 let _ = parser.execute("AT+CMD1=42");
89 let _ = parser.execute("AT+CMD1?");
90 let _ = parser.execute("AT+CMD2");
91 let _ = parser.execute("AT+CMD2?");
92 let _ = parser.execute("AT+CMD3=100");
93 let _ = parser.execute("AT+CMD3?");
94 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
95 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
96
97 loop {}
98}Sourcepub fn set_commands(&mut self, commands: &'a mut [(&'static str, &'a mut T)])
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 82)
70pub extern "C" fn main() -> ! {
71 let mut cmd1 = TestCommand { value: 0 };
72 let mut cmd2 = TestCommand { value: 5 };
73 let mut cmd3 = TestCommand { value: 10 };
74
75 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
76
77 let commands: &mut [(&str, &mut TestCommand)] = &mut [
78 ("AT+CMD1", &mut cmd1),
79 ("AT+CMD2", &mut cmd2),
80 ("AT+CMD3", &mut cmd3),
81 ];
82 parser.set_commands(commands);
83
84 // Execute (no-op result, just exercising the API)
85 let _ = parser.execute("AT+CMD1");
86 let _ = parser.execute("AT+CMD1?");
87 let _ = parser.execute("AT+CMD1=?");
88 let _ = parser.execute("AT+CMD1=42");
89 let _ = parser.execute("AT+CMD1?");
90 let _ = parser.execute("AT+CMD2");
91 let _ = parser.execute("AT+CMD2?");
92 let _ = parser.execute("AT+CMD3=100");
93 let _ = parser.execute("AT+CMD3?");
94 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
95 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
96
97 loop {}
98}Sourcepub fn execute(&mut self, input: &str) -> AtResult<SIZE>
pub fn execute(&mut self, input: &str) -> AtResult<SIZE>
Parse and execute an AT command string
§Arguments
input- The raw AT command string (e.g., “AT+CMD?”)
§Returns
Ok(Bytes<SIZE>)- Success response from the command handlerErr(AtError)- Error if parsing fails or command is not found
Examples found in repository?
examples/basic_parser.rs (line 85)
70pub extern "C" fn main() -> ! {
71 let mut cmd1 = TestCommand { value: 0 };
72 let mut cmd2 = TestCommand { value: 5 };
73 let mut cmd3 = TestCommand { value: 10 };
74
75 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
76
77 let commands: &mut [(&str, &mut TestCommand)] = &mut [
78 ("AT+CMD1", &mut cmd1),
79 ("AT+CMD2", &mut cmd2),
80 ("AT+CMD3", &mut cmd3),
81 ];
82 parser.set_commands(commands);
83
84 // Execute (no-op result, just exercising the API)
85 let _ = parser.execute("AT+CMD1");
86 let _ = parser.execute("AT+CMD1?");
87 let _ = parser.execute("AT+CMD1=?");
88 let _ = parser.execute("AT+CMD1=42");
89 let _ = parser.execute("AT+CMD1?");
90 let _ = parser.execute("AT+CMD2");
91 let _ = parser.execute("AT+CMD2?");
92 let _ = parser.execute("AT+CMD3=100");
93 let _ = parser.execute("AT+CMD3?");
94 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
95 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
96
97 loop {}
98}Auto Trait Implementations§
impl<'a, T, const SIZE: usize> Freeze for AtParser<'a, T, SIZE>where
T: ?Sized,
impl<'a, T, const SIZE: usize> RefUnwindSafe for AtParser<'a, T, SIZE>where
T: RefUnwindSafe + ?Sized,
impl<'a, T, const SIZE: usize> Send for AtParser<'a, T, SIZE>
impl<'a, T, const SIZE: usize> Sync for AtParser<'a, T, SIZE>
impl<'a, T, const SIZE: usize> Unpin for AtParser<'a, T, SIZE>where
T: ?Sized,
impl<'a, T, const SIZE: usize> UnsafeUnpin for AtParser<'a, T, SIZE>where
T: ?Sized,
impl<'a, T, const SIZE: usize> !UnwindSafe for AtParser<'a, T, SIZE>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more