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<Cow<'a, str>>

Get an argument by index (0-based) Arguments are separated by commas, except when they are inside double-quoted strings.

When an argument is wrapped in double quotes, the outer quotes are removed from the returned value and escaped quotes (\") are decoded to ".

Examples found in repository?
examples/embedded_basic.rs (line 40)
38fn parse_args_example() -> AtResult<'static, SIZE> {
39    let args = Args { raw: "foo,bar,baz" };
40    match args.get(1) {
41        Some(val) => Ok(at_response!(SIZE, AT_RESP; val.as_ref())),
42        None => Err((AT_RESP, AtError::InvalidArgs)),
43    }
44}
More examples
Hide additional examples
examples/embedded_uart_config.rs (line 67)
66    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
67        let data = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
68        self.write(data.as_ref());
69        Ok(at_response!(SIZE, at_response; "SENT"))
70    }
71}
72
73/// Device configuration command — get/set baudrate and mode
74struct ConfigModule {
75    baudrate: u32,
76    mode: u8,
77}
78
79impl ConfigModule {
80    const fn new() -> Self {
81        Self { baudrate: 115200, mode: 0 }
82    }
83}
84
85impl AtContext<SIZE> for ConfigModule {
86    /// Query: return current configuration as `<baudrate>,<mode>`
87    fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
88        Ok(at_response!(SIZE, at_response; self.baudrate, self.mode))
89    }
90
91    /// Test: return supported configuration ranges
92    fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
93        Ok(at_response!(SIZE, at_response; "<baudrate: 9600-115200>,<mode: 0|1>"))
94    }
95
96    /// Set: apply new baudrate and mode
97    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
98        let baudrate = args.get(0)
99            .ok_or((at_response, AtError::InvalidArgs))?
100            .parse::<u32>()
101            .map_err(|_| (at_response, AtError::InvalidArgs))?;
102
103        let mode = args.get(1)
104            .ok_or((at_response, AtError::InvalidArgs))?
105            .parse::<u8>()
106            .map_err(|_| (at_response, AtError::InvalidArgs))?;
107
108        if mode > 1 {
109            return Err((at_response, AtError::InvalidArgs));
110        }
111
112        self.baudrate = baudrate;
113        self.mode = mode;
114        // configure_uart(baudrate, mode);  // apply to hardware here
115
116        Ok(at_response!(SIZE, at_response; "OK"))
117    }
examples/basic_parser.rs (line 57)
56    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
57        let val_str = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
58        self.value = val_str.parse().map_err(|_| (at_response, AtError::InvalidArgs))?;
59        Ok(at_response!(SIZE, at_response; "OK"))
60    }
examples/embedded_error_handling.rs (line 61)
60    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
61        let val = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
62        match val.as_ref() {
63            "0" => { self.armed = false; Ok(at_response!(SIZE, at_response; "OK")) }
64            "1" => { self.armed = true;  Ok(at_response!(SIZE, at_response; "OK")) }
65            _ => Err((at_response, AtError::InvalidArgs)),
66        }
67    }
examples/complete_usage.rs (line 61)
60    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
61        let value = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
62        match value.as_ref() {
63            "0" => {
64                self.echo = false;
65                Ok(at_response!(SIZE, at_response; "OK"))
66            }
67            "1" => {
68                self.echo = true;
69                Ok(at_response!(SIZE, at_response; "OK"))
70            }
71            _ => Err((at_response, AtError::InvalidArgs)),
72        }
73    }
74}
75
76/// Reset command module - simulates system reset
77pub struct ResetModule;
78
79impl AtContext<SIZE> for ResetModule {
80    /// Execute: perform reset
81    fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
82        Ok(at_response!(SIZE, at_response; "OK"))
83    }
84
85    /// Test: show command description
86    fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
87        Ok(at_response!(SIZE, at_response; "Reset the system"))
88    }
89}
90
91/// Info command module - provides system information
92pub struct InfoModule {
93    pub version: &'static str,
94}
95
96impl AtContext<SIZE> for InfoModule {
97    /// Execute: return version string
98    fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
99        Ok(at_response!(SIZE, at_response; self.version))
100    }
101
102    /// Query: return detailed info
103    fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
104        Ok(at_response!(SIZE, at_response; "AT-Parser-RS - AT Command Parser Library"))
105    }
106}
107
108/// LED command module - controls an LED with multiple parameters
109pub struct LedModule {
110    pub state: bool,
111    pub brightness: u8,
112}
113
114impl AtContext<SIZE> for LedModule {
115    /// Execute: return current LED state
116    fn exec(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
117        Ok(at_response!(SIZE, at_response; if self.state { "ON" } else { "OFF" }))
118    }
119
120    /// Query: return state and brightness
121    fn query(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
122        Ok(at_response!(SIZE, at_response; self.state as u8, self.brightness))
123    }
124
125    /// Test: show usage
126    fn test(&mut self, at_response: &'static str) -> AtResult<'_, SIZE> {
127        Ok(at_response!(SIZE, at_response; "<state: 0|1>,<brightness: 0-100>"))
128    }
129
130    /// Set: change LED state and brightness
131    fn set(&mut self, at_response: &'static str, args: Args) -> AtResult<'_, SIZE> {
132        let state_str = args.get(0).ok_or((at_response, AtError::InvalidArgs))?;
133
134        self.state = match state_str.as_ref() {
135            "0" => false,
136            "1" => true,
137            _ => return Err((at_response, AtError::InvalidArgs)),
138        };
139
140        // Optional brightness parameter
141        if let Some(brightness_str) = args.get(1) {
142            let bri = brightness_str
143                .parse::<u8>()
144                .map_err(|_| (at_response, AtError::InvalidArgs))?;
145
146            if bri > 100 {
147                return Err((at_response, AtError::InvalidArgs));
148            }
149            self.brightness = bri;
150        }
151
152        Ok(at_response!(SIZE, at_response; "OK"))
153    }
Source

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

Get an argument by index without decoding escape sequences.

Quoted arguments are still returned without the surrounding quotes.

Source

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

Backward-compatible alias for Args::get.

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.