Skip to main content

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 43)
41fn parse_args_example() -> AtResult<SIZE> {
42    let args = Args { raw: "foo,bar,baz" };
43    match args.get(1) {
44        Some(val) => Ok(Bytes::from_str(val)),
45        None => Err(AtError::InvalidArgs),
46    }
47}
More examples
Hide additional examples
examples/basic_parser.rs (line 63)
62    fn set(&mut self, args: Args) -> AtResult<SIZE> {
63        let val_str = args.get(0).ok_or(AtError::InvalidArgs)?;
64        self.value = val_str.parse().map_err(|_| AtError::InvalidArgs)?;
65        Ok(Bytes::from_str("OK"))
66    }
examples/embedded_uart_config.rs (line 64)
63    fn set(&mut self, args: Args) -> AtResult<SIZE> {
64        if let Some(data) = args.get(0) {
65            self.write(data);
66            Ok(Bytes::from_str("SENT"))
67        } else {
68            Err(AtError::InvalidArgs)
69        }
70    }
71}
72
73// Device configuration context
74struct ConfigContext;
75
76impl AtContext<SIZE> for ConfigContext {
77    fn exec(&self) -> AtResult<SIZE> {
78        // Not supported for SETCFG
79        Err(AtError::NotSupported)
80    }
81    
82    fn query(&mut self) -> AtResult<SIZE> {
83        // Return current configuration as a static string
84        // In real implementation, format the values dynamically
85        Ok(Bytes::from_str("115200,1"))
86    }
87    
88    fn test(&mut self) -> AtResult<SIZE> {
89        // Return supported configuration options
90        Ok(Bytes::from_str("+SETCFG: (baudrate),(mode)"))
91    }
92    
93    fn set(&mut self, args: Args) -> AtResult<SIZE> {
94        let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
95        let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
96        match (baud, mode) {
97            (Some(b), Some(m)) => unsafe {
98                // Configure UART
99                UART.baudrate = b;
100                UART.mode = m;
101                Ok(Bytes::from_str("CONFIGURED"))
102            },
103            _ => Err(AtError::InvalidArgs),
104        }
105    }
examples/embedded_error_handling.rs (line 56)
52fn handle_at_command<'a>(cmd: &str, args: &'a str) -> Result<&'a str, AtError> {
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}
examples/complete_usage.rs (line 68)
67    fn set(&mut self, args: Args) -> AtResult<SIZE> {
68        let value = args.get(0).ok_or(AtError::InvalidArgs)?;
69        match value {
70            "0" => {
71                self.echo = false;
72                Ok(Bytes::from_str("ECHO OFF"))
73            }
74            "1" => {
75                self.echo = true;
76                Ok(Bytes::from_str("ECHO ON"))
77            }
78            _ => Err(AtError::InvalidArgs),
79        }
80    }
81}
82
83/// Reset command module - simulates system reset
84pub struct ResetModule;
85
86impl AtContext<SIZE> for ResetModule {
87    /// Execute: perform reset
88    fn exec(&self) -> AtResult<SIZE> {
89        Ok(Bytes::from_str("OK - System reset"))
90    }
91
92    /// Test: show command description
93    fn test(&mut self) -> AtResult<SIZE> {
94        Ok(Bytes::from_str("Reset the system"))
95    }
96}
97
98/// Info command module - provides system information
99pub struct InfoModule {
100    pub version: &'static str,
101}
102
103impl AtContext<SIZE> for InfoModule {
104    /// Execute: return system info
105    fn exec(&self) -> AtResult<SIZE> {
106        Ok(Bytes::from_str(self.version))
107    }
108
109    /// Query: return detailed info
110    fn query(&mut self) -> AtResult<SIZE> {
111        Ok(Bytes::from_str("AT-Parser-RS v1.0.0 - AT Command Parser Library"))
112    }
113}
114
115/// LED command module - controls an LED with multiple parameters
116pub struct LedModule {
117    pub state: bool,
118    pub brightness: u8,
119}
120
121impl AtContext<SIZE> for LedModule {
122    /// Execute: return current LED state
123    fn exec(&self) -> AtResult<SIZE> {
124        if self.state {
125            Ok(Bytes::from_str("LED: ON"))
126        } else {
127            Ok(Bytes::from_str("LED: OFF"))
128        }
129    }
130
131    /// Query: return state and brightness
132    fn query(&mut self) -> AtResult<SIZE> {
133        if self.state {
134            Ok(Bytes::from_str("1,100"))
135        } else {
136            Ok(Bytes::from_str("0,0"))
137        }
138    }
139
140    /// Test: show usage
141    fn test(&mut self) -> AtResult<SIZE> {
142        Ok(Bytes::from_str("AT+LED=<state>,<brightness> where state: 0|1, brightness: 0-100"))
143    }
144
145    /// Set: change LED state and brightness
146    fn set(&mut self, args: Args) -> AtResult<SIZE> {
147        let state_str = args.get(0).ok_or(AtError::InvalidArgs)?;
148        
149        self.state = match state_str {
150            "0" => false,
151            "1" => true,
152            _ => return Err(AtError::InvalidArgs),
153        };
154
155        // Optional brightness parameter
156        if let Some(brightness_str) = args.get(1) {
157            self.brightness = brightness_str
158                .parse::<u8>()
159                .map_err(|_| AtError::InvalidArgs)?;
160            
161            if self.brightness > 100 {
162                return Err(AtError::InvalidArgs);
163            }
164        }
165
166        if self.state {
167            Ok(Bytes::from_str("LED ON"))
168        } else {
169            Ok(Bytes::from_str("LED OFF"))
170        }
171    }

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> UnsafeUnpin 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.