pub struct Args<'a> {
pub raw: &'a str,
}Expand description
Structure holding the arguments passed to an AT command
Fields§
§raw: &'a strRaw argument string (comma-separated values)
Implementations§
Source§impl<'a> Args<'a>
impl<'a> Args<'a>
Sourcepub fn get(&self, index: usize) -> Option<Cow<'a, str>>
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?
More examples
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 }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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more