Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(*): feature gate string to error conversions behind feature gate, for cme/cms errors #209

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ jobs:
strategy:
matrix:
target: ["x86_64-unknown-linux-gnu", "thumbv6m-none-eabi"]
features:
[
"",
"derive",
]
features: ["", "derive"]
include:
- target: "x86_64-unknown-linux-gnu"
extra_features: "std,log"
Expand Down Expand Up @@ -138,4 +134,4 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --features std,heapless
args: --workspace --features std,heapless,string_errors
1 change: 1 addition & 0 deletions atat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ custom-error-messages = []
std = ["serde_at/std", "nom/std", "embassy-time/std", "embedded-io/std"]
hex_str_arrays = []
heapless = ["serde_at/heapless"]
string_errors = []
9 changes: 7 additions & 2 deletions atat/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
pub trait Parser {
/// Parse a URC, if it exists.
///
/// - if no URC exists, return [ParseError::NoMatch]

Check warning on line 35 in atat/src/digest.rs

View workflow job for this annotation

GitHub Actions / clippy

item in documentation is missing backticks

warning: item in documentation is missing backticks --> atat/src/digest.rs:35:37 | 35 | /// - if no URC exists, return [ParseError::NoMatch] | ^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown = note: `-W clippy::doc-markdown` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::doc_markdown)]` help: try | 35 | /// - if no URC exists, return [`ParseError::NoMatch`] | ~~~~~~~~~~~~~~~~~~~~~
/// - if a URC exists but is incomplete, return [ParseError::Incomplete]

Check warning on line 36 in atat/src/digest.rs

View workflow job for this annotation

GitHub Actions / clippy

item in documentation is missing backticks

warning: item in documentation is missing backticks --> atat/src/digest.rs:36:54 | 36 | /// - if a URC exists but is incomplete, return [ParseError::Incomplete] | ^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown help: try | 36 | /// - if a URC exists but is incomplete, return [`ParseError::Incomplete`] | ~~~~~~~~~~~~~~~~~~~~~~~~
/// - if a URC exists and is complete, return it and its length
fn parse(buf: &[u8]) -> Result<(&[u8], usize), ParseError>;
}
Expand Down Expand Up @@ -201,7 +201,7 @@
branch::alt,
bytes::streaming::tag,
character::complete,
combinator::{eof, map, map_res, not, recognize},
combinator::{eof, map, map_res, recognize},
error::ParseError,
sequence::tuple,
IResult,
Expand Down Expand Up @@ -256,6 +256,7 @@
)
}),
// Matches the equivalent of regex: "\r\n\+CME ERROR:\s*([^\n\r]+)\r\n"
#[cfg(feature = "string_errors")]
map(string_error("\r\n+CME ERROR:"), |(error_msg, len)| {
(
DigestResult::Response(Err(InternalError::CmeError(CmeError::from_msg(
Expand All @@ -265,6 +266,7 @@
)
}),
// Matches the equivalent of regex: "\r\n\+CMS ERROR:\s*([^\n\r]+)\r\n"
#[cfg(feature = "string_errors")]
map(string_error("\r\n+CMS ERROR:"), |(error_msg, len)| {
(
DigestResult::Response(Err(InternalError::CmsError(CmsError::from_msg(
Expand Down Expand Up @@ -397,6 +399,7 @@
}

/// Matches the equivalent of regex: "{token}\s*([^\n\r]+)\r\n"
#[cfg(feature = "string_errors")]
fn string_error<'a, T, Error: ParseError<&'a [u8]>>(
token: T,
) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], (&'a [u8], usize), Error>
Expand All @@ -407,7 +410,7 @@
move |i| {
let (i, (prefix_data, _, error_msg)) = tuple((
recognize(take_until_including(token.clone())),
not(tag("\r")),
nom::combinator::not(tag("\r")),
recognize(take_until_including("\r\n")),
))(i)?;

Expand Down Expand Up @@ -466,19 +469,19 @@
}

fn trim_ascii_whitespace(x: &[u8]) -> &[u8] {
let from = match x.iter().position(|x| !x.is_ascii_whitespace()) {
Some(i) => i,
None => return &x[0..0],
};

Check warning on line 475 in atat/src/digest.rs

View workflow job for this annotation

GitHub Actions / clippy

this could be rewritten as `let...else`

warning: this could be rewritten as `let...else` --> atat/src/digest.rs:472:9 | 472 | / let from = match x.iter().position(|x| !x.is_ascii_whitespace()) { 473 | | Some(i) => i, 474 | | None => return &x[0..0], 475 | | }; | |__________^ help: consider writing: `let Some(from) = x.iter().position(|x| !x.is_ascii_whitespace()) else { return &x[0..0] };` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else = note: `-W clippy::manual-let-else` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::manual_let_else)]`
let to = x.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
&x[from..=to]
}

pub fn trim_start_ascii_space(x: &[u8]) -> &[u8] {

Check warning on line 480 in atat/src/digest.rs

View workflow job for this annotation

GitHub Actions / clippy

this function could have a `#[must_use]` attribute

warning: this function could have a `#[must_use]` attribute --> atat/src/digest.rs:480:5 | 480 | pub fn trim_start_ascii_space(x: &[u8]) -> &[u8] { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn trim_start_ascii_space(x: &[u8]) -> &[u8]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate = note: `-W clippy::must-use-candidate` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::must_use_candidate)]`
match x.iter().position(|&x| x != b' ') {
Some(offset) => &x[offset..],
None => &x[0..0],
}

Check warning on line 484 in atat/src/digest.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/src/digest.rs:481:9 | 481 | / match x.iter().position(|&x| x != b' ') { 482 | | Some(offset) => &x[offset..], 483 | | None => &x[0..0], 484 | | } | |_________^ help: try: `x.iter().position(|&x| x != b' ').map_or_else(|| &x[0..0], |offset| &x[offset..])` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else = note: `-W clippy::option-if-let-else` implied by `-W clippy::nursery` = help: to override `-W clippy::nursery` add `#[allow(clippy::option_if_let_else)]`
}
}
#[cfg(test)]
Expand Down Expand Up @@ -554,6 +557,7 @@
}

#[test]
#[cfg(feature = "string_errors")]
fn mm_error() {
let tests: Vec<(&[u8], DigestResult, usize)> = vec![
(b"\r\nUNKNOWN COMMAND\r\n", DigestResult::None, 0),
Expand Down Expand Up @@ -1044,6 +1048,7 @@
}

#[test]
#[cfg(feature = "string_errors")]
fn verbose_error_response() {
let mut digester = AtDigester::<UrcTestParser>::new();
let mut buf = heapless::Vec::<u8, TEST_RX_BUF_LEN>::new();
Expand Down
1 change: 1 addition & 0 deletions atat/src/error/cme_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ impl From<u16> for CmeError {
}
}

#[cfg(feature = "string_errors")]
impl CmeError {
pub const fn from_msg(s: &[u8]) -> Self {
// FIXME:
Expand Down
1 change: 1 addition & 0 deletions atat/src/error/cms_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl From<u16> for CmsError {
}
}

#[cfg(feature = "string_errors")]
impl CmsError {
pub const fn from_msg(s: &[u8]) -> Self {
// FIXME:
Expand Down
Loading