Skip to content

Commit

Permalink
chore: update fmt ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Aug 12, 2024
1 parent fd898f7 commit d18f89f
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 132 deletions.
10 changes: 5 additions & 5 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
3 changes: 1 addition & 2 deletions rust/derive/src/derive_dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ struct DeriveDumb<'a>(&'a StrictDerive);

impl StrictDerive {
pub fn derive_dumb(&self) -> Result<TokenStream2> {
self.data
.derive(&self.conf.strict_crate, &ident!(StrictDumb), &DeriveDumb(self))
self.data.derive(&self.conf.strict_crate, &ident!(StrictDumb), &DeriveDumb(self))
}
}

Expand Down
9 changes: 2 additions & 7 deletions rust/derive/src/derive_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ struct DeriveEncode<'a>(&'a StrictDerive);

impl StrictDerive {
pub fn derive_encode(&self) -> Result<TokenStream2> {
self.data
.derive(&self.conf.strict_crate, &ident!(StrictEncode), &DeriveEncode(self))
self.data.derive(&self.conf.strict_crate, &ident!(StrictEncode), &DeriveEncode(self))
}
}

Expand Down Expand Up @@ -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! {
Expand Down
22 changes: 7 additions & 15 deletions rust/derive/src/derive_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down Expand Up @@ -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
Expand All @@ -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(),
};
Expand Down
4 changes: 1 addition & 3 deletions rust/derive/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ impl TryFrom<ParametrizedAttr> 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(),
Expand Down
7 changes: 1 addition & 6 deletions rust/derive/tests/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 5 additions & 16 deletions rust/src/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,15 @@ macro_rules! encode_nonzero {
fn strict_encode<W: TypedWrite>(&self, mut writer: W) -> io::Result<W> {
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<Self, DecodeError> {
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)
}
Expand Down Expand Up @@ -249,10 +244,7 @@ impl<T> StrictUnion for Option<T> where T: StrictType {}
impl<T: StrictEncode + StrictDumb> StrictEncode for Option<T> {
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
writer.write_union::<Self>(|u| {
let u = u
.define_unit(vname!("none"))
.define_newtype::<T>(vname!("some"))
.complete();
let u = u.define_unit(vname!("none")).define_newtype::<T>(vname!("some")).complete();

Ok(match self {
None => u.write_unit(vname!("none")),
Expand Down Expand Up @@ -326,10 +318,7 @@ impl<A: StrictEncode + Default, B: StrictEncode + Default, C: StrictEncode + Def
{
fn strict_encode<W: TypedWrite>(&self, writer: W) -> io::Result<W> {
writer.write_tuple::<Self>(|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())
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions rust/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>())
.field(&self.as_str())
.finish()
f.debug_tuple(&$crate::type_name::<Self>()).field(&self.as_str()).finish()
}
}

Expand Down
14 changes: 3 additions & 11 deletions rust/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,8 @@ impl<C1: RestrictedCharSet, C: RestrictedCharSet, const MIN: usize, const MAX: u
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let c = type_name::<C>();
let c1 = type_name::<C>();
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()
}
}

Expand Down Expand Up @@ -365,9 +359,7 @@ impl TryFrom<u8> for AsciiSym {
type Error = VariantError<u8>;

fn try_from(value: u8) -> Result<Self, Self::Error> {
AsciiChar::from_ascii(value)
.map_err(|_| VariantError::with::<AsciiSym>(value))
.map(Self)
AsciiChar::from_ascii(value).map_err(|_| VariantError::with::<AsciiSym>(value)).map(Self)
}
}
impl StrictType for AsciiSym {
Expand Down
4 changes: 1 addition & 3 deletions rust/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub fn encode<T: StrictEncode + Debug + Eq>(val: &T) -> Vec<u8> {

let ast_data = StrictWriter::in_memory::<MAX>();
let data = val.strict_encode(ast_data).unwrap().unbox().unconfine();
Confined::<Vec<u8>, 0, MAX>::try_from(data)
.unwrap()
.release()
Confined::<Vec<u8>, 0, MAX>::try_from(data).unwrap().release()
}

pub fn decode<T: StrictDecode + Debug + Eq>(data: impl AsRef<[u8]>) -> T {
Expand Down
7 changes: 2 additions & 5 deletions rust/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ pub trait TypedWrite: Sized {
mut self,
bytes: impl AsRef<[u8]>,
) -> io::Result<Self> {
self.raw_writer()
.write_raw_len::<MAX_LEN>(bytes.as_ref().len())?;
self.raw_writer().write_raw_len::<MAX_LEN>(bytes.as_ref().len())?;
self.raw_writer().write_raw::<MAX_LEN>(bytes)?;
Ok(self)
}
Expand Down Expand Up @@ -226,9 +225,7 @@ pub trait TypedRead {
#[doc(hidden)]
unsafe fn read_string<const MAX_LEN: usize>(&mut self) -> Result<Vec<u8>, DecodeError> {
let len = self.raw_reader().read_raw_len::<MAX_LEN>()?;
self.raw_reader()
.read_raw::<MAX_LEN>(len)
.map_err(DecodeError::from)
self.raw_reader().read_raw::<MAX_LEN>(len).map_err(DecodeError::from)
}
}

Expand Down
4 changes: 1 addition & 3 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ use crate::{LibName, TypeName, VariantName};

pub fn type_name<T>() -> 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::<T>().replace('&', "");
Expand Down
37 changes: 7 additions & 30 deletions rust/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,7 @@ impl<W: WriteRaw, P: StrictParent<W>> StructWriter<W, P> {

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("<unnamed>")
}
pub fn name(&self) -> &str { self.name.as_ref().map(|n| n.as_str()).unwrap_or("<unnamed>") }

pub fn into_parent(self) -> P { self.parent }

Expand Down Expand Up @@ -409,12 +404,7 @@ impl<W: WriteRaw> UnionWriter<W> {

pub fn variants(&self) -> &BTreeMap<Variant, VariantType> { &self.defined_variant }

pub fn name(&self) -> &str {
self.name
.as_ref()
.map(|n| n.as_str())
.unwrap_or("<unnamed>")
}
pub fn name(&self) -> &str { self.name.as_ref().map(|n| n.as_str()).unwrap_or("<unnamed>") }

pub fn tag_by_name(&self, name: &VariantName) -> u8 {
*self
Expand All @@ -427,9 +417,7 @@ impl<W: WriteRaw> UnionWriter<W> {
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()
Expand All @@ -438,11 +426,8 @@ impl<W: WriteRaw> UnionWriter<W> {
}

fn _write_variant(mut self, name: VariantName, variant_type: VariantType) -> io::Result<Self> {
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!(
Expand All @@ -461,16 +446,8 @@ impl<W: WriteRaw> UnionWriter<W> {
}

fn _complete_definition(self) -> Self {
let declared = self
.declared_variants
.values()
.map(|v| v.as_str())
.collect::<BTreeSet<_>>();
let defined = self
.defined_variant
.keys()
.map(|v| v.name.as_str())
.collect::<BTreeSet<_>>();
let declared = self.declared_variants.values().map(|v| v.as_str()).collect::<BTreeSet<_>>();
let defined = self.defined_variant.keys().map(|v| v.name.as_str()).collect::<BTreeSet<_>>();
assert_eq!(
declared,
defined,
Expand Down
Loading

0 comments on commit d18f89f

Please sign in to comment.