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 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//! Advanced example: macro usage and custom error handling
21//! This example demonstrates code patterns suitable for no_std environments
22
23#![allow(dead_code)]
24
25extern crate at_parser_rs;
26
27use at_parser_rs::{Args, AtError};
28
29// Example macro to register commands (mock, since AtContext is missing)
30macro_rules! dummy_at_modules {
31    ($($name:expr => $module:expr),* $(,)?) => {
32        // In a real case, modules would be registered here
33        // In no_std, be careful not to use heap or mutable statics without sync
34    };
35}
36
37dummy_at_modules! {
38    "CMD1" => 1,
39    "CMD2" => 2,
40}
41
42// Function that simulates AT command handling
43fn handle_at_command<'a>(cmd: &str, args: &'a str) -> Result<&'a str, AtError> {
44    match cmd {
45        "CMD1" => {
46            let a = Args { raw: args };
47            a.get(0).ok_or(AtError::InvalidArgs)
48        }
49        "CMD2" => Ok("OK"),
50        _ => Err(AtError::UnknownCommand),
51    }
52}
53
54// Example call
55fn example_usage() -> &'static str {
56    match handle_at_command("CMD1", "foo,bar") {
57        Ok(val) => val,
58        Err(_) => "Errore",
59    }
60}
61
62// Mock main for compilation (in real embedded code, this would be in your firmware)
63fn main() {
64    // Example usage - in embedded this would be called from your main loop
65    let _result = example_usage();
66}