From f81211fddf7fe48fe653cb685c8fada115f5e999 Mon Sep 17 00:00:00 2001 From: Erin Power Date: Fri, 13 Sep 2024 12:56:51 +0200 Subject: [PATCH] fix!: Remove Tag and TagTree from module root --- README.md | 2 +- macros/src/asn_type.rs | 6 +-- macros/src/config.rs | 26 ++++++----- macros/src/decode.rs | 6 +-- macros/src/encode.rs | 4 +- macros/src/enum.rs | 24 +++++----- macros/src/lib.rs | 4 +- macros/src/tag.rs | 2 +- src/ber.rs | 2 +- src/error/decode.rs | 4 +- src/error/encode.rs | 5 +- src/jer/de.rs | 70 +++++++++++----------------- src/jer/enc.rs | 68 ++++++++++++--------------- src/lib.rs | 6 +-- src/oer/de.rs | 28 ++++++----- src/oer/enc.rs | 21 ++++----- src/per/enc.rs | 2 +- src/types/date.rs | 2 +- src/types/integer.rs | 3 +- src/types/prefix.rs | 2 +- standards/kerberos/tests/issue111.rs | 6 ++- standards/smi/src/v1.rs | 4 +- standards/snmp/src/v3.rs | 4 +- tests/personnel.rs | 10 ++-- 24 files changed, 145 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index d941d4c8..9c1fcabd 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ When modelling an ASN.1 data type, there are three traits we'll need to implemen ```rust # struct Person; -use rasn::{AsnType, Tag}; +use rasn::{AsnType, types::Tag}; impl AsnType for Person { // Default tag for sequences. diff --git a/macros/src/asn_type.rs b/macros/src/asn_type.rs index dbc67683..e4fc7e1d 100644 --- a/macros/src/asn_type.rs +++ b/macros/src/asn_type.rs @@ -48,8 +48,8 @@ pub fn derive_struct_impl( ); quote!({ - const LIST: &'static [#crate_root::TagTree] = &[#(#tag_tree),*]; - const TAG_TREE: #crate_root::TagTree = #crate_root::TagTree::Choice(LIST); + const LIST: &'static [#crate_root::types::TagTree] = &[#(#tag_tree),*]; + const TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(LIST); const _: () = assert!(TAG_TREE.is_unique(), #error_message); }) }) @@ -104,7 +104,7 @@ pub fn derive_struct_impl( #[automatically_derived] impl #impl_generics #crate_root::AsnType for #name #ty_generics #where_clause { - const TAG: #crate_root::Tag = { + const TAG: #crate_root::types::Tag = { #(#all_optional_tags_are_unique)* #tag diff --git a/macros/src/config.rs b/macros/src/config.rs index ffdc09a9..ff4b8d73 100644 --- a/macros/src/config.rs +++ b/macros/src/config.rs @@ -411,9 +411,11 @@ impl Config { quote!(<#ty as #crate_root::AsnType>::TAG) }) }) - .or_else(|| (fields == &syn::Fields::Unit).then(|| quote!(#crate_root::Tag::NULL))) - .or_else(|| self.set.then(|| quote!(#crate_root::Tag::SET))) - .unwrap_or(quote!(#crate_root::Tag::SEQUENCE)) + .or_else(|| { + (fields == &syn::Fields::Unit).then(|| quote!(#crate_root::types::Tag::NULL)) + }) + .or_else(|| self.set.then(|| quote!(#crate_root::types::Tag::SET))) + .unwrap_or(quote!(#crate_root::types::Tag::SEQUENCE)) } } @@ -633,7 +635,7 @@ impl<'config> VariantConfig<'config> { .tag .as_ref() .map(|t| t.to_tokens(crate_root)) - .unwrap_or(quote!(#crate_root::Tag::SEQUENCE)); + .unwrap_or(quote!(#crate_root::types::Tag::SEQUENCE)); let decode_impl = super::decode::map_from_inner_type( tag, @@ -657,7 +659,7 @@ impl<'config> VariantConfig<'config> { }; quote! { - if #crate_root::TagTree::tag_contains(&tag, &[#tag_tree]) { + if #crate_root::types::TagTree::tag_contains(&tag, &[#tag_tree]) { return #decode_op } } @@ -682,7 +684,7 @@ impl<'config> VariantConfig<'config> { let crate_root = &self.container_config.crate_root; if self.tag.is_some() || self.container_config.automatic_tags { let tag = self.tag(context).to_tokens(crate_root); - quote!(#crate_root::TagTree::Leaf(#tag)) + quote!(#crate_root::types::TagTree::Leaf(#tag)) } else { let field_tags = self .variant @@ -694,7 +696,7 @@ impl<'config> VariantConfig<'config> { match self.variant.fields { syn::Fields::Unit => { - quote!(#crate_root::TagTree::Leaf(<() as #crate_root::AsnType>::TAG)) + quote!(#crate_root::types::TagTree::Leaf(<() as #crate_root::AsnType>::TAG)) } syn::Fields::Named(_) => { let error_message = format!( @@ -705,10 +707,10 @@ impl<'config> VariantConfig<'config> { ); quote!({ - const FIELD_LIST: &'static [#crate_root::TagTree] = &[#(#field_tags),*]; - const FIELD_TAG_TREE: #crate_root::TagTree = #crate_root::TagTree::Choice(FIELD_LIST); + const FIELD_LIST: &'static [#crate_root::types::TagTree] = &[#(#field_tags),*]; + const FIELD_TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(FIELD_LIST); const _: () = assert!(FIELD_TAG_TREE.is_unique(), #error_message); - #crate_root::TagTree::Leaf(#crate_root::Tag::SEQUENCE) + #crate_root::types::TagTree::Leaf(#crate_root::types::Tag::SEQUENCE) }) } syn::Fields::Unnamed(_) => { @@ -1130,7 +1132,7 @@ impl<'a> FieldConfig<'a> { let tag = tag.to_tokens(crate_root); quote!(#tag) } else if self.container_config.automatic_tags { - quote!(#crate_root::Tag::new(#crate_root::types::Class::Context, #context as u32)) + quote!(#crate_root::types::Tag::new(#crate_root::types::Class::Context, #context as u32)) } else { let mut ty = self.field.ty.clone(); ty.strip_lifetimes(); @@ -1144,7 +1146,7 @@ impl<'a> FieldConfig<'a> { if self.tag.is_some() || self.container_config.automatic_tags { let tag = self.tag(context); - quote!(#crate_root::TagTree::Leaf(#tag)) + quote!(#crate_root::types::TagTree::Leaf(#tag)) } else { self.container_config.tag_tree_for_ty(ty) } diff --git a/macros/src/decode.rs b/macros/src/decode.rs index a9c3a00b..87c5a6d4 100644 --- a/macros/src/decode.rs +++ b/macros/src/decode.rs @@ -29,7 +29,7 @@ pub fn derive_struct_impl( } else { quote! { match tag { - #crate_root::Tag::EOC => { + #crate_root::types::Tag::EOC => { Ok(Self(<#ty>::decode(decoder)?)) } _ => { @@ -132,7 +132,7 @@ pub fn derive_struct_impl( }; ( - quote!(const #const_name: #crate_root::Tag = #tag;), + quote!(const #const_name: #crate_root::types::Tag = #tag;), quote!((#context, #const_name) => { #choice_name::#field_name(#decode_impl) }), quote!(#choice_name::#field_name(value) => { #set_field_impl }) ) @@ -243,7 +243,7 @@ pub fn derive_struct_impl( quote! { impl #impl_generics #crate_root::Decode for #name #ty_generics #where_clause { - fn decode_with_tag_and_constraints<'constraints, D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result { + fn decode_with_tag_and_constraints<'constraints, D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::types::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result { #decode_impl } } diff --git a/macros/src/encode.rs b/macros/src/encode.rs index 21ea2c58..ef36f413 100644 --- a/macros/src/encode.rs +++ b/macros/src/encode.rs @@ -36,7 +36,7 @@ pub fn derive_struct_impl( } else { quote!( match tag { - #crate_root::Tag::EOC => { + #crate_root::types::Tag::EOC => { self.0.encode(encoder) } _ => { @@ -82,7 +82,7 @@ pub fn derive_struct_impl( quote! { #[allow(clippy::mutable_key_type)] impl #impl_generics #crate_root::Encode for #name #ty_generics #where_clause { - fn encode_with_tag_and_constraints<'constraints, EN: #crate_root::Encoder>(&self, encoder: &mut EN, tag: #crate_root::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result<(), EN::Error> { + fn encode_with_tag_and_constraints<'constraints, EN: #crate_root::Encoder>(&self, encoder: &mut EN, tag: #crate_root::types::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result<(), EN::Error> { #(#vars)* #encode_impl diff --git a/macros/src/enum.rs b/macros/src/enum.rs index 229bddc6..fc73eb2f 100644 --- a/macros/src/enum.rs +++ b/macros/src/enum.rs @@ -19,8 +19,8 @@ impl Enum { || { self.config .enumerated - .then(|| quote!(#crate_root::Tag::ENUMERATED)) - .unwrap_or(quote!(#crate_root::Tag::EOC)) + .then(|| quote!(#crate_root::types::Tag::ENUMERATED)) + .unwrap_or(quote!(#crate_root::types::Tag::EOC)) }, |t| t.to_tokens(crate_root), ); @@ -39,14 +39,14 @@ impl Enum { quote! { { - const VARIANT_LIST: &'static [#crate_root::TagTree] = &[#(#field_tags),*]; - const VARIANT_TAG_TREE: #crate_root::TagTree = #crate_root::TagTree::Choice(VARIANT_LIST); + const VARIANT_LIST: &'static [#crate_root::types::TagTree] = &[#(#field_tags),*]; + const VARIANT_TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(VARIANT_LIST); const _: () = assert!(VARIANT_TAG_TREE.is_unique(), #error_message); VARIANT_TAG_TREE } } } else { - quote!(#crate_root::TagTree::Leaf(#tag)) + quote!(#crate_root::types::TagTree::Leaf(#tag)) }; let name = &self.name; @@ -150,12 +150,12 @@ impl Enum { quote! { impl #impl_generics #crate_root::AsnType for #name #ty_generics #where_clause { - const TAG: #crate_root::Tag = { + const TAG: #crate_root::types::Tag = { #tag }; - const TAG_TREE: #crate_root::TagTree = { - const LIST: &'static [#crate_root::TagTree] = &[#tag_tree]; - const TAG_TREE: #crate_root::TagTree = #crate_root::TagTree::Choice(LIST); + const TAG_TREE: #crate_root::types::TagTree = { + const LIST: &'static [#crate_root::types::TagTree] = &[#tag_tree]; + const TAG_TREE: #crate_root::types::TagTree = #crate_root::types::TagTree::Choice(LIST); const _: () = assert!(TAG_TREE.is_unique(), #error_message); #return_val }; @@ -275,7 +275,7 @@ impl Enum { quote! { #[automatically_derived] impl #impl_generics #crate_root::types::DecodeChoice for #name #ty_generics #where_clause { - fn from_tag(decoder: &mut D, tag: #crate_root::Tag) -> core::result::Result { + fn from_tag(decoder: &mut D, tag: #crate_root::types::Tag) -> core::result::Result { use #crate_root::de::Decode; #from_tag } @@ -288,7 +288,7 @@ impl Enum { #[automatically_derived] impl #impl_generics #crate_root::Decode for #name #ty_generics #where_clause { - fn decode_with_tag_and_constraints<'constraints, D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result { + fn decode_with_tag_and_constraints<'constraints, D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::types::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result { #decode_with_tag } @@ -310,7 +310,7 @@ impl Enum { }; quote! { - fn encode_with_tag_and_constraints<'constraints, EN: #crate_root::Encoder>(&self, encoder: &mut EN, tag: #crate_root::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result<(), EN::Error> { + fn encode_with_tag_and_constraints<'constraints, EN: #crate_root::Encoder>(&self, encoder: &mut EN, tag: #crate_root::types::Tag, constraints: #crate_root::types::Constraints<'constraints>) -> core::result::Result<(), EN::Error> { #operation } } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 150cc728..b2485fc5 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -42,7 +42,7 @@ pub fn decode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream impl #crate_root::Decode for #name { fn decode_with_tag_and_constraints( decoder: &mut D, - tag: #crate_root::Tag, + tag: #crate_root::types::Tag, _: #crate_root::prelude::Constraints, ) -> Result { decoder.decode_null(tag).map(|_| #name) @@ -87,7 +87,7 @@ pub fn encode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream fn encode_with_tag_and_constraints( &self, encoder: &mut E, - tag: #crate_root::Tag, + tag: #crate_root::types::Tag, _: #crate_root::prelude::Constraints, ) -> Result<(), E::Error> { encoder.encode_null(tag).map(drop) diff --git a/macros/src/tag.rs b/macros/src/tag.rs index 63886330..5e301eb8 100644 --- a/macros/src/tag.rs +++ b/macros/src/tag.rs @@ -159,7 +159,7 @@ impl Tag { match self { Self::Value { class, value, .. } => { let cls = class.as_tokens(crate_root); - quote!(#crate_root::Tag::new(#cls, #value)) + quote!(#crate_root::types::Tag::new(#cls, #value)) } Self::Delegate { ty } => quote!(<#ty as #crate_root::AsnType>::TAG), } diff --git a/src/ber.rs b/src/ber.rs index 70b8f70d..2ef4103a 100644 --- a/src/ber.rs +++ b/src/ber.rs @@ -230,7 +230,7 @@ mod tests { fn encode_with_tag_and_constraints( &self, encoder: &mut EN, - tag: crate::Tag, + tag: crate::types::Tag, _: Constraints, ) -> Result<(), EN::Error> { encoder.encode_set::(tag, |encoder| { diff --git a/src/error/decode.rs b/src/error/decode.rs index 1afe8e62..dd121ab6 100644 --- a/src/error/decode.rs +++ b/src/error/decode.rs @@ -70,9 +70,7 @@ impl From for DecodeError { /// # Example /// ```rust /// use nom::Needed; -/// use rasn::codec::Codec; -/// use rasn::error::DecodeErrorKind; -/// use rasn::prelude::*; +/// use rasn::{Codec, error::DecodeErrorKind, prelude::*}; /// /// #[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq)] /// #[rasn(delegate)] diff --git a/src/error/encode.rs b/src/error/encode.rs index a87d10ba..bfe1f813 100644 --- a/src/error/encode.rs +++ b/src/error/encode.rs @@ -57,10 +57,7 @@ impl From for EncodeError { /// /// # Example /// ``` -/// -/// use rasn::prelude::*; -/// use rasn::error::{EncodeErrorKind, strings::PermittedAlphabetError}; -/// use rasn::codec::Codec; +/// use rasn::{Codec, error::{EncodeErrorKind, strings::PermittedAlphabetError}, prelude::*}; /// /// #[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq)] /// #[rasn(delegate, from("a..z"))] diff --git a/src/jer/de.rs b/src/jer/de.rs index 1659104a..c41282df 100644 --- a/src/jer/de.rs +++ b/src/jer/de.rs @@ -53,7 +53,7 @@ impl crate::Decoder for Decoder { fn decode_bit_string( &mut self, - _t: crate::Tag, + _t: Tag, constraints: Constraints, ) -> Result { let (mut padded, bitstring_length) = if let Some(size) = constraints @@ -108,36 +108,33 @@ impl crate::Decoder for Decoder { Ok(padded) } - fn decode_bool(&mut self, _t: crate::Tag) -> Result { + fn decode_bool(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::boolean_from_value, self.stack) } - fn decode_enumerated(&mut self, _t: crate::Tag) -> Result { + fn decode_enumerated(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::enumerated_from_value, self.stack) } fn decode_integer( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { decode_jer_value!(Self::integer_from_value::, self.stack) } - fn decode_null(&mut self, _t: crate::Tag) -> Result<(), Self::Error> { + fn decode_null(&mut self, _t: Tag) -> Result<(), Self::Error> { decode_jer_value!(Self::null_from_value, self.stack) } - fn decode_object_identifier( - &mut self, - _t: crate::Tag, - ) -> Result { + fn decode_object_identifier(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::object_identifier_from_value, self.stack) } fn decode_sequence( &mut self, - _: crate::Tag, + _: Tag, _: Option, decode_fn: F, ) -> Result @@ -168,7 +165,7 @@ impl crate::Decoder for Decoder { fn decode_sequence_of( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result, Self::Error> { decode_jer_value!(|v| self.sequence_of_from_value(v), self.stack) @@ -176,7 +173,7 @@ impl crate::Decoder for Decoder { fn decode_set_of( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result, Self::Error> { decode_jer_value!(|v| self.set_of_from_value(v), self.stack) @@ -184,23 +181,19 @@ impl crate::Decoder for Decoder { fn decode_octet_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result, Self::Error> { decode_jer_value!(Self::octet_string_from_value, self.stack) } - fn decode_utf8_string( - &mut self, - _t: crate::Tag, - _c: Constraints, - ) -> Result { + fn decode_utf8_string(&mut self, _t: Tag, _c: Constraints) -> Result { decode_jer_value!(Self::string_from_value, self.stack) } fn decode_visible_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? @@ -216,7 +209,7 @@ impl crate::Decoder for Decoder { fn decode_general_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? @@ -230,11 +223,7 @@ impl crate::Decoder for Decoder { }) } - fn decode_ia5_string( - &mut self, - _t: crate::Tag, - _c: Constraints, - ) -> Result { + fn decode_ia5_string(&mut self, _t: Tag, _c: Constraints) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? .try_into() .map_err(|e| { @@ -248,7 +237,7 @@ impl crate::Decoder for Decoder { fn decode_printable_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? @@ -264,7 +253,7 @@ impl crate::Decoder for Decoder { fn decode_numeric_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? @@ -280,17 +269,13 @@ impl crate::Decoder for Decoder { fn decode_teletex_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result { todo!() } - fn decode_bmp_string( - &mut self, - _t: crate::Tag, - _c: Constraints, - ) -> Result { + fn decode_bmp_string(&mut self, _t: Tag, _c: Constraints) -> Result { decode_jer_value!(Self::string_from_value, self.stack)? .try_into() .map_err(|e| { @@ -302,35 +287,32 @@ impl crate::Decoder for Decoder { }) } - fn decode_explicit_prefix( - &mut self, - _t: crate::Tag, - ) -> Result { + fn decode_explicit_prefix(&mut self, _t: Tag) -> Result { D::decode(self) } - fn decode_utc_time(&mut self, _t: crate::Tag) -> Result { + fn decode_utc_time(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::utc_time_from_value, self.stack) } - fn decode_generalized_time(&mut self, _t: crate::Tag) -> Result { + fn decode_generalized_time(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::general_time_from_value, self.stack) } - fn decode_date(&mut self, _t: crate::Tag) -> Result { + fn decode_date(&mut self, _t: Tag) -> Result { decode_jer_value!(Self::date_from_value, self.stack) } fn decode_set( &mut self, - _t: crate::Tag, + _t: Tag, decode_fn: D, field_fn: F, ) -> Result where SET: crate::Decode + Constructed, FIELDS: crate::Decode, - D: Fn(&mut Self, usize, crate::Tag) -> Result, + D: Fn(&mut Self, usize, Tag) -> Result, F: FnOnce(alloc::vec::Vec) -> Result, { let mut last = self.stack.pop().ok_or_else(JerDecodeErrorKind::eoi)?; @@ -386,7 +368,7 @@ impl crate::Decoder for Decoder { fn decode_optional_with_tag( &mut self, - _: crate::Tag, + _: Tag, ) -> Result, Self::Error> { self.decode_optional() } @@ -400,7 +382,7 @@ impl crate::Decoder for Decoder { fn decode_optional_with_tag_and_constraints( &mut self, - _t: crate::Tag, + _t: Tag, _c: Constraints, ) -> Result, Self::Error> { self.decode_optional() diff --git a/src/jer/enc.rs b/src/jer/enc.rs index 7504de67..1fe49865 100644 --- a/src/jer/enc.rs +++ b/src/jer/enc.rs @@ -5,7 +5,7 @@ use jzon::{object::Object, JsonValue}; use crate::{ enc::Error, error::{EncodeError, JerEncodeErrorKind}, - types::{fields::Fields, variants, IntegerType}, + types::{fields::Fields, variants, IntegerType, Tag}, }; pub struct Encoder { @@ -62,21 +62,17 @@ impl crate::Encoder for Encoder { type Error = EncodeError; - fn encode_any( - &mut self, - t: crate::Tag, - value: &crate::types::Any, - ) -> Result { + fn encode_any(&mut self, t: Tag, value: &crate::types::Any) -> Result { self.encode_octet_string(t, <_>::default(), &value.contents) } - fn encode_bool(&mut self, _: crate::Tag, value: bool) -> Result { + fn encode_bool(&mut self, _: Tag, value: bool) -> Result { self.update_root_or_constructed(JsonValue::Boolean(value)) } fn encode_bit_string( &mut self, - _t: crate::Tag, + _t: Tag, constraints: crate::types::Constraints, value: &crate::types::BitStr, ) -> Result { @@ -109,7 +105,7 @@ impl crate::Encoder for Encoder { fn encode_enumerated( &mut self, - _: crate::Tag, + _: Tag, value: &E, ) -> Result { self.update_root_or_constructed(JsonValue::String(alloc::string::String::from( @@ -119,7 +115,7 @@ impl crate::Encoder for Encoder { fn encode_object_identifier( &mut self, - _t: crate::Tag, + _t: Tag, value: &[u32], ) -> Result { self.update_root_or_constructed(JsonValue::String( @@ -133,7 +129,7 @@ impl crate::Encoder for Encoder { fn encode_integer( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &I, ) -> Result { @@ -147,13 +143,13 @@ impl crate::Encoder for Encoder { } } - fn encode_null(&mut self, _: crate::Tag) -> Result { + fn encode_null(&mut self, _: Tag) -> Result { self.update_root_or_constructed(JsonValue::Null) } fn encode_octet_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &[u8], ) -> Result { @@ -168,7 +164,7 @@ impl crate::Encoder for Encoder { fn encode_general_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::GeneralString, ) -> Result { @@ -180,7 +176,7 @@ impl crate::Encoder for Encoder { fn encode_utf8_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &str, ) -> Result { @@ -189,7 +185,7 @@ impl crate::Encoder for Encoder { fn encode_visible_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::VisibleString, ) -> Result { @@ -201,7 +197,7 @@ impl crate::Encoder for Encoder { fn encode_ia5_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::Ia5String, ) -> Result { @@ -213,7 +209,7 @@ impl crate::Encoder for Encoder { fn encode_printable_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::PrintableString, ) -> Result { @@ -225,7 +221,7 @@ impl crate::Encoder for Encoder { fn encode_numeric_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::NumericString, ) -> Result { @@ -237,7 +233,7 @@ impl crate::Encoder for Encoder { fn encode_teletex_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, _value: &crate::types::TeletexString, ) -> Result { @@ -246,7 +242,7 @@ impl crate::Encoder for Encoder { fn encode_bmp_string( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &crate::types::BmpString, ) -> Result { @@ -258,7 +254,7 @@ impl crate::Encoder for Encoder { fn encode_generalized_time( &mut self, - _t: crate::Tag, + _t: Tag, value: &crate::types::GeneralizedTime, ) -> Result { self.update_root_or_constructed(JsonValue::String( @@ -271,7 +267,7 @@ impl crate::Encoder for Encoder { fn encode_utc_time( &mut self, - _t: crate::Tag, + _t: Tag, value: &crate::types::UtcTime, ) -> Result { self.update_root_or_constructed(JsonValue::String( @@ -284,7 +280,7 @@ impl crate::Encoder for Encoder { fn encode_date( &mut self, - _t: crate::Tag, + _t: Tag, value: &crate::types::Date, ) -> Result { self.update_root_or_constructed(JsonValue::String( @@ -297,17 +293,13 @@ impl crate::Encoder for Encoder { fn encode_explicit_prefix( &mut self, - _: crate::Tag, + _: Tag, value: &V, ) -> Result { value.encode(self) } - fn encode_sequence( - &mut self, - __t: crate::Tag, - encoder_scope: F, - ) -> Result + fn encode_sequence(&mut self, __t: Tag, encoder_scope: F) -> Result where C: crate::types::Constructed, F: FnOnce(&mut Self) -> Result<(), Self::Error>, @@ -334,7 +326,7 @@ impl crate::Encoder for Encoder { fn encode_sequence_of( &mut self, - _t: crate::Tag, + _t: Tag, value: &[E], _c: crate::types::Constraints, ) -> Result { @@ -352,7 +344,7 @@ impl crate::Encoder for Encoder { )?)) } - fn encode_set(&mut self, tag: crate::Tag, value: F) -> Result + fn encode_set(&mut self, tag: Tag, value: F) -> Result where C: crate::types::Constructed, F: FnOnce(&mut Self) -> Result<(), Self::Error>, @@ -362,7 +354,7 @@ impl crate::Encoder for Encoder { fn encode_set_of( &mut self, - _t: crate::Tag, + _t: Tag, value: &crate::types::SetOf, _c: crate::types::Constraints, ) -> Result { @@ -386,7 +378,7 @@ impl crate::Encoder for Encoder { fn encode_some_with_tag_and_constraints( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: &E, ) -> Result { @@ -398,7 +390,7 @@ impl crate::Encoder for Encoder { Ok(()) } - fn encode_none_with_tag(&mut self, _t: crate::Tag) -> Result { + fn encode_none_with_tag(&mut self, _t: Tag) -> Result { self.stack.pop(); Ok(()) } @@ -406,8 +398,8 @@ impl crate::Encoder for Encoder { fn encode_choice( &mut self, _c: crate::types::Constraints, - tag: crate::types::Tag, - encode_fn: impl FnOnce(&mut Self) -> Result, + tag: Tag, + encode_fn: impl FnOnce(&mut Self) -> Result, ) -> Result { let variants = variants::Variants::from_slice( &[E::VARIANTS, E::EXTENDED_VARIANTS.unwrap_or(&[])].concat(), @@ -441,7 +433,7 @@ impl crate::Encoder for Encoder { fn encode_extension_addition( &mut self, - _t: crate::Tag, + _t: Tag, _c: crate::types::Constraints, value: E, ) -> Result { diff --git a/src/lib.rs b/src/lib.rs index 0154c1c2..1588f679 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,11 +103,11 @@ macro_rules! encode_error_with_constraints { mod bits; mod codec; -mod num; -mod per; pub mod de; pub mod enc; pub mod error; +mod num; +mod per; pub mod types; // Data Formats @@ -127,7 +127,7 @@ pub use self::{ codec::Codec, de::{Decode, Decoder}, enc::{Encode, Encoder}, - types::{AsnType, Tag, TagTree}, + types::AsnType, }; /// A prelude containing the codec traits and all types defined in the [`types`] diff --git a/src/oer/de.rs b/src/oer/de.rs index a636c796..15fb0382 100644 --- a/src/oer/de.rs +++ b/src/oer/de.rs @@ -3,20 +3,26 @@ // the structure of the encoding. In particular, the end of the encoding cannot be determined from // the encoding itself without knowledge of the type being encoded ITU-T X.696 (6.2). -use super::enc::EncodingRules; -use crate::oer::ranges; -use crate::prelude::{ - Any, BitString, BmpString, Constraints, Constructed, DecodeChoice, Enumerated, GeneralString, - GeneralizedTime, Ia5String, NumericString, ObjectIdentifier, PrintableString, SetOf, - TeletexString, UtcTime, VisibleString, -}; -use crate::types::fields::{Field, Fields}; -use crate::{types, Codec, Decode, Tag}; use alloc::{ collections::VecDeque, string::{String, ToString}, vec::Vec, }; + +use crate::{ + oer::ranges, + types::{ + self, + fields::{Field, Fields}, + Any, BitString, BmpString, Constraints, Constructed, DecodeChoice, Enumerated, + GeneralString, GeneralizedTime, Ia5String, NumericString, ObjectIdentifier, + PrintableString, SetOf, Tag, TeletexString, UtcTime, VisibleString, + }, + Codec, Decode, +}; + +use super::enc::EncodingRules; + use bitvec::field::BitField; use nom::{AsBytes, Slice}; use num_integer::div_ceil; @@ -847,9 +853,9 @@ impl<'input> crate::Decoder for Decoder<'input> { { let is_extensible = constraints.extensible(); let tag: Tag = self.parse_tag()?; - let is_root_extension = crate::TagTree::tag_contains(&tag, D::VARIANTS); + let is_root_extension = crate::types::TagTree::tag_contains(&tag, D::VARIANTS); let is_extended_extension = - crate::TagTree::tag_contains(&tag, D::EXTENDED_VARIANTS.unwrap_or(&[])); + crate::types::TagTree::tag_contains(&tag, D::EXTENDED_VARIANTS.unwrap_or(&[])); if is_root_extension { D::from_tag(self, tag) } else if is_extensible && is_extended_extension { diff --git a/src/oer/enc.rs b/src/oer/enc.rs index 1a9d468d..8246a04a 100644 --- a/src/oer/enc.rs +++ b/src/oer/enc.rs @@ -1,18 +1,17 @@ use alloc::vec::Vec; - -use crate::oer::ranges; -use crate::prelude::{ - Any, BitStr, BmpString, Choice, Constructed, Enumerated, GeneralString, GeneralizedTime, - Ia5String, NumericString, PrintableString, SetOf, TeletexString, UtcTime, VisibleString, -}; -use crate::types::IntegerType; -use crate::Codec; use bitvec::prelude::*; use hashbrown::HashMap; use num_traits::ToPrimitive; -use crate::types::{fields::FieldPresence, BitString, Constraints, Date}; -use crate::{Encode, Tag}; +use crate::{ + oer::ranges, + types::{ + fields::FieldPresence, Any, BitStr, BitString, BmpString, Choice, Constraints, Constructed, + Date, Enumerated, GeneralString, GeneralizedTime, Ia5String, IntegerType, NumericString, + PrintableString, SetOf, Tag, TeletexString, UtcTime, VisibleString, + }, + Codec, Encode, +}; /// ITU-T X.696 (02/2021) version of (C)OER encoding /// On this crate, only canonical version will be used to provide unique and reproducible encodings. @@ -921,7 +920,7 @@ impl crate::Encoder for Encoder { ) -> Result { let mut choice_encoder = Self::new(self.options.without_set_encoding()); let tag = encode_fn(&mut choice_encoder)?; - let is_root_extension = crate::TagTree::tag_contains(&tag, E::VARIANTS); + let is_root_extension = crate::types::TagTree::tag_contains(&tag, E::VARIANTS); let tag_bytes: Vec = Self::encode_tag(tag); let mut buffer = Vec::new(); buffer.extend(tag_bytes); diff --git a/src/per/enc.rs b/src/per/enc.rs index 101be9be..84420b59 100644 --- a/src/per/enc.rs +++ b/src/per/enc.rs @@ -1155,7 +1155,7 @@ impl crate::Encoder for Encoder { ) -> Result { let mut buffer = BitString::new(); - let is_root_extension = crate::TagTree::tag_contains(&tag, E::VARIANTS); + let is_root_extension = crate::types::TagTree::tag_contains(&tag, E::VARIANTS); self.encode_extensible_bit(&constraints, &mut buffer, || is_root_extension); let variants = crate::types::variants::Variants::from_static(if is_root_extension { E::VARIANTS diff --git a/src/types/date.rs b/src/types/date.rs index 360dfc3f..d1c9a12b 100644 --- a/src/types/date.rs +++ b/src/types/date.rs @@ -1,6 +1,6 @@ use crate::prelude::Constraints; use crate::types::Date; -use crate::{AsnType, Decode, Decoder, Encode, Encoder, Tag}; +use crate::{types::Tag, AsnType, Decode, Decoder, Encode, Encoder}; impl AsnType for Date { const TAG: Tag = Tag::DATE; diff --git a/src/types/integer.rs b/src/types/integer.rs index f47bcd17..2166c821 100644 --- a/src/types/integer.rs +++ b/src/types/integer.rs @@ -1,5 +1,4 @@ -use crate::types::{constraints, AsnType, Constraints, Extensible}; -use crate::Tag; +use crate::types::{constraints, AsnType, Constraints, Extensible, Tag}; use alloc::boxed::Box; use core::hash::Hash; use num_bigint::{BigInt, BigUint, ToBigInt}; diff --git a/src/types/prefix.rs b/src/types/prefix.rs index ea1b2def..c0c90ef6 100644 --- a/src/types/prefix.rs +++ b/src/types/prefix.rs @@ -1,4 +1,4 @@ -use crate::{AsnType, Tag}; +use crate::{types::Tag, AsnType}; /// A newtype wrapper that will explicitly tag its value with `T`'s tag. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/standards/kerberos/tests/issue111.rs b/standards/kerberos/tests/issue111.rs index 5902bf24..82d938ef 100644 --- a/standards/kerberos/tests/issue111.rs +++ b/standards/kerberos/tests/issue111.rs @@ -1,4 +1,8 @@ -use rasn::{ber, types::Class, Decoder, Encoder, Tag}; +use rasn::{ + ber, + types::{Class, Tag}, + Decoder, Encoder, +}; use rasn_kerberos::KerberosFlags; #[test] diff --git a/standards/smi/src/v1.rs b/standards/smi/src/v1.rs index 729ca341..3203b67c 100644 --- a/standards/smi/src/v1.rs +++ b/standards/smi/src/v1.rs @@ -2,8 +2,8 @@ use rasn::{ error::EncodeError, - types::{Constraints, FixedOctetString, Integer, ObjectIdentifier, OctetString, Oid}, - AsnType, Decode, Encode, Tag, + types::{Constraints, FixedOctetString, Integer, ObjectIdentifier, OctetString, Oid, Tag}, + AsnType, Decode, Encode, }; pub const INTERNET: &Oid = Oid::ISO_IDENTIFIED_ORGANISATION_DOD_INTERNET; diff --git a/standards/snmp/src/v3.rs b/standards/snmp/src/v3.rs index 033d9773..883f2539 100644 --- a/standards/snmp/src/v3.rs +++ b/standards/snmp/src/v3.rs @@ -51,7 +51,7 @@ impl Message { /// - If the value fails to be encoded as the `codec`. pub fn decode_security_parameters( &self, - codec: rasn::codec::Codec, + codec: rasn::Codec, ) -> Result> { if self.global_data.security_model != T::ID.into() { return Err(Box::new(alloc::format!( @@ -73,7 +73,7 @@ impl Message { /// - If the value fails to be encoded as the `codec`. pub fn encode_security_parameters( &mut self, - codec: rasn::codec::Codec, + codec: rasn::Codec, value: &T, ) -> Result<(), alloc::boxed::Box> { self.global_data.security_model = T::ID.into(); diff --git a/tests/personnel.rs b/tests/personnel.rs index 4b883ce7..9285f051 100644 --- a/tests/personnel.rs +++ b/tests/personnel.rs @@ -20,7 +20,7 @@ impl rasn::Encode for PersonnelRecord { fn encode_with_tag_and_constraints( &self, encoder: &mut EN, - tag: rasn::Tag, + tag: rasn::types::Tag, _: rasn::types::Constraints, ) -> core::result::Result<(), EN::Error> { #[allow(unused)] @@ -39,20 +39,20 @@ impl rasn::Encode for PersonnelRecord { .encode_set::(tag, |encoder| { self.name.encode(encoder)?; encoder.encode_explicit_prefix( - rasn::Tag::new(rasn::types::Class::Context, 0), + rasn::types::Tag::new(rasn::types::Class::Context, 0), &self.title, )?; self.number.encode(encoder)?; encoder.encode_explicit_prefix( - rasn::Tag::new(rasn::types::Class::Context, 1), + rasn::types::Tag::new(rasn::types::Class::Context, 1), &self.date_of_hire, )?; encoder.encode_explicit_prefix( - rasn::Tag::new(rasn::types::Class::Context, 2), + rasn::types::Tag::new(rasn::types::Class::Context, 2), &self.name_of_spouse, )?; encoder.encode_default_with_tag( - rasn::Tag::new(rasn::types::Class::Context, 3), + rasn::types::Tag::new(rasn::types::Class::Context, 3), &self.children, >::default, )?;