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<&'a str>

Get an argument by index (0-based) Arguments are separated by commas

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

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.