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/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/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 }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