Skip to content

chore: clippy pendantic fixes #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions macros/macros_impl/src/asn_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use itertools::Itertools;

use crate::config::*;
use crate::config::{Config, FieldConfig};

#[allow(clippy::too_many_lines)]
pub fn derive_struct_impl(
name: &syn::Ident,
generics: syn::Generics,
Expand Down Expand Up @@ -44,10 +45,9 @@ pub fn derive_struct_impl(
.filter_map(|(key, fields)| key.then_some(fields))
.map(|fields| {
let error_message = format!(
"{}'s fields is not a valid \
"{name}'s fields is not a valid \
order of ASN.1 tags, ensure that your field's tags and \
OPTIONALs are correct.",
name
OPTIONALs are correct."
);

let tag_tree = fields.map(|f| f.tag_tree()).collect::<Vec<_>>();
Expand Down
12 changes: 3 additions & 9 deletions macros/macros_impl/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use quote::ToTokens;
use syn::Fields;

use crate::config::*;
use crate::config::{map_to_inner_type, Config, FieldConfig};

#[allow(clippy::too_many_lines)]
pub fn derive_struct_impl(
Expand Down Expand Up @@ -40,12 +40,7 @@ pub fn derive_struct_impl(
quote!(Self(<#ty>::decode(decoder)?, #(#phantom_data),*))
};

if config
.tag
.as_ref()
.map(|tag| tag.is_explicit())
.unwrap_or_default()
{
if config.tag.as_ref().is_some_and(|tag| tag.is_explicit()) {
quote! {
decoder.decode_explicit_prefix::<#ty>(tag).map(#map_quote)
}
Expand Down Expand Up @@ -312,8 +307,7 @@ pub fn map_from_inner_type(
let name = field
.ident
.as_ref()
.map(|ident| quote!(#ident))
.unwrap_or_else(|| quote!(#i));
.map_or_else(|| quote!(#i), |ident| quote!(#ident));
quote!(#name : inner.#name)
});

Expand Down
38 changes: 21 additions & 17 deletions macros/macros_impl/src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use syn::LitStr;

use crate::config::*;
use crate::config::{Config, FieldConfig};

pub fn derive_struct_impl(
name: &syn::Ident,
Expand Down Expand Up @@ -85,7 +85,11 @@ pub fn derive_struct_impl(
}, identifier).map(drop)
};

if config.tag.as_ref().is_some_and(|tag| tag.is_explicit()) {
if config
.tag
.as_ref()
.is_some_and(super::tag::Tag::is_explicit)
{
map_to_inner_type(
config.tag.as_ref().unwrap(),
name,
Expand Down Expand Up @@ -152,6 +156,7 @@ pub fn map_to_inner_type(
quote!(#name : &#name_prefixed)
});

#[allow(clippy::items_after_statements)]
fn wrap(
fields: impl Iterator<Item = proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
Expand All @@ -171,6 +176,7 @@ pub fn map_to_inner_type(
quote!(&self.#i)
});

#[allow(clippy::items_after_statements)]
fn wrap(
fields: impl Iterator<Item = proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
Expand Down Expand Up @@ -201,26 +207,24 @@ pub fn map_to_inner_type(

fn fields_as_vars(fields: &syn::Fields) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
fields.iter().enumerate().map(|(i, field)| {
let self_name = field
.ident
.as_ref()
.map(|ident| quote!(#ident))
.unwrap_or_else(|| {
let self_name = field.ident.as_ref().map_or_else(
|| {
let i = syn::Index::from(i);
quote!(#i)
});
},
|ident| quote!(#ident),
);

let name = field
.ident
.as_ref()
.map(|ident| {
let prefixed = format_ident!("__rasn_field_{}", ident);
quote!(#prefixed)
})
.unwrap_or_else(|| {
let name = field.ident.as_ref().map_or_else(
|| {
let ident = format_ident!("i{}", i);
quote!(#ident)
});
},
|ident| {
let prefixed = format_ident!("__rasn_field_{}", ident);
quote!(#prefixed)
},
);

quote!(#[allow(unused)] let #name = &self.#self_name;)
})
Expand Down
6 changes: 3 additions & 3 deletions macros/macros_impl/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::Span;
use quote::ToTokens;
use syn::LitStr;

use crate::config::*;
use crate::config::{Config, Constraint, Constraints, Value, VariantConfig};

pub struct Enum<'a> {
pub name: &'a syn::Ident,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Enum<'_> {
let field_tags = if self.config.choice {
variant_configs
.iter()
.map(|config| config.tag_tree())
.map(super::config::VariantConfig::tag_tree)
.collect::<Result<Vec<_>, _>>()?
} else {
Vec::new()
Expand Down Expand Up @@ -511,7 +511,7 @@ impl Enum<'_> {
.iter()
.cloned()
.map(|mut variant| {
for field in variant.fields.iter_mut() {
for field in &mut variant.fields {
field.ty = syn::Type::Reference(syn::TypeReference {
and_token: <_>::default(),
lifetime: encode_lifetime.clone().into(),
Expand Down
2 changes: 1 addition & 1 deletion macros/macros_impl/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl GenericsExt for syn::Generics {
fn add_trait_bounds(&mut self, crate_root: &syn::Path, ident: syn::Ident) {
for param in self.type_params_mut() {
if param.colon_token.is_none() {
param.colon_token = Some(Default::default());
param.colon_token = Some(syn::token::Colon::default());
}
param.bounds.push(
syn::TraitBound {
Expand Down
19 changes: 9 additions & 10 deletions macros/macros_impl/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl Class {
return Err(syn::Error::new(
ident.span(),
format!(
"Class MUST BE `universal`, `application`, `context`, or `private`. Found: {}",
s
"Class MUST BE `universal`, `application`, `context`, or `private`. Found: {s}"
),
))
}
Expand All @@ -42,7 +41,7 @@ impl Class {
}

impl Class {
pub fn as_tokens(&self, crate_root: &syn::Path) -> proc_macro2::TokenStream {
pub fn as_tokens(self, crate_root: &syn::Path) -> proc_macro2::TokenStream {
match self {
Self::Universal => quote!(#crate_root::types::Class::Universal),
Self::Application => quote!(#crate_root::types::Class::Application),
Expand Down Expand Up @@ -71,7 +70,7 @@ impl Tag {
let content;
parenthesized!(content in meta.input);
if content.peek(syn::Lit) {
tag = (Class::Context, content.parse::<syn::Lit>()?)
tag = (Class::Context, content.parse::<syn::Lit>()?);
} else if content.peek(syn::Ident) {
let ident: syn::Ident = content.parse()?;
if content.peek(syn::token::Paren) {
Expand Down Expand Up @@ -138,15 +137,15 @@ impl Tag {
},
syn::Fields::Named(_) => Self::SEQUENCE(),
syn::Fields::Unnamed(_) => {
if fields.iter().count() != 1 {
if fields.iter().count() == 1 {
let ty = fields.iter().next().cloned().unwrap().ty;

Self::Delegate { ty }
} else {
return Err(syn::Error::new(
fields.span(),
"Unnamed fields are not supported.",
));
} else {
let ty = fields.iter().next().cloned().unwrap().ty;

Self::Delegate { ty }
}
}
})
Expand All @@ -155,7 +154,7 @@ impl Tag {
pub fn is_explicit(&self) -> bool {
match self {
Self::Value { explicit, .. } => *explicit,
_ => false,
Self::Delegate { .. } => false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use syn::{parse_macro_input, DeriveInput};

/// Helper function print out the derive.
fn __print_stream(stream: proc_macro2::TokenStream) -> proc_macro::TokenStream {
println!("{}", stream);
println!("{stream}");
stream.into()
}

Expand Down
28 changes: 17 additions & 11 deletions src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ mod tests {
}

#[test]
#[allow(clippy::too_many_lines)]
fn integer() {
type B = ConstrainedInteger<5, 99>;

Expand All @@ -164,7 +165,7 @@ mod tests {
type F = ConstrainedInteger<0, 256>;
type G = ConstrainedInteger<0, 65535>;
type H = ConstrainedInteger<0, 65536>;
type I = ConstrainedInteger<0, 10000000000>;
type I = ConstrainedInteger<0, 10_000_000_000>;

#[derive(Debug, AsnType, Decode, Encode, PartialEq)]
#[rasn(crate_root = "crate")]
Expand All @@ -188,9 +189,9 @@ mod tests {

type N = ConstrainedInteger<0, 65535>;
type O = ConstrainedInteger<0, 65536>;
type P = ConstrainedInteger<0, 2147483647>;
type Q = ConstrainedInteger<0, 4294967295>;
type R = ConstrainedInteger<0, 4294967296>;
type P = ConstrainedInteger<0, 2_147_483_647>;
type Q = ConstrainedInteger<0, 4_294_967_295>;
type R = ConstrainedInteger<0, 4_294_967_296>;

#[derive(Debug, AsnType, Decode, Encode, PartialEq)]
#[rasn(crate_root = "crate")]
Expand Down Expand Up @@ -223,7 +224,7 @@ mod tests {
C,
C {
a: true,
b: Integer::from(43554344223i64),
b: Integer::from(43_554_344_223_i64),
c: false,
d: Integer::from(-9)
},
Expand All @@ -241,7 +242,7 @@ mod tests {
round_trip!(
aper,
I,
I::new(10000000000i64),
I::new(10_000_000_000_i64),
&[0x80, 0x02, 0x54, 0x0b, 0xe4, 0x00]
);
round_trip!(
Expand Down Expand Up @@ -279,19 +280,24 @@ mod tests {
round_trip!(aper, P, P::new(256), &[0x40, 0x01, 0x00]);
round_trip!(aper, P, P::new(65535), &[0x40, 0xff, 0xff]);
round_trip!(aper, P, P::new(65536), &[0x80, 0x01, 0x00, 0x00]);
round_trip!(aper, P, P::new(16777215), &[0x80, 0xff, 0xff, 0xff]);
round_trip!(aper, P, P::new(16777216), &[0xc0, 0x01, 0x00, 0x00, 0x00]);
round_trip!(aper, P, P::new(100000000), &[0xc0, 0x05, 0xf5, 0xe1, 0x00]);
round_trip!(aper, P, P::new(16_777_215), &[0x80, 0xff, 0xff, 0xff]);
round_trip!(aper, P, P::new(16_777_216), &[0xc0, 0x01, 0x00, 0x00, 0x00]);
round_trip!(
aper,
P,
P::new(100_000_000),
&[0xc0, 0x05, 0xf5, 0xe1, 0x00]
);
round_trip!(
aper,
Q,
Q::new(4294967295u64),
Q::new(4_294_967_295_u64),
&[0xc0, 0xff, 0xff, 0xff, 0xff]
);
round_trip!(
aper,
R,
R::new(4294967296u64),
R::new(4_294_967_296_u64),
&[0x80, 0x01, 0x00, 0x00, 0x00, 0x00]
);
round_trip!(
Expand Down
10 changes: 5 additions & 5 deletions src/ber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub fn decode_with_remainder<T: crate::Decode>(
input: &[u8],
) -> Result<(T, &[u8]), crate::error::DecodeError> {
let decoder = &mut de::Decoder::new(input, de::DecoderOptions::ber());
let decoded = T::decode(decoder)?;
Ok((decoded, decoder.remaining()))
let decoded_instance = T::decode(decoder)?;
Ok((decoded_instance, decoder.remaining()))
}

/// Attempts to encode `value` to BER.
Expand Down Expand Up @@ -91,10 +91,10 @@ mod tests {
#[test]
fn leading_integer_bytes() {
const DATA: &[u8] = &[0x02, 0x06, 0x00, 0x00, 0x33, 0x44, 0x55, 0x66];
assert_eq!(decode::<u32>(DATA).unwrap(), 0x33445566u32);
assert_eq!(decode::<u32>(DATA).unwrap(), 0x3344_5566_u32);

const SIGNED_DATA: &[u8] = &[0x02, 0x06, 0xFF, 0xFF, 0x83, 0x44, 0x55, 0x66];
assert_eq!(decode::<i32>(SIGNED_DATA).unwrap(), -2092673690);
assert_eq!(decode::<i32>(SIGNED_DATA).unwrap(), -2_092_673_690);
}

#[test]
Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {
let result = crate::ber::decode::<crate::types::GeneralizedTime>(&data);
// if err, print error
if result.is_err() {
println!("{:?}", result);
println!("{result:?}");
}
assert!(result.is_ok());
let data = [
Expand Down
Loading
Loading