at_parser_rs/
lib.rs

1/***************************************************************************
2 *
3 * AT Command Parser
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20//! AT Command Parser Library
21//!
22//! This library provides a flexible parser for AT commands, commonly used in
23//! embedded systems and communication devices. It supports no_std environments.
24
25#![cfg_attr(any(feature = "enable_panic", feature = "osal_rs"), no_std)]
26
27#[cfg(any(feature = "enable_panic", feature = "osal_rs"))]
28extern crate alloc;
29
30#[cfg(feature = "osal_rs")]
31extern crate osal_rs;
32
33#[cfg(any(feature = "enable_panic", feature = "osal_rs"))]
34use core::prelude::rust_2021::*;
35use core::iter::Iterator;
36use core::option::Option;
37use core::result::Result;
38
39#[cfg(feature = "enable_panic")]
40#[panic_handler]
41fn panic(_info: &core::panic::PanicInfo) -> ! {
42    loop {}
43}
44
45pub mod context;
46pub mod parser;
47
48
49/// Error types that can occur during AT command processing
50#[derive(Debug)]
51pub enum AtError {
52    /// The command is not recognized
53    UnknownCommand,
54    /// The command is recognized but not supported
55    NotSupported,
56    /// The command arguments are invalid
57    InvalidArgs,
58}
59
60/// Result type for AT command operations
61/// Returns either a static string response or an AtError
62pub type AtResult<'a> = Result<&'a str, AtError>;
63
64/// Structure holding the arguments passed to an AT command
65pub struct Args<'a> {
66    /// Raw argument string (comma-separated values)
67    pub raw: &'a str,
68}
69
70impl<'a> Args<'a> {
71    /// Get an argument by index (0-based)
72    /// Arguments are separated by commas
73    pub fn get(&self, index: usize) -> Option<&'a str> {
74        self.raw.split(',').nth(index)
75    }
76}
77
78
79/// Macro to define AT command modules
80/// Creates a static array of command names and their associated context handlers
81#[macro_export]
82macro_rules! at_modules {
83    (
84        $( $name:expr => $module:ident ),* $(,)?
85    ) => {
86        static COMMANDS: &[(&'static str, &mut dyn AtContext)] = unsafe {
87            &[
88                $(
89                    ($name, &mut $module),
90                )*
91            ]
92        };
93    };
94}