Skip to content

Commit

Permalink
Add LEN as a const associated to AtatCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rmja committed Dec 19, 2023
1 parent d3997ad commit 28340c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions atat/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub trait AtatUrc {
///
/// impl<'a> AtatCmd for SetGreetingText<'a> {
/// type Response = NoResponse;
/// const LEN: usize = 64;
///
/// fn write(&self, mut buf: &mut [u8]) -> usize {
/// assert!(buf.len() >= Self::LEN);
/// let buf_len = buf.len();
/// use embedded_io::Write;
/// write!(buf, "AT+CSGT={}", self.text);
Expand All @@ -62,6 +64,9 @@ pub trait AtatCmd {
/// The type of the response. Must implement the `AtatResp` trait.
type Response: AtatResp;

/// The size of the buffer required to write the request.
const LEN: usize;

/// Whether or not this command can be aborted.
const CAN_ABORT: bool = false;

Expand Down Expand Up @@ -94,6 +99,7 @@ impl<const L: usize> AtatResp for String<L> {}

impl<const L: usize> AtatCmd for String<L> {
type Response = String<256>;
const LEN: usize = L;

fn write(&self, buf: &mut [u8]) -> usize {
let bytes = self.as_bytes();
Expand Down
10 changes: 10 additions & 0 deletions atat_derive/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ pub fn atat_cmd(input: TokenStream) -> TokenStream {
None => quote! {},
};

let mut cmd_len = cmd_prefix.len() + cmd.len() + termination.len();
if value_sep {
cmd_len += 1;
}
if quote_escape_strings {
cmd_len += 2;
}

let (field_names, field_names_str): (Vec<_>, Vec<_>) = variants
.iter()
.map(|f| {
Expand All @@ -93,6 +101,8 @@ pub fn atat_cmd(input: TokenStream) -> TokenStream {
impl #impl_generics atat::AtatCmd for #ident #ty_generics #where_clause {
type Response = #resp;

const LEN: usize = { #ident_len + #cmd_len };

#timeout

#abortable
Expand Down

0 comments on commit 28340c5

Please sign in to comment.