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<&'a str>
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?
More examples
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/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> 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