at_parser_rs/
lib.rs

1
2//! AT Command Parser Library
3//!
4//! This library provides a flexible parser for AT commands, commonly used in
5//! embedded systems and communication devices. It supports no_std environments.
6
7#![cfg_attr(feature = "enable_panic", no_std)]
8
9#[cfg(feature = "enable_panic")]
10extern crate alloc;
11
12#[cfg(feature = "osal_rs")]
13extern crate osal_rs;
14
15#[cfg(feature = "enable_panic")]
16#[panic_handler]
17fn panic(_info: &core::panic::PanicInfo) -> ! {
18    loop {}
19}
20
21pub mod context;
22pub mod parser;
23
24
25/// Error types that can occur during AT command processing
26#[derive(Debug)]
27pub enum AtError {
28    /// The command is not recognized
29    UnknownCommand,
30    /// The command is recognized but not supported
31    NotSupported,
32    /// The command arguments are invalid
33    InvalidArgs,
34}
35
36/// Result type for AT command operations
37/// Returns either a static string response or an AtError
38pub type AtResult<'a> = Result<&'a str, AtError>;
39
40/// Structure holding the arguments passed to an AT command
41pub struct Args<'a> {
42    /// Raw argument string (comma-separated values)
43    pub raw: &'a str,
44}
45
46impl<'a> Args<'a> {
47    /// Get an argument by index (0-based)
48    /// Arguments are separated by commas
49    pub fn get(&self, index: usize) -> Option<&'a str> {
50        self.raw.split(',').nth(index)
51    }
52}
53
54
55/// Macro to define AT command modules
56/// Creates a static array of command names and their associated context handlers
57#[macro_export]
58macro_rules! at_modules {
59    (
60        $( $name:expr => $module:ident ),* $(,)?
61    ) => {
62        static COMMANDS: &[(&'static str, &mut dyn AtContext)] = unsafe {
63            &[
64                $(
65                    ($name, &mut $module),
66                )*
67            ]
68        };
69    };
70}