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