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 const fn new() -> Self
pub const fn new() -> Self
Create a new empty parser with no registered commands.
Call set_commands before dispatching any
input with execute.
§Example
let mut parser: AtParser<MyHandler, SIZE> = AtParser::new();
// parser has no commands yet; execute() will return Err(UnknownCommand)Examples found in repository?
71pub extern "C" fn main() -> ! {
72 let mut cmd1 = TestCommand { value: 0 };
73 let mut cmd2 = TestCommand { value: 5 };
74 let mut cmd3 = TestCommand { value: 10 };
75
76 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
77
78 let commands: &mut [(&str, &mut TestCommand)] = &mut [
79 ("AT+CMD1", &mut cmd1),
80 ("AT+CMD2", &mut cmd2),
81 ("AT+CMD3", &mut cmd3),
82 ];
83 parser.set_commands(commands);
84
85 // Execute (no-op result, just exercising the API)
86 let _ = parser.execute("AT+CMD1");
87 let _ = parser.execute("AT+CMD1?");
88 let _ = parser.execute("AT+CMD1=?");
89 let _ = parser.execute("AT+CMD1=42");
90 let _ = parser.execute("AT+CMD1?");
91 let _ = parser.execute("AT+CMD2");
92 let _ = parser.execute("AT+CMD2?");
93 let _ = parser.execute("AT+CMD3=100");
94 let _ = parser.execute("AT+CMD3?");
95 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
96 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
97
98 loop {}
99}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 the commands that this parser will dispatch.
The slice maps each AT command name to a mutable reference to its
handler. Command names are matched verbatim and case-sensitively
against the prefix of the input string (before any suffix such as
?, =?, or =<args>).
§Arguments
commands— mutable slice of(&'static str, &mut T)pairs
§Example
struct PingModule;
impl AtContext<SIZE> for PingModule {
fn exec(&mut self) -> AtResult<'_, SIZE> { Ok(Bytes::from_str("PONG")) }
}
let mut ping = PingModule;
let mut parser: AtParser<PingModule, SIZE> = AtParser::new();
let commands: &mut [(&str, &mut PingModule)] = &mut [
("AT+PING", &mut ping),
];
parser.set_commands(commands);Using trait objects to mix different handler types:
let mut ping = PingModule;
let mut echo = EchoModule;
let mut parser: AtParser<dyn AtContext<SIZE>, SIZE> = AtParser::new();
let commands: &mut [(&str, &mut dyn AtContext<SIZE>)] = &mut [
("AT+PING", &mut ping),
("AT+ECHO", &mut echo),
];
parser.set_commands(commands);Examples found in repository?
71pub extern "C" fn main() -> ! {
72 let mut cmd1 = TestCommand { value: 0 };
73 let mut cmd2 = TestCommand { value: 5 };
74 let mut cmd3 = TestCommand { value: 10 };
75
76 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
77
78 let commands: &mut [(&str, &mut TestCommand)] = &mut [
79 ("AT+CMD1", &mut cmd1),
80 ("AT+CMD2", &mut cmd2),
81 ("AT+CMD3", &mut cmd3),
82 ];
83 parser.set_commands(commands);
84
85 // Execute (no-op result, just exercising the API)
86 let _ = parser.execute("AT+CMD1");
87 let _ = parser.execute("AT+CMD1?");
88 let _ = parser.execute("AT+CMD1=?");
89 let _ = parser.execute("AT+CMD1=42");
90 let _ = parser.execute("AT+CMD1?");
91 let _ = parser.execute("AT+CMD2");
92 let _ = parser.execute("AT+CMD2?");
93 let _ = parser.execute("AT+CMD3=100");
94 let _ = parser.execute("AT+CMD3?");
95 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
96 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
97
98 loop {}
99}Sourcepub fn execute<'b>(&'b mut self, input: &'b str) -> AtResult<'b, SIZE>
pub fn execute<'b>(&'b mut self, input: &'b str) -> AtResult<'b, SIZE>
Parse and execute an AT command string.
Leading and trailing whitespace is stripped before parsing. The command name is matched against the registered commands; if found, the appropriate handler method is called based on the command form detected from the suffix.
§Arguments
input— raw AT command string (e.g."AT+CMD?","AT+CMD=1,2")
§Returns
Ok(Bytes<SIZE>)— response buffer returned by the matched handlerErr(AtError::UnknownCommand)— no handler found for the command nameErr(AtError::NotSupported)— handler found but the requested form is not implementedErr(AtError::InvalidArgs)— handler returned an argument error
§Example
struct EchoModule { enabled: bool }
impl AtContext<SIZE> for EchoModule {
fn query(&mut self) -> AtResult<'_, SIZE> {
Ok(Bytes::from_str(if self.enabled { "1" } else { "0" }))
}
fn set(&mut self, args: Args) -> AtResult<'_, SIZE> {
let value = args.get(0).ok_or(AtError::InvalidArgs)?;
match value.as_ref() {
"0" => { self.enabled = false; Ok(Bytes::from_str("OK")) }
"1" => { self.enabled = true; Ok(Bytes::from_str("OK")) }
_ => Err(AtError::InvalidArgs),
}
}
}
let mut echo = EchoModule { enabled: false };
let mut parser: AtParser<EchoModule, SIZE> = AtParser::new();
let commands: &mut [(&str, &mut EchoModule)] = &mut [("AT+ECHO", &mut echo)];
parser.set_commands(commands);
assert!(parser.execute("AT+ECHO=1").is_ok()); // sets echo on
assert!(parser.execute("AT+ECHO?").is_ok()); // queries state
assert!(parser.execute("AT+UNKNOWN").is_err()); // Err(UnknownCommand)
assert!(parser.execute("AT+ECHO=9").is_err()); // Err(InvalidArgs)Examples found in repository?
71pub extern "C" fn main() -> ! {
72 let mut cmd1 = TestCommand { value: 0 };
73 let mut cmd2 = TestCommand { value: 5 };
74 let mut cmd3 = TestCommand { value: 10 };
75
76 let mut parser: AtParser<TestCommand, SIZE> = AtParser::new();
77
78 let commands: &mut [(&str, &mut TestCommand)] = &mut [
79 ("AT+CMD1", &mut cmd1),
80 ("AT+CMD2", &mut cmd2),
81 ("AT+CMD3", &mut cmd3),
82 ];
83 parser.set_commands(commands);
84
85 // Execute (no-op result, just exercising the API)
86 let _ = parser.execute("AT+CMD1");
87 let _ = parser.execute("AT+CMD1?");
88 let _ = parser.execute("AT+CMD1=?");
89 let _ = parser.execute("AT+CMD1=42");
90 let _ = parser.execute("AT+CMD1?");
91 let _ = parser.execute("AT+CMD2");
92 let _ = parser.execute("AT+CMD2?");
93 let _ = parser.execute("AT+CMD3=100");
94 let _ = parser.execute("AT+CMD3?");
95 let _ = parser.execute("AT+UNKNOWN"); // -> Err(UnknownCommand)
96 let _ = parser.execute("AT+CMD1=abc"); // -> Err(InvalidArgs)
97
98 loop {}
99}