From 4f9bf16117f93c0bbfd5cf773bfe62edef859e9f Mon Sep 17 00:00:00 2001 From: Eric Rodrigues Pires Date: Sat, 11 Oct 2025 18:33:52 -0300 Subject: [PATCH] Clippy --- axum_duper/src/lib.rs | 10 ++--- duper/src/ast.rs | 2 +- duper/src/builder.rs | 46 ++++++++++--------- duper/src/format.rs | 25 ++++++----- duper/src/parser.rs | 4 +- duper/src/pretty_printer.rs | 88 ++++++++++++++++++------------------- duper/src/serializer.rs | 6 +++ serde_duper/src/de.rs | 8 ++-- serde_duper/src/error.rs | 6 +-- serde_duper/src/lib.rs | 2 +- serde_duper/src/ser.rs | 4 +- serde_duper/tests/struct.rs | 2 +- 12 files changed, 106 insertions(+), 97 deletions(-) diff --git a/axum_duper/src/lib.rs b/axum_duper/src/lib.rs index 5f6f082..9449752 100644 --- a/axum_duper/src/lib.rs +++ b/axum_duper/src/lib.rs @@ -45,11 +45,11 @@ where { pub fn from_bytes(bytes: &[u8]) -> Result { let string = str::from_utf8(bytes).map_err(|_| DuperRejection::DuperDataError)?; - Self::from_str(string) + Self::from_string(string) } - pub fn from_str(string: &str) -> Result { - match serde_duper::from_str(string) { + pub fn from_string(string: &str) -> Result { + match serde_duper::from_string(string) { Ok(value) => Ok(Self(value)), Err(err) => match err.inner.kind { ErrorKind::ParseError(_) => Err(DuperRejection::DuperSyntaxError), @@ -81,7 +81,7 @@ where let string = String::from_request(req, state) .await .map_err(|_| DuperRejection::DuperDataError)?; - Self::from_str(&string) + Self::from_string(&string) } } @@ -102,7 +102,7 @@ where let string = String::from_request(req, state) .await .map_err(|_| DuperRejection::DuperDataError)?; - Ok(Some(Self::from_str(&string)?)) + Ok(Some(Self::from_string(&string)?)) } } diff --git a/duper/src/ast.rs b/duper/src/ast.rs index 4a4a4da..4623761 100644 --- a/duper/src/ast.rs +++ b/duper/src/ast.rs @@ -204,7 +204,7 @@ pub enum DuperInner<'a> { } impl<'a> DuperValue<'a> { - pub fn accept<'v, V: DuperVisitor>(&self, visitor: &'v mut V) -> V::Value { + pub fn accept(&self, visitor: &mut V) -> V::Value { match &self.inner { DuperInner::Object(object) => visitor.visit_object(self.identifier.as_ref(), object), DuperInner::Array(array) => visitor.visit_array(self.identifier.as_ref(), array), diff --git a/duper/src/builder.rs b/duper/src/builder.rs index c87946e..4d38405 100644 --- a/duper/src/builder.rs +++ b/duper/src/builder.rs @@ -19,18 +19,18 @@ pub(crate) struct DuperBuilder; impl DuperBuilder { pub(crate) fn build_duper_stream( pair: Pair<'_, Rule>, - ) -> Result>, Error> { + ) -> Result>, Box>> { pair.into_inner() .map(|inner_pair| Self::build_duper_trunk(inner_pair)) .collect() } - pub(crate) fn build_duper(pair: Pair<'_, Rule>) -> Result, Error> { + pub(crate) fn build_duper(pair: Pair<'_, Rule>) -> Result, Box>> { Self::build_duper_trunk(pair) } - fn build_duper_trunk(pair: Pair<'_, Rule>) -> Result, Error> { - let span = pair.as_span().clone(); + fn build_duper_trunk(pair: Pair<'_, Rule>) -> Result, Box>> { + let span = pair.as_span(); let mut duper_trunk = pair.into_inner(); let mut next = duper_trunk.next().unwrap(); let identifier = match next.as_rule() { @@ -51,23 +51,23 @@ impl DuperBuilder { inner: DuperInner::Array(Self::build_array(next)?), }, rule => { - return Err(Error::new_from_span( + return Err(Box::new(Error::new_from_span( ErrorVariant::CustomError { message: format!("unexpected rule in trunk {rule:?}"), }, span, - )); + ))); } }) } - fn build_object(pair: Pair<'_, Rule>) -> Result, Error> { + fn build_object(pair: Pair<'_, Rule>) -> Result, Box>> { debug_assert!(matches!(pair.as_rule(), Rule::object)); - let span = pair.as_span().clone(); + let span = pair.as_span(); let kv_pairs: Result, DuperValue<'_>)>, _> = pair .into_inner() .map(|pair| { - let span = pair.as_span().clone(); + let span = pair.as_span(); let mut inner_pair = pair.into_inner(); let key_pair = inner_pair.next().unwrap(); let key = match key_pair.as_rule() { @@ -79,12 +79,12 @@ impl DuperBuilder { } Rule::plain_key => Cow::Borrowed(key_pair.as_str()), rule => { - return Err(Error::new_from_span( + return Err(Box::new(Error::new_from_span( ErrorVariant::CustomError { message: format!("unexpected rule in object key {rule:?}"), }, span, - )); + ))); } }; let value = Self::build_value(inner_pair.next().unwrap()); @@ -96,16 +96,16 @@ impl DuperBuilder { if unique_keys.len() == kv_pairs.len() { Ok(DuperObject(kv_pairs)) } else { - Err(Error::new_from_span( + Err(Box::new(Error::new_from_span( ErrorVariant::CustomError { message: "duplicate keys in object".into(), }, span, - )) + ))) } } - fn build_array(pair: Pair<'_, Rule>) -> Result, Error> { + fn build_array(pair: Pair<'_, Rule>) -> Result, Box>> { debug_assert!(matches!(pair.as_rule(), Rule::array)); let vec: Result>, _> = pair .into_inner() @@ -114,7 +114,7 @@ impl DuperBuilder { Ok(DuperArray(vec?)) } - fn build_tuple(pair: Pair<'_, Rule>) -> Result, Error> { + fn build_tuple(pair: Pair<'_, Rule>) -> Result, Box>> { debug_assert!(matches!(pair.as_rule(), Rule::tuple)); let vec: Result>, _> = pair .into_inner() @@ -123,8 +123,8 @@ impl DuperBuilder { Ok(DuperTuple(vec?)) } - fn build_value(pair: Pair<'_, Rule>) -> Result, Error> { - let span = pair.as_span().clone(); + fn build_value(pair: Pair<'_, Rule>) -> Result, Box>> { + let span = pair.as_span(); let mut inner_pair = pair.into_inner(); let mut next = inner_pair.next().unwrap(); let identifier = match next.as_rule() { @@ -158,9 +158,7 @@ impl DuperBuilder { Rule::integer => DuperInner::Integer({ let integer_inner = next.into_inner().next().unwrap(); match integer_inner.as_rule() { - Rule::decimal_integer => { - i64::from_str_radix(integer_inner.as_str(), 10).unwrap() - } + Rule::decimal_integer => integer_inner.as_str().parse().unwrap(), Rule::hex_integer => { i64::from_str_radix(integer_inner.as_str().split_at(2).1, 16).unwrap() } @@ -171,12 +169,12 @@ impl DuperBuilder { i64::from_str_radix(integer_inner.as_str().split_at(2).1, 2).unwrap() } rule => { - return Err(Error::new_from_span( + return Err(Box::new(Error::new_from_span( ErrorVariant::CustomError { message: format!("unexpected rule in integer value {rule:?}"), }, span, - )); + ))); } } }), @@ -184,12 +182,12 @@ impl DuperBuilder { Rule::boolean => DuperInner::Boolean(next.as_str().parse().unwrap()), Rule::null => DuperInner::Null, rule => { - return Err(Error::new_from_span( + return Err(Box::new(Error::new_from_span( ErrorVariant::CustomError { message: format!("unexpected rule in value {rule:?}"), }, span, - )); + ))); } }, }) diff --git a/duper/src/format.rs b/duper/src/format.rs index 292fef0..04cc07f 100644 --- a/duper/src/format.rs +++ b/duper/src/format.rs @@ -45,7 +45,11 @@ fn format_cow_str<'a>(string: &Cow<'a, str>) -> Cow<'a, str> { max_hashtags = max_hashtags.max(curr_hashtags); } ' ' => was_hashtag = false, - '\r' | '\n' | '\t' | _ if char.is_control() || char.is_whitespace() => { + '\r' | '\n' | '\t' => { + has_char_that_should_be_escaped = true; + break; + } + char if char.is_control() || char.is_whitespace() => { has_char_that_should_be_escaped = true; break; } @@ -58,7 +62,7 @@ fn format_cow_str<'a>(string: &Cow<'a, str>) -> Cow<'a, str> { Cow::Owned(format!(r#"r{}"{}"{}"#, hashtags, string, hashtags)) } else { // Regular string with escaping - let escaped_key = Cow::from(escape_str(&string)).into_owned(); + let escaped_key = Cow::from(escape_str(string)).into_owned(); Cow::Owned(format!(r#""{escaped_key}""#)) } } @@ -91,9 +95,11 @@ pub(crate) fn format_duper_bytes<'a>(bytes: &'a DuperBytes<'a>) -> Cow<'a, str> max_hashtags = max_hashtags.max(curr_hashtags); } b' ' => was_hashtag = false, - b'\r' | b'\n' | b'\t' | _ - if byte.is_ascii_control() || byte.is_ascii_whitespace() => - { + b'\r' | b'\n' | b'\t' => { + has_char_that_should_be_escaped = true; + break; + } + byte if byte.is_ascii_control() || byte.is_ascii_whitespace() => { has_char_that_should_be_escaped = true; break; } @@ -103,17 +109,16 @@ pub(crate) fn format_duper_bytes<'a>(bytes: &'a DuperBytes<'a>) -> Cow<'a, str> if quotes > max_hashtags && !has_char_that_should_be_escaped { // Raw bytes let hashtags: String = (0..=max_hashtags).map(|_| '#').collect(); - let unesecaped_bytes: String = - bytes.0.into_iter().copied().map(|b| b as char).collect(); - return Cow::Owned(format!( + let unesecaped_bytes: String = bytes.0.iter().copied().map(|b| b as char).collect(); + Cow::Owned(format!( r#"r{}"{}"{}"#, hashtags, unesecaped_bytes, hashtags - )); + )) } else { // Regular bytes with escaping let escaped_bytes: String = bytes .0 - .into_iter() + .iter() .copied() .flat_map(ascii::escape_default) .map(|b| b as char) diff --git a/duper/src/parser.rs b/duper/src/parser.rs index a29e125..209142f 100644 --- a/duper/src/parser.rs +++ b/duper/src/parser.rs @@ -7,12 +7,12 @@ use crate::{ast::DuperValue, builder::DuperBuilder}; pub struct DuperParser; impl DuperParser { - pub fn parse_duper_stream(input: &'_ str) -> Result>, Error> { + pub fn parse_duper_stream(input: &'_ str) -> Result>, Box>> { let mut pairs = Self::parse(Rule::duper_stream, input)?; DuperBuilder::build_duper_stream(pairs.next().unwrap()) } - pub fn parse_duper(input: &'_ str) -> Result, Error> { + pub fn parse_duper(input: &'_ str) -> Result, Box>> { let mut pairs = Self::parse(Rule::duper, input)?; DuperBuilder::build_duper(pairs.next().unwrap()) } diff --git a/duper/src/pretty_printer.rs b/duper/src/pretty_printer.rs index 6aeb977..ac5c8cb 100644 --- a/duper/src/pretty_printer.rs +++ b/duper/src/pretty_printer.rs @@ -13,6 +13,12 @@ pub struct PrettyPrinter { indent: usize, } +impl Default for PrettyPrinter { + fn default() -> Self { + Self::new() + } +} + impl PrettyPrinter { pub fn new() -> Self { Self { indent: 0 } @@ -63,23 +69,21 @@ impl DuperVisitor for PrettyPrinter { string.push_str(&self.indentation()); string.push_str("})\n"); } + } else if object.is_empty() { + string.push_str("{}"); } else { - if object.is_empty() { - string.push_str("{}"); - } else { - string.push_str("{\n"); - self.increase_indentation(); - for (key, value) in object.iter() { - string.push_str(&self.indentation()); - string.push_str(&format_key(key)); - string.push_str(": "); - string.push_str(&value.accept(self)); - string.push_str(",\n"); - } - self.decrease_indentation(); + string.push_str("{\n"); + self.increase_indentation(); + for (key, value) in object.iter() { string.push_str(&self.indentation()); - string.push('}'); + string.push_str(&format_key(key)); + string.push_str(": "); + string.push_str(&value.accept(self)); + string.push_str(",\n"); } + self.decrease_indentation(); + string.push_str(&self.indentation()); + string.push('}'); } string @@ -108,21 +112,19 @@ impl DuperVisitor for PrettyPrinter { string.push_str(&self.indentation()); string.push_str("])\n"); } + } else if array.is_empty() { + string.push_str("[]"); } else { - if array.is_empty() { - string.push_str("[]"); - } else { - string.push_str("[\n"); - self.increase_indentation(); - for value in array.iter() { - string.push_str(&self.indentation()); - string.push_str(&value.accept(self)); - string.push_str(",\n"); - } - self.decrease_indentation(); + string.push_str("[\n"); + self.increase_indentation(); + for value in array.iter() { string.push_str(&self.indentation()); - string.push(']'); + string.push_str(&value.accept(self)); + string.push_str(",\n"); } + self.decrease_indentation(); + string.push_str(&self.indentation()); + string.push(']'); } string @@ -155,25 +157,23 @@ impl DuperVisitor for PrettyPrinter { string.push_str(&self.indentation()); string.push_str("))"); } + } else if tuple.is_empty() { + string.push_str("(,)"); + } else if tuple.len() == 1 { + string.push('('); + string.push_str(&tuple.get(0).unwrap().accept(self)); + string.push_str(",)"); } else { - if tuple.is_empty() { - string.push_str("(,)"); - } else if tuple.len() == 1 { - string.push_str("("); - string.push_str(&tuple.get(0).unwrap().accept(self)); - string.push_str(",)"); - } else { - string.push_str("(\n"); - self.increase_indentation(); - for value in tuple.iter() { - string.push_str(&self.indentation()); - string.push_str(&value.accept(self)); - string.push_str(",\n"); - } - self.decrease_indentation(); + string.push_str("(\n"); + self.increase_indentation(); + for value in tuple.iter() { string.push_str(&self.indentation()); - string.push(')'); + string.push_str(&value.accept(self)); + string.push_str(",\n"); } + self.decrease_indentation(); + string.push_str(&self.indentation()); + string.push(')'); } string @@ -196,7 +196,7 @@ impl DuperVisitor for PrettyPrinter { string.push('\n'); self.decrease_indentation(); string.push_str(&self.indentation()); - string.push_str(")"); + string.push(')'); string } else { format!("{identifier}({value})") @@ -222,7 +222,7 @@ impl DuperVisitor for PrettyPrinter { string.push_str(&bytes); self.decrease_indentation(); string.push_str(&self.indentation()); - string.push_str(")"); + string.push(')'); string } else { format!("{identifier}({bytes})") diff --git a/duper/src/serializer.rs b/duper/src/serializer.rs index 5767437..a8d92d2 100644 --- a/duper/src/serializer.rs +++ b/duper/src/serializer.rs @@ -11,6 +11,12 @@ use crate::{ pub struct Serializer; +impl Default for Serializer { + fn default() -> Self { + Self::new() + } +} + impl Serializer { pub fn new() -> Self { Self diff --git a/serde_duper/src/de.rs b/serde_duper/src/de.rs index 6e5a59d..6028dfd 100644 --- a/serde_duper/src/de.rs +++ b/serde_duper/src/de.rs @@ -10,7 +10,7 @@ pub struct Deserializer<'de> { } impl<'de> Deserializer<'de> { - pub fn from_str(input: &'de str) -> Result { + pub fn from_string(input: &'de str) -> Result { let value = DuperParser::parse_duper(input)?; Ok(Self { value: Some(value) }) } @@ -20,11 +20,11 @@ impl<'de> Deserializer<'de> { } } -pub fn from_str<'a, T>(input: &'a str) -> Result +pub fn from_string<'a, T>(input: &'a str) -> Result where T: Deserialize<'a>, { - let mut deserializer = Deserializer::from_str(input)?; + let mut deserializer = Deserializer::from_string(input)?; let t = T::deserialize(&mut deserializer)?; Ok(t) } @@ -38,7 +38,7 @@ where Ok(t) } -impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { +impl<'de> de::Deserializer<'de> for &mut Deserializer<'de> { type Error = de::value::Error; // --- Deserialize DuperValue --- diff --git a/serde_duper/src/error.rs b/serde_duper/src/error.rs index f567efb..98b4290 100644 --- a/serde_duper/src/error.rs +++ b/serde_duper/src/error.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Display}; #[derive(Debug, Clone)] pub enum ErrorKind { - ParseError(pest::error::Error), + ParseError(Box>), SerializationError, DeserializationError(serde_core::de::value::Error), InvalidValue, @@ -78,8 +78,8 @@ impl From for Error { } } -impl From> for Error { - fn from(value: pest::error::Error) -> Self { +impl From>> for Error { + fn from(value: Box>) -> Self { let message = value.variant.message().into_owned(); Self::new(ErrorKind::ParseError(value), message) } diff --git a/serde_duper/src/lib.rs b/serde_duper/src/lib.rs index 52c445a..9dc7a70 100644 --- a/serde_duper/src/lib.rs +++ b/serde_duper/src/lib.rs @@ -2,7 +2,7 @@ mod de; mod error; mod ser; -pub use de::{Deserializer, from_str, from_value}; +pub use de::{Deserializer, from_string, from_value}; pub use duper::{DuperInner, DuperValue}; pub use error::{Error, ErrorImpl, ErrorKind, Result}; pub use ser::{Serializer, to_duper, to_string, to_string_pretty}; diff --git a/serde_duper/src/ser.rs b/serde_duper/src/ser.rs index a14f926..1d2f323 100644 --- a/serde_duper/src/ser.rs +++ b/serde_duper/src/ser.rs @@ -277,7 +277,7 @@ impl<'a, 'b> ser::Serializer for &'a mut Serializer<'b> { fn serialize_seq(self, len: Option) -> Result { Ok(Self::SerializeSeq { serializer: self, - elements: len.map(|len| Vec::with_capacity(len)).unwrap_or(Vec::new()), + elements: len.map(|len| Vec::with_capacity(len)).unwrap_or_default(), }) } @@ -318,7 +318,7 @@ impl<'a, 'b> ser::Serializer for &'a mut Serializer<'b> { fn serialize_map(self, len: Option) -> Result { Ok(Self::SerializeMap { serializer: self, - entries: len.map(|len| Vec::with_capacity(len)).unwrap_or(Vec::new()), + entries: len.map(|len| Vec::with_capacity(len)).unwrap_or_default(), next_key: None, }) } diff --git a/serde_duper/tests/struct.rs b/serde_duper/tests/struct.rs index 37b2fb7..2609658 100644 --- a/serde_duper/tests/struct.rs +++ b/serde_duper/tests/struct.rs @@ -12,7 +12,7 @@ struct Test { #[test] fn serialize() { - let value: Test = serde_duper::from_str( + let value: Test = serde_duper::from_string( r##" { int: 42, -- 2.51.2