Skip to main content

embedded_error_handling/
embedded_error_handling.rs

1/***************************************************************************
2 *
3 * AT Command Parser
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
18 *
19 ***************************************************************************/
20 
21//! Advanced example: macro usage and custom error handling
22//! This example demonstrates code patterns suitable for no_std environments
23
24#![allow(dead_code)]
25
26extern crate at_parser_rs;
27
28use at_parser_rs::{Args, AtError};
29
30// Example macro to register commands (mock, since AtContext is missing)
31macro_rules! dummy_at_modules {
32    ($($name:expr => $module:expr),* $(,)?) => {
33        // In a real case, modules would be registered here
34        // In no_std, be careful not to use heap or mutable statics without sync
35    };
36}
37
38dummy_at_modules! {
39    "CMD1" => 1,
40    "CMD2" => 2,
41}
42
43// Function that simulates AT command handling
44fn handle_at_command<'a>(cmd: &str, args: &'a str) -> Result<&'a str, AtError> {
45    match cmd {
46        "CMD1" => {
47            let a = Args { raw: args };
48            a.get(0).ok_or(AtError::InvalidArgs)
49        }
50        "CMD2" => Ok("OK"),
51        _ => Err(AtError::UnknownCommand),
52    }
53}
54
55// Example call
56fn example_usage() -> &'static str {
57    match handle_at_command("CMD1", "foo,bar") {
58        Ok(val) => val,
59        Err(_) => "Errore",
60    }
61}
62
63// Mock main for compilation (in real embedded code, this would be in your firmware)
64fn main() {
65    // Example usage - in embedded this would be called from your main loop
66    let _result = example_usage();
67}