From 6c74860f82ce8afb2623a30ebec30dd8b600680a Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 24 Jul 2023 09:24:38 +0200 Subject: [PATCH] apply clippy --fix --- src/error.rs | 2 +- src/header.rs | 5 ++--- src/parse.rs | 2 +- src/part/part.rs | 6 ++---- src/rfc5322/datetime.rs | 16 ++++++++-------- src/rfc5322/mailbox.rs | 10 +++++----- src/rfc5322/trace.rs | 10 +++++----- src/text/encoding.rs | 2 +- src/text/misc_token.rs | 12 ++++++------ src/text/quoted.rs | 8 ++++---- src/text/whitespace.rs | 10 +++++----- src/text/words.rs | 10 +++++----- 12 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/error.rs b/src/error.rs index 7a4dadf..21f9f03 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use nom; + #[derive(Debug, PartialEq)] pub enum EMLError<'a> { diff --git a/src/header.rs b/src/header.rs index 365e1d7..bc82efd 100644 --- a/src/header.rs +++ b/src/header.rs @@ -23,11 +23,10 @@ impl<'a, T> CompFieldList<'a, T> { pub fn known(self) -> Vec { self.0 .into_iter() - .map(|v| match v { + .filter_map(|v| match v { CompField::Known(f) => Some(f), _ => None, }) - .flatten() .collect() } } @@ -81,7 +80,7 @@ pub fn opt_field(input: &[u8]) -> IResult<&[u8], (&[u8], Unstructured)> { terminated( pair( terminated( - take_while1(|c| c >= 0x21 && c <= 0x7E && c != 0x3A), + take_while1(|c| (0x21..=0x7E).contains(&c) && c != 0x3A), tuple((space0, tag(b":"), space0)), ), unstructured, diff --git a/src/parse.rs b/src/parse.rs index c8bb891..834964c 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -11,5 +11,5 @@ fn main() { let eml = eml_codec::email(&rawmail).unwrap(); println!("{:#?}", eml); assert!(eml.1.date.is_some()); - assert!(eml.1.from.len() > 0); + assert!(!eml.1.from.is_empty()); } diff --git a/src/part/part.rs b/src/part/part.rs index 7a31ae7..84789b7 100644 --- a/src/part/part.rs +++ b/src/part/part.rs @@ -96,13 +96,11 @@ impl<'a> CompFieldList<'a, MixedField<'a>> { k.into_iter().partition(|v| v.mime().is_some()); let mime = v1 .into_iter() - .map(|v| v.to_mime()) - .flatten() + .filter_map(|v| v.to_mime()) .collect::(); let imf = v2 .into_iter() - .map(|v| v.to_imf()) - .flatten() + .filter_map(|v| v.to_imf()) .collect::(); (mime, imf) } diff --git a/src/rfc5322/datetime.rs b/src/rfc5322/datetime.rs index efc50c8..4a3d00b 100644 --- a/src/rfc5322/datetime.rs +++ b/src/rfc5322/datetime.rs @@ -144,7 +144,7 @@ fn strict_year(input: &[u8]) -> IResult<&[u8], i32> { delimited( fws, map( - terminated(take_while_m_n(4, 9, |c| c >= 0x30 && c <= 0x39), digit0), + terminated(take_while_m_n(4, 9, |c| (0x30..=0x39).contains(&c)), digit0), |d: &[u8]| { encoding_rs::UTF_8 .decode_without_bom_handling(d) @@ -162,15 +162,15 @@ fn obs_year(input: &[u8]) -> IResult<&[u8], i32> { map( delimited( opt(cfws), - terminated(take_while_m_n(2, 7, |c| c >= 0x30 && c <= 0x39), digit0), + terminated(take_while_m_n(2, 7, |c| (0x30..=0x39).contains(&c)), digit0), opt(cfws), ), |cap: &[u8]| { let year_txt = encoding_rs::UTF_8.decode_without_bom_handling(cap).0; let d = year_txt.parse::().unwrap_or(0); - if d >= 0 && d <= 49 { + if (0..=49).contains(&d) { 2000 + d - } else if d >= 50 && d <= 999 { + } else if (50..=999).contains(&d) { 1900 + d } else { d @@ -227,8 +227,8 @@ fn strict_zone(input: &[u8]) -> IResult<&[u8], Option> { tuple(( opt(fws), is_a("+-"), - take_while_m_n(2, 2, |c| c >= 0x30 && c <= 0x39), - take_while_m_n(2, 2, |c| c >= 0x30 && c <= 0x39), + take_while_m_n(2, 2, |c| (0x30..=0x39).contains(&c)), + take_while_m_n(2, 2, |c| (0x30..=0x39).contains(&c)), )), |(_, op, dig_zone_hour, dig_zone_min)| { let zone_hour: i32 = @@ -293,7 +293,7 @@ fn obs_zone(input: &[u8]) -> IResult<&[u8], Option> { value(FixedOffset::west_opt(0 * HOUR), tag_no_case(b"Z")), // Military Timezones East alt(( - value(FixedOffset::east_opt(1 * HOUR), tag_no_case(b"A")), + value(FixedOffset::east_opt(HOUR), tag_no_case(b"A")), value(FixedOffset::east_opt(2 * HOUR), tag_no_case(b"B")), value(FixedOffset::east_opt(3 * HOUR), tag_no_case(b"C")), value(FixedOffset::east_opt(4 * HOUR), tag_no_case(b"D")), @@ -308,7 +308,7 @@ fn obs_zone(input: &[u8]) -> IResult<&[u8], Option> { )), // Military Timezones West alt(( - value(FixedOffset::west_opt(1 * HOUR), tag_no_case(b"N")), + value(FixedOffset::west_opt(HOUR), tag_no_case(b"N")), value(FixedOffset::west_opt(2 * HOUR), tag_no_case(b"O")), value(FixedOffset::west_opt(3 * HOUR), tag_no_case(b"P")), value(FixedOffset::west_opt(4 * HOUR), tag_no_case(b"Q")), diff --git a/src/rfc5322/mailbox.rs b/src/rfc5322/mailbox.rs index b8f80dd..40641f7 100644 --- a/src/rfc5322/mailbox.rs +++ b/src/rfc5322/mailbox.rs @@ -166,9 +166,9 @@ fn obs_local_part(input: &[u8]) -> IResult<&[u8], LocalPart> { map( many0(alt(( map(tag(&[ascii::PERIOD]), |_| LocalPartToken::Dot), - map(word, |v| LocalPartToken::Word(v)), + map(word, LocalPartToken::Word), ))), - |v| LocalPart(v), + LocalPart, )(input) } @@ -223,7 +223,7 @@ impl<'a> fmt::Debug for Domain<'a> { /// ``` pub fn obs_domain(input: &[u8]) -> IResult<&[u8], Domain> { alt(( - map(separated_list1(tag("."), atom), |v| Domain::Atoms(v)), + map(separated_list1(tag("."), atom), Domain::Atoms), domain_litteral, ))(input) } @@ -244,12 +244,12 @@ fn domain_litteral(input: &[u8]) -> IResult<&[u8], Domain> { fn inner_domain_litteral(input: &[u8]) -> IResult<&[u8], Domain> { map( terminated(many0(preceded(opt(fws), take_while1(is_dtext))), opt(fws)), - |v| Domain::Litteral(v), + Domain::Litteral, )(input) } fn is_strict_dtext(c: u8) -> bool { - (c >= 0x21 && c <= 0x5A) || (c >= 0x5E && c <= 0x7E) + (0x21..=0x5A).contains(&c) || (0x5E..=0x7E).contains(&c) } /// Is domain text diff --git a/src/rfc5322/trace.rs b/src/rfc5322/trace.rs index 7d399ac..d26a658 100644 --- a/src/rfc5322/trace.rs +++ b/src/rfc5322/trace.rs @@ -46,7 +46,7 @@ pub fn received_log(input: &[u8]) -> IResult<&[u8], ReceivedLog> { } pub fn return_path(input: &[u8]) -> IResult<&[u8], Option> { - alt((map(mailbox::angle_addr, |a| Some(a)), empty_path))(input) + alt((map(mailbox::angle_addr, Some), empty_path))(input) } fn empty_path(input: &[u8]) -> IResult<&[u8], Option> { @@ -63,12 +63,12 @@ fn empty_path(input: &[u8]) -> IResult<&[u8], Option> { fn received_tokens(input: &[u8]) -> IResult<&[u8], ReceivedLogToken> { alt(( terminated( - map(misc_token::word, |x| ReceivedLogToken::Word(x)), + map(misc_token::word, ReceivedLogToken::Word), not(is_a([ascii::PERIOD, ascii::AT])), ), - map(mailbox::angle_addr, |x| ReceivedLogToken::Addr(x)), - map(mailbox::addr_spec, |x| ReceivedLogToken::Addr(x)), - map(mailbox::obs_domain, |x| ReceivedLogToken::Domain(x)), + map(mailbox::angle_addr, ReceivedLogToken::Addr), + map(mailbox::addr_spec, ReceivedLogToken::Addr), + map(mailbox::obs_domain, ReceivedLogToken::Domain), ))(input) } diff --git a/src/text/encoding.rs b/src/text/encoding.rs index d3171c1..4fe8d91 100644 --- a/src/text/encoding.rs +++ b/src/text/encoding.rs @@ -127,7 +127,7 @@ pub fn ptext(input: &[u8]) -> IResult<&[u8], Vec> { } fn safe_char2(input: &[u8]) -> IResult<&[u8], QuotedChunk> { - map(take_while1(is_safe_char2), |v| QuotedChunk::Safe(v))(input) + map(take_while1(is_safe_char2), QuotedChunk::Safe)(input) } /// RFC2047 section 4.2 diff --git a/src/text/misc_token.rs b/src/text/misc_token.rs index 039276d..acd8b61 100644 --- a/src/text/misc_token.rs +++ b/src/text/misc_token.rs @@ -83,9 +83,9 @@ impl<'a> fmt::Debug for Word<'a> { /// ``` pub fn word(input: &[u8]) -> IResult<&[u8], Word> { alt(( - map(quoted_string, |v| Word::Quoted(v)), - map(encoded_word, |v| Word::Encoded(v)), - map(atom, |v| Word::Atom(v)), + map(quoted_string, Word::Quoted), + map(encoded_word, Word::Encoded), + map(atom, Word::Atom), ))(input) } @@ -113,7 +113,7 @@ impl<'a> fmt::Debug for Phrase<'a> { /// phrase = 1*word / obs-phrase /// ``` pub fn phrase(input: &[u8]) -> IResult<&[u8], Phrase> { - let (input, phrase) = map(many1(word), |v| Phrase(v))(input)?; + let (input, phrase) = map(many1(word), Phrase)(input)?; Ok((input, phrase)) } @@ -188,8 +188,8 @@ pub fn unstructured(input: &[u8]) -> IResult<&[u8], Unstructured> { let (input, r) = many0(preceded( opt(fws), alt(( - map(encoded_word, |v| UnstrToken::Encoded(v)), - map(take_while1(is_unstructured), |v| UnstrToken::Plain(v)), + map(encoded_word, UnstrToken::Encoded), + map(take_while1(is_unstructured), UnstrToken::Plain), )), ))(input)?; diff --git a/src/text/quoted.rs b/src/text/quoted.rs index 3bd1d85..f3b0dcb 100644 --- a/src/text/quoted.rs +++ b/src/text/quoted.rs @@ -52,8 +52,8 @@ pub fn quoted_pair(input: &[u8]) -> IResult<&[u8], &[u8]> { /// ``` fn is_restr_qtext(c: u8) -> bool { c == ascii::EXCLAMATION - || (c >= ascii::NUM && c <= ascii::LEFT_BRACKET) - || (c >= ascii::RIGHT_BRACKET && c <= ascii::TILDE) + || (ascii::NUM..=ascii::LEFT_BRACKET).contains(&c) + || (ascii::RIGHT_BRACKET..=ascii::TILDE).contains(&c) } fn is_qtext(c: u8) -> bool { @@ -85,7 +85,7 @@ pub fn quoted_string(input: &[u8]) -> IResult<&[u8], QuotedString> { let mut qstring = content .iter() .fold(QuotedString::default(), |mut acc, (maybe_wsp, c)| { - if let Some(_) = maybe_wsp { + if maybe_wsp.is_some() { acc.push(&[ascii::SP]); } acc.push(c); @@ -93,7 +93,7 @@ pub fn quoted_string(input: &[u8]) -> IResult<&[u8], QuotedString> { }); let (input, maybe_wsp) = opt(fws)(input)?; - if let Some(_) = maybe_wsp { + if maybe_wsp.is_some() { qstring.push(&[ascii::SP]); } diff --git a/src/text/whitespace.rs b/src/text/whitespace.rs index 66c7e5c..0e8650a 100644 --- a/src/text/whitespace.rs +++ b/src/text/whitespace.rs @@ -117,9 +117,9 @@ pub fn is_ctext(c: u8) -> bool { /// obs-ctext ///``` pub fn is_restr_ctext(c: u8) -> bool { - (c >= ascii::EXCLAMATION && c <= ascii::SQUOTE) - || (c >= ascii::ASTERISK && c <= ascii::LEFT_BRACKET) - || (c >= ascii::RIGHT_BRACKET && c <= ascii::TILDE) + (ascii::EXCLAMATION..=ascii::SQUOTE).contains(&c) + || (ascii::ASTERISK..=ascii::LEFT_BRACKET).contains(&c) + || (ascii::RIGHT_BRACKET..=ascii::TILDE).contains(&c) } /// US ASCII control characters without effect @@ -132,10 +132,10 @@ pub fn is_restr_ctext(c: u8) -> bool { /// %d127 ; white space characters /// ``` pub fn is_obs_no_ws_ctl(c: u8) -> bool { - (c >= ascii::SOH && c <= ascii::BS) + (ascii::SOH..=ascii::BS).contains(&c) || c == ascii::VT || c == ascii::FF - || (c >= ascii::SO && c <= ascii::US) + || (ascii::SO..=ascii::US).contains(&c) || c == ascii::DEL } diff --git a/src/text/words.rs b/src/text/words.rs index 8cd2ce8..6ed7c90 100644 --- a/src/text/words.rs +++ b/src/text/words.rs @@ -10,7 +10,7 @@ use nom::{ }; pub fn is_vchar(c: u8) -> bool { - c >= ascii::EXCLAMATION && c <= ascii::TILDE + (ascii::EXCLAMATION..=ascii::TILDE).contains(&c) } /// MIME Token allowed characters @@ -101,10 +101,10 @@ mod tests { #[test] fn test_atext() { - assert!(is_atext('=' as u8)); - assert!(is_atext('5' as u8)); - assert!(is_atext('Q' as u8)); - assert!(!is_atext(' ' as u8)); + assert!(is_atext(b'=')); + assert!(is_atext(b'5')); + assert!(is_atext(b'Q')); + assert!(!is_atext(b' ')); //assert!(is_atext('É')); // support utf8 }