Args

Struct Args 

Source
pub struct Args<'a> {
    pub raw: &'a str,
}
Expand description

Structure holding the arguments passed to an AT command

Fields§

§raw: &'a str

Raw argument string (comma-separated values)

Implementations§

Source§

impl<'a> Args<'a>

Source

pub fn get(&self, index: usize) -> Option<&'a str>

Get an argument by index (0-based) Arguments are separated by commas

Examples found in repository?
examples/embedded_basic.rs (line 13)
11fn parse_args_example() -> AtResult<'static> {
12    let args = Args { raw: "foo,bar,baz" };
13    match args.get(1) {
14        Some(val) => Ok(val),
15        None => Err(AtError::InvalidArgs),
16    }
17}
More examples
Hide additional examples
examples/basic_parser.rs (line 34)
33    fn set(&mut self, args: Args) -> AtResult<'static> {
34        let val_str = args.get(0).ok_or(AtError::InvalidArgs)?;
35        self.value = val_str.parse().map_err(|_| AtError::InvalidArgs)?;
36        Ok("OK")
37    }
examples/embedded_uart_config.rs (line 34)
33    fn set(&mut self, args: Args) -> AtResult<'static> {
34        if let Some(data) = args.get(0) {
35            self.write(data);
36            Ok("SENT")
37        } else {
38            Err(AtError::InvalidArgs)
39        }
40    }
41}
42
43// Device configuration context
44struct ConfigContext;
45
46impl AtContext for ConfigContext {
47    fn exec(&self) -> AtResult<'static> {
48        // Not supported for SETCFG
49        Err(AtError::NotSupported)
50    }
51    
52    fn query(&mut self) -> AtResult<'static> {
53        // Return current configuration as a static string
54        // In real implementation, format the values dynamically
55        Ok("115200,1")
56    }
57    
58    fn test(&mut self) -> AtResult<'static> {
59        // Return supported configuration options
60        Ok("+SETCFG: (baudrate),(mode)")
61    }
62    
63    fn set(&mut self, args: Args) -> AtResult<'static> {
64        let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
65        let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
66        match (baud, mode) {
67            (Some(b), Some(m)) => unsafe {
68                // Configure UART
69                UART.baudrate = b;
70                UART.mode = m;
71                Ok("CONFIGURED")
72            },
73            _ => Err(AtError::InvalidArgs),
74        }
75    }
examples/embedded_error_handling.rs (line 28)
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}
examples/complete_usage.rs (line 37)
36    fn set(&mut self, args: Args) -> AtResult<'static> {
37        let value = args.get(0).ok_or(AtError::InvalidArgs)?;
38        match value {
39            "0" => {
40                self.echo = false;
41                Ok("ECHO OFF")
42            }
43            "1" => {
44                self.echo = true;
45                Ok("ECHO ON")
46            }
47            _ => Err(AtError::InvalidArgs),
48        }
49    }
50}
51
52/// Reset command module - simulates system reset
53pub struct ResetModule;
54
55impl AtContext for ResetModule {
56    /// Execute: perform reset
57    fn exec(&self) -> AtResult<'static> {
58        println!("  [System reset triggered]");
59        Ok("OK - System reset")
60    }
61
62    /// Test: show command description
63    fn test(&mut self) -> AtResult<'static> {
64        Ok("Reset the system")
65    }
66}
67
68/// Info command module - provides system information
69pub struct InfoModule {
70    pub version: &'static str,
71}
72
73impl AtContext for InfoModule {
74    /// Execute: return system info
75    fn exec(&self) -> AtResult<'static> {
76        Ok(self.version)
77    }
78
79    /// Query: return detailed info
80    fn query(&mut self) -> AtResult<'static> {
81        Ok("AT-Parser-RS v1.0.0 - AT Command Parser Library")
82    }
83}
84
85/// LED command module - controls an LED with multiple parameters
86pub struct LedModule {
87    pub state: bool,
88    pub brightness: u8,
89}
90
91impl AtContext for LedModule {
92    /// Execute: return current LED state
93    fn exec(&self) -> AtResult<'static> {
94        if self.state {
95            Ok("LED: ON")
96        } else {
97            Ok("LED: OFF")
98        }
99    }
100
101    /// Query: return state and brightness
102    fn query(&mut self) -> AtResult<'static> {
103        if self.state {
104            Ok("1,100")
105        } else {
106            Ok("0,0")
107        }
108    }
109
110    /// Test: show usage
111    fn test(&mut self) -> AtResult<'static> {
112        Ok("AT+LED=<state>,<brightness> where state: 0|1, brightness: 0-100")
113    }
114
115    /// Set: change LED state and brightness
116    fn set(&mut self, args: Args) -> AtResult<'static> {
117        let state_str = args.get(0).ok_or(AtError::InvalidArgs)?;
118        
119        self.state = match state_str {
120            "0" => false,
121            "1" => true,
122            _ => return Err(AtError::InvalidArgs),
123        };
124
125        // Optional brightness parameter
126        if let Some(brightness_str) = args.get(1) {
127            self.brightness = brightness_str
128                .parse::<u8>()
129                .map_err(|_| AtError::InvalidArgs)?;
130            
131            if self.brightness > 100 {
132                return Err(AtError::InvalidArgs);
133            }
134        }
135
136        if self.state {
137            Ok("LED ON")
138        } else {
139            Ok("LED OFF")
140        }
141    }

Auto Trait Implementations§

§

impl<'a> Freeze for Args<'a>

§

impl<'a> RefUnwindSafe for Args<'a>

§

impl<'a> Send for Args<'a>

§

impl<'a> Sync for Args<'a>

§

impl<'a> Unpin for Args<'a>

§

impl<'a> UnwindSafe for Args<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.