Skip to content

Commit

Permalink
Add reattempt on parse err flag to AtatCmd (#178)
Browse files Browse the repository at this point in the history
* add reattempt_on_parse_err flag to AtatCmd

* add missing parsing of reattempt_on_parse_err in atat_cmd attribute

* fix REATTEMPT_ON_PARSE_ERR type in macro expand

* make REATTEMPT_ON_PARSE_ERR default to true
  • Loading branch information
dragonnn authored Nov 16, 2023
1 parent c7d3cb3 commit c4881f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions atat/src/asynch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub trait AtatClient {

match self.send(cmd).await {
Err(Error::Timeout) => {}
Err(Error::Parse) => {
if !Cmd::REATTEMPT_ON_PARSE_ERR {
return Err(Error::Parse);
}
}
r => return r,
}
}
Expand Down
4 changes: 4 additions & 0 deletions atat/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub trait AtatCmd<const LEN: usize> {
/// using `send_retry`.
const ATTEMPTS: u8 = 1;

/// Whether or not to reattempt a command on a parse error
/// using `send_retry`.
const REATTEMPT_ON_PARSE_ERR: bool = true;

/// Force client to look for a response.
/// Empty slice is then passed to parse by client.
/// Implemented to enhance expandability of ATAT
Expand Down
12 changes: 12 additions & 0 deletions atat_derive/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn atat_cmd(input: TokenStream) -> TokenStream {
resp,
timeout_ms,
attempts,
reattempt_on_parse_err,
abortable,
value_sep,
cmd_prefix,
Expand Down Expand Up @@ -59,6 +60,15 @@ pub fn atat_cmd(input: TokenStream) -> TokenStream {
None => quote! {},
};

Check warning on line 61 in atat_derive/src/cmd.rs

View workflow job for this annotation

GitHub Actions / clippy

use Option::map_or_else instead of an if let/else

warning: use Option::map_or_else instead of an if let/else --> atat_derive/src/cmd.rs:54:20 | 54 | let attempts = match attempts { | ____________________^ 55 | | Some(attempts) => { 56 | | quote! { 57 | | const ATTEMPTS: u8 = #attempts; ... | 60 | | None => quote! {}, 61 | | }; | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else help: try | 54 ~ let attempts = attempts.map_or_else(|| quote! {}, |attempts| quote! { 55 + const ATTEMPTS: u8 = #attempts; 56 ~ }); |

Check warning on line 61 in atat_derive/src/cmd.rs

View workflow job for this annotation

GitHub Actions / clippy

use Option::map_or_else instead of an if let/else

warning: use Option::map_or_else instead of an if let/else --> atat_derive/src/cmd.rs:54:20 | 54 | let attempts = match attempts { | ____________________^ 55 | | Some(attempts) => { 56 | | quote! { 57 | | const ATTEMPTS: u8 = #attempts; ... | 60 | | None => quote! {}, 61 | | }; | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else help: try | 54 ~ let attempts = attempts.map_or_else(|| quote! {}, |attempts| quote! { 55 + const ATTEMPTS: u8 = #attempts; 56 ~ }); |

let reattempt_on_parse_err = match reattempt_on_parse_err {
Some(reattempt_on_parse_err) => {
quote! {
const REATTEMPT_ON_PARSE_ERR: bool = #reattempt_on_parse_err;
}
}
None => quote! {},
};

Check warning on line 70 in atat_derive/src/cmd.rs

View workflow job for this annotation

GitHub Actions / clippy

use Option::map_or_else instead of an if let/else

warning: use Option::map_or_else instead of an if let/else --> atat_derive/src/cmd.rs:63:34 | 63 | let reattempt_on_parse_err = match reattempt_on_parse_err { | __________________________________^ 64 | | Some(reattempt_on_parse_err) => { 65 | | quote! { 66 | | const REATTEMPT_ON_PARSE_ERR: bool = #reattempt_on_parse_err; ... | 69 | | None => quote! {}, 70 | | }; | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else help: try | 63 ~ let reattempt_on_parse_err = reattempt_on_parse_err.map_or_else(|| quote! {}, |reattempt_on_parse_err| quote! { 64 + const REATTEMPT_ON_PARSE_ERR: bool = #reattempt_on_parse_err; 65 ~ }); |

Check warning on line 70 in atat_derive/src/cmd.rs

View workflow job for this annotation

GitHub Actions / clippy

use Option::map_or_else instead of an if let/else

warning: use Option::map_or_else instead of an if let/else --> atat_derive/src/cmd.rs:63:34 | 63 | let reattempt_on_parse_err = match reattempt_on_parse_err { | __________________________________^ 64 | | Some(reattempt_on_parse_err) => { 65 | | quote! { 66 | | const REATTEMPT_ON_PARSE_ERR: bool = #reattempt_on_parse_err; ... | 69 | | None => quote! {}, 70 | | }; | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else help: try | 63 ~ let reattempt_on_parse_err = reattempt_on_parse_err.map_or_else(|| quote! {}, |reattempt_on_parse_err| quote! { 64 + const REATTEMPT_ON_PARSE_ERR: bool = #reattempt_on_parse_err; 65 ~ }); |

// let quote_escape_strings = !matches!(quote_escape_strings, Some(false));

let mut cmd_len = cmd_prefix.len() + cmd.len() + termination.len();
Expand Down Expand Up @@ -99,6 +109,8 @@ pub fn atat_cmd(input: TokenStream) -> TokenStream {

#attempts

#reattempt_on_parse_err

#[inline]
fn as_bytes(&self) -> atat::heapless::Vec<u8, { #ident_len + #cmd_len }> {
match atat::serde_at::to_vec(self, #cmd, atat::serde_at::SerializeOptions {
Expand Down
16 changes: 16 additions & 0 deletions atat_derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct CmdAttributes {
pub timeout_ms: Option<u32>,
pub attempts: Option<u8>,
pub abortable: Option<bool>,
pub reattempt_on_parse_err: Option<bool>,
pub value_sep: bool,
pub cmd_prefix: String,
pub termination: String,
Expand Down Expand Up @@ -253,6 +254,7 @@ impl Parse for CmdAttributes {
timeout_ms: None,
attempts: None,
abortable: None,
reattempt_on_parse_err: None,
value_sep: true,
cmd_prefix: String::from("AT"),
termination: String::from("\r\n"),
Expand Down Expand Up @@ -289,6 +291,20 @@ impl Parse for CmdAttributes {
))
}
}
} else if optional.path.is_ident("reattempt_on_parse_err") {
match optional.value {
Expr::Lit(ExprLit {
lit: Lit::Bool(v), ..
}) => {
at_cmd.reattempt_on_parse_err = Some(v.value);
}
_ => {
return Err(Error::new(
call_site,
"expected bool value for 'reattempt_on_parse_err'",
))
}
}
} else if optional.path.is_ident("abortable") {
match optional.value {
Expr::Lit(ExprLit {
Expand Down

0 comments on commit c4881f4

Please sign in to comment.