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