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 34)
33 fn set(&mut self, args: Args) -> AtResult<'static> {
34 if let Some(data) = args.get(0) {
35 self.write(data);
36 Ok("SENT")
37 } else {
38 Err(AtError::InvalidArgs)
39 }
40 }
41}
42
43// Device configuration context
44struct ConfigContext;
45
46impl AtContext for ConfigContext {
47 fn exec(&self) -> AtResult<'static> {
48 // Not supported for SETCFG
49 Err(AtError::NotSupported)
50 }
51
52 fn query(&mut self) -> AtResult<'static> {
53 // Return current configuration as a static string
54 // In real implementation, format the values dynamically
55 Ok("115200,1")
56 }
57
58 fn test(&mut self) -> AtResult<'static> {
59 // Return supported configuration options
60 Ok("+SETCFG: (baudrate),(mode)")
61 }
62
63 fn set(&mut self, args: Args) -> AtResult<'static> {
64 let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
65 let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
66 match (baud, mode) {
67 (Some(b), Some(m)) => unsafe {
68 // Configure UART
69 UART.baudrate = b;
70 UART.mode = m;
71 Ok("CONFIGURED")
72 },
73 _ => Err(AtError::InvalidArgs),
74 }
75 }examples/complete_usage.rs (line 37)
36 fn set(&mut self, args: Args) -> AtResult<'static> {
37 let value = args.get(0).ok_or(AtError::InvalidArgs)?;
38 match value {
39 "0" => {
40 self.echo = false;
41 Ok("ECHO OFF")
42 }
43 "1" => {
44 self.echo = true;
45 Ok("ECHO ON")
46 }
47 _ => Err(AtError::InvalidArgs),
48 }
49 }
50}
51
52/// Reset command module - simulates system reset
53pub struct ResetModule;
54
55impl AtContext for ResetModule {
56 /// Execute: perform reset
57 fn exec(&self) -> AtResult<'static> {
58 println!(" [System reset triggered]");
59 Ok("OK - System reset")
60 }
61
62 /// Test: show command description
63 fn test(&mut self) -> AtResult<'static> {
64 Ok("Reset the system")
65 }
66}
67
68/// Info command module - provides system information
69pub struct InfoModule {
70 pub version: &'static str,
71}
72
73impl AtContext for InfoModule {
74 /// Execute: return system info
75 fn exec(&self) -> AtResult<'static> {
76 Ok(self.version)
77 }
78
79 /// Query: return detailed info
80 fn query(&mut self) -> AtResult<'static> {
81 Ok("AT-Parser-RS v1.0.0 - AT Command Parser Library")
82 }
83}
84
85/// LED command module - controls an LED with multiple parameters
86pub struct LedModule {
87 pub state: bool,
88 pub brightness: u8,
89}
90
91impl AtContext for LedModule {
92 /// Execute: return current LED state
93 fn exec(&self) -> AtResult<'static> {
94 if self.state {
95 Ok("LED: ON")
96 } else {
97 Ok("LED: OFF")
98 }
99 }
100
101 /// Query: return state and brightness
102 fn query(&mut self) -> AtResult<'static> {
103 if self.state {
104 Ok("1,100")
105 } else {
106 Ok("0,0")
107 }
108 }
109
110 /// Test: show usage
111 fn test(&mut self) -> AtResult<'static> {
112 Ok("AT+LED=<state>,<brightness> where state: 0|1, brightness: 0-100")
113 }
114
115 /// Set: change LED state and brightness
116 fn set(&mut self, args: Args) -> AtResult<'static> {
117 let state_str = args.get(0).ok_or(AtError::InvalidArgs)?;
118
119 self.state = match state_str {
120 "0" => false,
121 "1" => true,
122 _ => return Err(AtError::InvalidArgs),
123 };
124
125 // Optional brightness parameter
126 if let Some(brightness_str) = args.get(1) {
127 self.brightness = brightness_str
128 .parse::<u8>()
129 .map_err(|_| AtError::InvalidArgs)?;
130
131 if self.brightness > 100 {
132 return Err(AtError::InvalidArgs);
133 }
134 }
135
136 if self.state {
137 Ok("LED ON")
138 } else {
139 Ok("LED OFF")
140 }
141 }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> 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