From e1b6514f24e27fc2402ecf8650bae98ce3f481ca Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 17 Nov 2022 15:02:33 +0100 Subject: [PATCH] Fix clippy lints --- src/dec/decode.rs | 14 +++++++------- src/dec/mod.rs | 28 +++++++++++++--------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/dec/decode.rs b/src/dec/decode.rs index edde630..89f704e 100644 --- a/src/dec/decode.rs +++ b/src/dec/decode.rs @@ -54,7 +54,7 @@ impl<'a> From>> for DecodeError<'a> { // ---- /// Decodes a NetText string into the term it represents. -pub fn decode<'a>(input: &'a [u8]) -> DecodeResult<'a, Term<'a, 'static>> { +pub fn decode(input: &[u8]) -> DecodeResult<'_, Term<'_, 'static>> { let (rest, term) = decode_term(input)?; let (end, _) = take_while(is_whitespace)(rest)?; if !end.is_empty() { @@ -63,7 +63,7 @@ pub fn decode<'a>(input: &'a [u8]) -> DecodeResult<'a, Term<'a, 'static>> { Ok(Term(term)) } -fn decode_term<'a>(input: &'a [u8]) -> IResult<&'a [u8], AnyTerm<'a, 'static>> { +fn decode_term(input: &[u8]) -> IResult<&'_ [u8], AnyTerm<'_, 'static>> { eprintln!("DT: `{}`", debug(input)); let (start, _) = take_while(is_whitespace)(input)?; eprintln!("DT2: `{}`", debug(start)); @@ -79,7 +79,7 @@ fn decode_term<'a>(input: &'a [u8]) -> IResult<&'a [u8], AnyTerm<'a, 'static>> { } } -fn decode_nonlist_term<'a>(input: &'a [u8]) -> IResult<&'a [u8], NonListTerm<'a, 'static>> { +fn decode_nonlist_term(input: &[u8]) -> IResult<&'_ [u8], NonListTerm<'_, 'static>> { eprintln!("DNLT: `{}`", debug(input)); let (rest, term) = alt(( map(decode_str, NonListTerm::Str), @@ -89,7 +89,7 @@ fn decode_nonlist_term<'a>(input: &'a [u8]) -> IResult<&'a [u8], NonListTerm<'a, Ok((rest, term)) } -fn decode_str<'a>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8]> { +fn decode_str(input: &[u8]) -> IResult<&'_ [u8], &'_ [u8]> { eprintln!("DS: `{}`", debug(input)); let (rest, data) = take_while1(is_string_char)(input)?; Ok((rest, data)) @@ -97,7 +97,7 @@ fn decode_str<'a>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8]> { type DictType<'a> = (&'a [u8], HashMap<&'a [u8], AnyTerm<'a, 'static>>); -fn decode_dict<'a>(dict_begin: &'a [u8]) -> IResult<&'a [u8], DictType<'a>> { +fn decode_dict(dict_begin: &[u8]) -> IResult<&'_ [u8], DictType<'_>> { eprintln!("DDbegin: `{}`", debug(dict_begin)); let (d, _) = tag(DICT_OPEN)(dict_begin)?; eprintln!("DD2: `{}`", debug(d)); @@ -117,13 +117,13 @@ fn decode_dict<'a>(dict_begin: &'a [u8]) -> IResult<&'a [u8], DictType<'a>> { Ok((dict_end, (dict_raw, dict))) } -fn dict_separator<'a>(d: &'a [u8]) -> IResult<&'a [u8], ()> { +fn dict_separator(d: &[u8]) -> IResult<&'_ [u8], ()> { let (d, _) = take_while(is_whitespace)(d)?; let (d, _) = tag(DICT_DELIM)(d)?; Ok((d, ())) } -fn decode_dict_item<'a>(d: &'a [u8]) -> IResult<&'a [u8], (&'a [u8], AnyTerm<'a, 'static>)> { +fn decode_dict_item(d: &[u8]) -> IResult<&'_ [u8], (&'_ [u8], AnyTerm<'_, 'static>)> { eprintln!("DDI: `{}`", debug(d)); let (d, _) = take_while(is_whitespace)(d)?; eprintln!("DDI1: `{}`", debug(d)); diff --git a/src/dec/mod.rs b/src/dec/mod.rs index 8731a26..9c1e7d7 100644 --- a/src/dec/mod.rs +++ b/src/dec/mod.rs @@ -199,17 +199,15 @@ impl<'a, 'b> Term<'a, 'b> { /// ``` pub fn list_of_first(&self) -> Result<[Term<'a, '_>; N], TypeError> { match self.0.mkref() { - AnyTerm::ListRef(raw, list) => { - if list.len() < N { - Err(TypeError::WrongLength(list.len(), N)) - } else if list.len() == N { - Ok(list - .iter() - .map(|x| Term(x.mkref().into())) - .collect::>() - .try_into() - .unwrap()) - } else { + AnyTerm::ListRef(raw, list) => match list.len().cmp(&N) { + std::cmp::Ordering::Less => Err(TypeError::WrongLength(list.len(), N)), + std::cmp::Ordering::Equal => Ok(list + .iter() + .map(|x| Term(x.mkref().into())) + .collect::>() + .try_into() + .unwrap()), + std::cmp::Ordering::Greater => { let mut ret = Vec::with_capacity(N); for item in list[0..N - 1].iter() { ret.push(Term(item.mkref().into())); @@ -227,7 +225,7 @@ impl<'a, 'b> Term<'a, 'b> { Ok(ret.try_into().unwrap()) } - } + }, x if N == 1 => Ok([Term(x)] .into_iter() .collect::>() @@ -342,7 +340,7 @@ impl<'a, 'b> AnyTerm<'a, 'b> { fn mkref(&self) -> AnyTerm<'a, '_> { match &self { AnyTerm::Str(s) => AnyTerm::Str(s), - AnyTerm::Dict(r, d) => AnyTerm::DictRef(r, &d), + AnyTerm::Dict(r, d) => AnyTerm::DictRef(r, d), AnyTerm::DictRef(r, d) => AnyTerm::DictRef(r, d), AnyTerm::List(r, l) => AnyTerm::ListRef(r, &l[..]), AnyTerm::ListRef(r, l) => AnyTerm::ListRef(r, l), @@ -361,7 +359,7 @@ impl<'a, 'b> NonListTerm<'a, 'b> { fn mkref(&self) -> NonListTerm<'a, '_> { match &self { NonListTerm::Str(s) => NonListTerm::Str(s), - NonListTerm::Dict(r, d) => NonListTerm::DictRef(r, &d), + NonListTerm::Dict(r, d) => NonListTerm::DictRef(r, d), NonListTerm::DictRef(r, d) => NonListTerm::DictRef(r, d), } } @@ -387,7 +385,7 @@ impl<'a, 'b> std::fmt::Display for Term<'a, 'b> { // ---- DEBUG REPR ---- -pub fn debug<'a>(x: &'a [u8]) -> &'a str { +pub fn debug(x: &[u8]) -> &str { std::str::from_utf8(x).unwrap_or("") }