From d18f89f3d040e367609586542f9d87cf70208e20 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 12 Aug 2024 21:33:55 +0200 Subject: [PATCH] chore: update fmt ruleset --- .rustfmt.toml | 10 ++++----- rust/derive/src/derive_dumb.rs | 3 +-- rust/derive/src/derive_encode.rs | 9 ++------ rust/derive/src/derive_type.rs | 22 ++++++------------- rust/derive/src/params.rs | 4 +--- rust/derive/tests/type.rs | 7 +----- rust/src/embedded.rs | 21 +++++------------- rust/src/ident.rs | 4 +--- rust/src/stl.rs | 14 +++--------- rust/src/test.rs | 4 +--- rust/src/traits.rs | 7 ++---- rust/src/types.rs | 4 +--- rust/src/writer.rs | 37 ++++++-------------------------- rust/test_helpers/src/lib.rs | 37 ++++++++++++-------------------- 14 files changed, 51 insertions(+), 132 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index 952a284..98e9b85 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -4,12 +4,15 @@ version = "Two" max_width = 100 array_width = 100 attr_fn_like_width = 100 +comment_width = 100 +chain_width = 100 fn_call_width = 100 +single_line_if_else_max_width = 100 -format_code_in_doc_comments = true fn_single_line = true +format_code_in_doc_comments = true format_macro_matchers = true -format_macro_bodues = true +format_macro_bodies = true format_strings = true merge_derives = false overflow_delimited_expr = true @@ -19,9 +22,6 @@ use_try_shorthand = true wrap_comments = true where_single_line = true unstable_features = true -empty_item_single_line = true - -binop_separator = "Back" imports_granularity = "Module" group_imports = "StdExternalCrate" diff --git a/rust/derive/src/derive_dumb.rs b/rust/derive/src/derive_dumb.rs index 4d78b18..2d22882 100644 --- a/rust/derive/src/derive_dumb.rs +++ b/rust/derive/src/derive_dumb.rs @@ -29,8 +29,7 @@ struct DeriveDumb<'a>(&'a StrictDerive); impl StrictDerive { pub fn derive_dumb(&self) -> Result { - self.data - .derive(&self.conf.strict_crate, &ident!(StrictDumb), &DeriveDumb(self)) + self.data.derive(&self.conf.strict_crate, &ident!(StrictDumb), &DeriveDumb(self)) } } diff --git a/rust/derive/src/derive_encode.rs b/rust/derive/src/derive_encode.rs index bb1e803..a00f361 100644 --- a/rust/derive/src/derive_encode.rs +++ b/rust/derive/src/derive_encode.rs @@ -29,8 +29,7 @@ struct DeriveEncode<'a>(&'a StrictDerive); impl StrictDerive { pub fn derive_encode(&self) -> Result { - self.data - .derive(&self.conf.strict_crate, &ident!(StrictEncode), &DeriveEncode(self)) + self.data.derive(&self.conf.strict_crate, &ident!(StrictEncode), &DeriveEncode(self)) } } @@ -73,11 +72,7 @@ impl DeriveInner for DeriveEncode<'_> { let no = fields.iter().enumerate().filter_map(|(index, field)| { let attr = FieldAttr::with(field.attr.clone(), FieldKind::Unnamed).ok()?; - if attr.skip { - None - } else { - Some(Index::from(index)) - } + if attr.skip { None } else { Some(Index::from(index)) } }); Ok(quote! { diff --git a/rust/derive/src/derive_type.rs b/rust/derive/src/derive_type.rs index 8ddd98e..da5b7ae 100644 --- a/rust/derive/src/derive_type.rs +++ b/rust/derive/src/derive_type.rs @@ -41,14 +41,11 @@ impl StrictDerive { let trait_crate = &self.conf.strict_crate; let type_name = &self.data.name; - let impl_type = self - .data - .derive(trait_crate, &ident!(StrictType), &DeriveType(self))?; + let impl_type = self.data.derive(trait_crate, &ident!(StrictType), &DeriveType(self))?; let impl_outer = match &self.data.inner { DataInner::Struct(_) => { - self.data - .derive(trait_crate, &ident!(StrictProduct), &DeriveProduct)? + self.data.derive(trait_crate, &ident!(StrictProduct), &DeriveProduct)? } DataInner::Enum(variants) => { let enum_attr = EnumAttr::with(self.data.attr.clone(), variants.enum_kind())?; @@ -87,8 +84,7 @@ impl StrictDerive { }; let impl_struct_enum = - self.data - .derive(trait_crate, &ident!(StrictSum), &DeriveSum(enum_attr))?; + self.data.derive(trait_crate, &ident!(StrictSum), &DeriveSum(enum_attr))?; quote! { #impl_into_u8 @@ -101,20 +97,16 @@ impl StrictDerive { let impl_inner = match &self.data.inner { DataInner::Struct(Fields::Named(_)) => { - self.data - .derive(trait_crate, &ident!(StrictStruct), &DeriveStruct)? + self.data.derive(trait_crate, &ident!(StrictStruct), &DeriveStruct)? } DataInner::Struct(Fields::Unnamed(_)) => { - self.data - .derive(trait_crate, &ident!(StrictTuple), &DeriveTuple)? + self.data.derive(trait_crate, &ident!(StrictTuple), &DeriveTuple)? } DataInner::Enum(variants) if variants.enum_kind() == EnumKind::Primitive => { - self.data - .derive(trait_crate, &ident!(StrictEnum), &DeriveEnum)? + self.data.derive(trait_crate, &ident!(StrictEnum), &DeriveEnum)? } DataInner::Enum(_) => { - self.data - .derive(trait_crate, &ident!(StrictUnion), &DeriveUnion)? + self.data.derive(trait_crate, &ident!(StrictUnion), &DeriveUnion)? } _ => TokenStream2::new(), }; diff --git a/rust/derive/src/params.rs b/rust/derive/src/params.rs index 08f685a..acf31ac 100644 --- a/rust/derive/src/params.rs +++ b/rust/derive/src/params.rs @@ -117,9 +117,7 @@ impl TryFrom for ContainerAttr { params.check(EnumAttr::attr_req(map, EnumKind::Primitive))?; Ok(ContainerAttr { - strict_crate: params - .arg_value(ATTR_CRATE) - .unwrap_or_else(|_| path!(strict_encoding)), + strict_crate: params.arg_value(ATTR_CRATE).unwrap_or_else(|_| path!(strict_encoding)), lib: params.unwrap_arg_value(ATTR_LIB), rename: params.arg_value(ATTR_RENAME).ok(), dumb: params.arg_value(ATTR_DUMB).ok(), diff --git a/rust/derive/tests/type.rs b/rust/derive/tests/type.rs index ce13447..d904b5c 100644 --- a/rust/derive/tests/type.rs +++ b/rust/derive/tests/type.rs @@ -135,12 +135,7 @@ fn skip_field() -> common::Result { must_camelize: 2, wrong_name: 3, }; - assert_eq!( - val.to_strict_serialized::<{ usize::MAX }>() - .unwrap() - .as_slice(), - &[2] - ); + assert_eq!(val.to_strict_serialized::<{ usize::MAX }>().unwrap().as_slice(), &[2]); let val = Struct { must_camelize: 2, wrong_name: 0, diff --git a/rust/src/embedded.rs b/rust/src/embedded.rs index 4a8f509..65647fd 100644 --- a/rust/src/embedded.rs +++ b/rust/src/embedded.rs @@ -101,20 +101,15 @@ macro_rules! encode_nonzero { fn strict_encode(&self, mut writer: W) -> io::Result { unsafe { writer = writer.register_primitive(Primitive::$id); - writer - .raw_writer() - .write_raw_array(self.get().to_le_bytes())?; + writer.raw_writer().write_raw_array(self.get().to_le_bytes())?; } Ok(writer) } } impl $crate::StrictDecode for $ty { fn strict_decode(reader: &mut impl TypedRead) -> Result { - let buf = unsafe { - reader - .raw_reader() - .read_raw_array::<{ Self::BITS as usize / 8 }>()? - }; + let buf = + unsafe { reader.raw_reader().read_raw_array::<{ Self::BITS as usize / 8 }>()? }; let v = <$p>::from_le_bytes(buf); Self::new(v).ok_or(DecodeError::ZeroNatural) } @@ -249,10 +244,7 @@ impl StrictUnion for Option where T: StrictType {} impl StrictEncode for Option { fn strict_encode(&self, writer: W) -> io::Result { writer.write_union::(|u| { - let u = u - .define_unit(vname!("none")) - .define_newtype::(vname!("some")) - .complete(); + let u = u.define_unit(vname!("none")).define_newtype::(vname!("some")).complete(); Ok(match self { None => u.write_unit(vname!("none")), @@ -326,10 +318,7 @@ impl(&self, writer: W) -> io::Result { writer.write_tuple::(|w| { - Ok(w.write_field(&self.0)? - .write_field(&self.1)? - .write_field(&self.2)? - .complete()) + Ok(w.write_field(&self.0)?.write_field(&self.1)?.write_field(&self.2)?.complete()) }) } } diff --git a/rust/src/ident.rs b/rust/src/ident.rs index f974d81..7c41a23 100644 --- a/rust/src/ident.rs +++ b/rust/src/ident.rs @@ -50,9 +50,7 @@ macro_rules! impl_ident_type { impl ::core::fmt::Debug for $ty { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - f.debug_tuple(&$crate::type_name::()) - .field(&self.as_str()) - .finish() + f.debug_tuple(&$crate::type_name::()).field(&self.as_str()).finish() } } diff --git a/rust/src/stl.rs b/rust/src/stl.rs index 6edd335..53dd6b6 100644 --- a/rust/src/stl.rs +++ b/rust/src/stl.rs @@ -207,14 +207,8 @@ impl) -> std::fmt::Result { let c = type_name::(); let c1 = type_name::(); - let c = if c == c1 { - c.to_owned() - } else { - format!("{c1}, {c}") - }; - f.debug_tuple(&format!("RString<{c}[{MIN}..{MAX}]>")) - .field(&self.as_str()) - .finish() + let c = if c == c1 { c.to_owned() } else { format!("{c1}, {c}") }; + f.debug_tuple(&format!("RString<{c}[{MIN}..{MAX}]>")).field(&self.as_str()).finish() } } @@ -365,9 +359,7 @@ impl TryFrom for AsciiSym { type Error = VariantError; fn try_from(value: u8) -> Result { - AsciiChar::from_ascii(value) - .map_err(|_| VariantError::with::(value)) - .map(Self) + AsciiChar::from_ascii(value).map_err(|_| VariantError::with::(value)).map(Self) } } impl StrictType for AsciiSym { diff --git a/rust/src/test.rs b/rust/src/test.rs index 8e18df4..c863f3b 100644 --- a/rust/src/test.rs +++ b/rust/src/test.rs @@ -31,9 +31,7 @@ pub fn encode(val: &T) -> Vec { let ast_data = StrictWriter::in_memory::(); let data = val.strict_encode(ast_data).unwrap().unbox().unconfine(); - Confined::, 0, MAX>::try_from(data) - .unwrap() - .release() + Confined::, 0, MAX>::try_from(data).unwrap().release() } pub fn decode(data: impl AsRef<[u8]>) -> T { diff --git a/rust/src/traits.rs b/rust/src/traits.rs index d53d8b2..7347572 100644 --- a/rust/src/traits.rs +++ b/rust/src/traits.rs @@ -130,8 +130,7 @@ pub trait TypedWrite: Sized { mut self, bytes: impl AsRef<[u8]>, ) -> io::Result { - self.raw_writer() - .write_raw_len::(bytes.as_ref().len())?; + self.raw_writer().write_raw_len::(bytes.as_ref().len())?; self.raw_writer().write_raw::(bytes)?; Ok(self) } @@ -226,9 +225,7 @@ pub trait TypedRead { #[doc(hidden)] unsafe fn read_string(&mut self) -> Result, DecodeError> { let len = self.raw_reader().read_raw_len::()?; - self.raw_reader() - .read_raw::(len) - .map_err(DecodeError::from) + self.raw_reader().read_raw::(len).map_err(DecodeError::from) } } diff --git a/rust/src/types.rs b/rust/src/types.rs index da15d18..2c403a9 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -28,9 +28,7 @@ use crate::{LibName, TypeName, VariantName}; pub fn type_name() -> String { fn get_ident(path: &str) -> &str { - path.rsplit_once("::") - .map(|(_, n)| n.trim()) - .unwrap_or(path) + path.rsplit_once("::").map(|(_, n)| n.trim()).unwrap_or(path) } let name = any::type_name::().replace('&', ""); diff --git a/rust/src/writer.rs b/rust/src/writer.rs index 3e4b701..ded98fb 100644 --- a/rust/src/writer.rs +++ b/rust/src/writer.rs @@ -228,12 +228,7 @@ impl> StructWriter { pub fn fields_count(&self) -> u8 { self.tuple_fields.unwrap_or(self.named_fields.len() as u8) } - pub fn name(&self) -> &str { - self.name - .as_ref() - .map(|n| n.as_str()) - .unwrap_or("") - } + pub fn name(&self) -> &str { self.name.as_ref().map(|n| n.as_str()).unwrap_or("") } pub fn into_parent(self) -> P { self.parent } @@ -409,12 +404,7 @@ impl UnionWriter { pub fn variants(&self) -> &BTreeMap { &self.defined_variant } - pub fn name(&self) -> &str { - self.name - .as_ref() - .map(|n| n.as_str()) - .unwrap_or("") - } + pub fn name(&self) -> &str { self.name.as_ref().map(|n| n.as_str()).unwrap_or("") } pub fn tag_by_name(&self, name: &VariantName) -> u8 { *self @@ -427,9 +417,7 @@ impl UnionWriter { let tag = self.tag_by_name(&name); let variant = Variant::named(tag, name); assert!( - self.defined_variant - .insert(variant.clone(), variant_type) - .is_none(), + self.defined_variant.insert(variant.clone(), variant_type).is_none(), "variant '{:#}' is already defined as a part of '{}'", &variant, self.name() @@ -438,11 +426,8 @@ impl UnionWriter { } fn _write_variant(mut self, name: VariantName, variant_type: VariantType) -> io::Result { - let (variant, t) = self - .defined_variant - .iter() - .find(|(f, _)| f.name == name) - .unwrap_or_else(|| { + let (variant, t) = + self.defined_variant.iter().find(|(f, _)| f.name == name).unwrap_or_else(|| { panic!("variant '{:#}' was not defined in '{}'", &name, self.name()) }); assert_eq!( @@ -461,16 +446,8 @@ impl UnionWriter { } fn _complete_definition(self) -> Self { - let declared = self - .declared_variants - .values() - .map(|v| v.as_str()) - .collect::>(); - let defined = self - .defined_variant - .keys() - .map(|v| v.name.as_str()) - .collect::>(); + let declared = self.declared_variants.values().map(|v| v.as_str()).collect::>(); + let defined = self.defined_variant.keys().map(|v| v.name.as_str()).collect::>(); assert_eq!( declared, defined, diff --git a/rust/test_helpers/src/lib.rs b/rust/test_helpers/src/lib.rs index f4f033e..1e24d32 100644 --- a/rust/test_helpers/src/lib.rs +++ b/rust/test_helpers/src/lib.rs @@ -226,8 +226,7 @@ where T: Clone + PartialEq + Debug /// # Covered test case /// /// - Strict encoding must match little-endian encoding of the value -/// - Roundtrip encoding-decoding of the enum variant must result in the -/// original value +/// - Roundtrip encoding-decoding of the enum variant must result in the original value /// /// # Example /// @@ -305,12 +304,10 @@ macro_rules! test_encoding_enum { /// # Covered test cases /// /// - Each enum variant must have a primitive value -/// - Primitive value representing enum variant must be equal to strict encoding -/// of the same variant. If a primitive enum value occupies of several bytes -/// (`u16`, `u32` and other large integer types), strict encoding must match -/// little-endian encoding of the value -/// - Roundtrip encoding-decoding of the enum variant must result in the -/// original value +/// - Primitive value representing enum variant must be equal to strict encoding of the same +/// variant. If a primitive enum value occupies of several bytes (`u16`, `u32` and other large +/// integer types), strict encoding must match little-endian encoding of the value +/// - Roundtrip encoding-decoding of the enum variant must result in the original value /// - Each enum variant must be equal to itself /// - Each enum variant must not be equal to any other enum variant /// - Enum variants must be ordered according to their primitive values @@ -391,18 +388,15 @@ macro_rules! test_encoding_enum_by_values { /// # Covered test cases /// /// - Each enum variant must have a primitive value -/// - Primitive value representing enum variant must be equal to strict encoding -/// of the same variant. If a primitive enum value occupies of several bytes -/// (`u16`, `u32` and other large integer types), strict encoding must match -/// little-endian encoding of the value -/// - Roundtrip encoding-decoding of the enum variant must result in the -/// original value +/// - Primitive value representing enum variant must be equal to strict encoding of the same +/// variant. If a primitive enum value occupies of several bytes (`u16`, `u32` and other large +/// integer types), strict encoding must match little-endian encoding of the value +/// - Roundtrip encoding-decoding of the enum variant must result in the original value /// - Each enum variant must be equal to itself /// - Each enum variant must not be equal to any other enum variant /// - Enum variants must be ordered according to their primitive values -/// - All 8-bit integers which do not match any of enum variants must not be -/// decoded with strict decoder into a valid enum and their decoding must -/// result in an error. +/// - All 8-bit integers which do not match any of enum variants must not be decoded with strict +/// decoder into a valid enum and their decoding must result in an error. /// /// # Example /// @@ -526,8 +520,7 @@ where T: StrictEncode + StrictDecode + PartialEq + Debug + Clone /// Errors on: /// - encoding or decoding failures; /// - if the original object is not equivalent to its decoded version; -/// - if encoder returns number of bytes that does not match the length of the -/// encoded data. +/// - if encoder returns number of bytes that does not match the length of the encoded data. /// /// # Panics /// @@ -579,8 +572,7 @@ where T: StrictEncode + StrictDecode + PartialEq + Clone + Debug { /// Errors on: /// - encoding or decoding failures; /// - if the original test vector is not equivalent to its transcoded version; -/// - if encoder returns number of bytes that does not match the length of the -/// test vector. +/// - if encoder returns number of bytes that does not match the length of the test vector. /// /// # Panics /// @@ -629,8 +621,7 @@ where T: StrictEncode + StrictDecode + PartialEq + Clone + Debug { /// - encoding or decoding failures; /// - if the original object is not equivalent to its decoded version; /// - if the original test vector is not equivalent to its transcoded version; -/// - if encoder returns number of bytes that does not match the length of the -/// test vector. +/// - if encoder returns number of bytes that does not match the length of the test vector. /// /// # Panics ///