embedded_error_handling/
embedded_error_handling.rs

1//! Advanced example: macro usage and custom error handling
2//! This example demonstrates code patterns suitable for no_std environments
3
4#![allow(dead_code)]
5
6extern crate at_parser_rs;
7
8use at_parser_rs::{Args, AtError};
9
10// Example macro to register commands (mock, since AtContext is missing)
11macro_rules! dummy_at_modules {
12    ($($name:expr => $module:expr),* $(,)?) => {
13        // In a real case, modules would be registered here
14        // In no_std, be careful not to use heap or mutable statics without sync
15    };
16}
17
18dummy_at_modules! {
19    "CMD1" => 1,
20    "CMD2" => 2,
21}
22
23// Function that simulates AT command handling
24fn handle_at_command<'a>(cmd: &str, args: &'a str) -> Result<&'a str, AtError> {
25    match cmd {
26        "CMD1" => {
27            let a = Args { raw: args };
28            a.get(0).ok_or(AtError::InvalidArgs)
29        }
30        "CMD2" => Ok("OK"),
31        _ => Err(AtError::UnknownCommand),
32    }
33}
34
35// Example call
36fn example_usage() -> &'static str {
37    match handle_at_command("CMD1", "foo,bar") {
38        Ok(val) => val,
39        Err(_) => "Errore",
40    }
41}
42
43// Mock main for compilation (in real embedded code, this would be in your firmware)
44fn main() {
45    // Example usage - in embedded this would be called from your main loop
46    let _result = example_usage();
47}