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

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> 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.