Skip to content

Commit

Permalink
fix!: Remove Tag and TagTree from module root
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Sep 13, 2024
1 parent 2efddca commit f81211f
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 166 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions macros/src/asn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
})
Expand Down Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions macros/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
Expand All @@ -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
Expand All @@ -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!(
Expand All @@ -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(_) => {
Expand Down Expand Up @@ -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();
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions macros/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?))
}
_ => {
Expand Down Expand Up @@ -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 })
)
Expand Down Expand Up @@ -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<Self, D::Error> {
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<Self, D::Error> {
#decode_impl
}
}
Expand Down
4 changes: 2 additions & 2 deletions macros/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
_ => {
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions macros/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand All @@ -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;
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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<D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::Tag) -> core::result::Result<Self, D::Error> {
fn from_tag<D: #crate_root::Decoder>(decoder: &mut D, tag: #crate_root::types::Tag) -> core::result::Result<Self, D::Error> {
use #crate_root::de::Decode;
#from_tag
}
Expand All @@ -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<Self, D::Error> {
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<Self, D::Error> {
#decode_with_tag
}

Expand All @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: #crate_root::Decoder>(
decoder: &mut D,
tag: #crate_root::Tag,
tag: #crate_root::types::Tag,
_: #crate_root::prelude::Constraints,
) -> Result<Self, D::Error> {
decoder.decode_null(tag).map(|_| #name)
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn encode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream
fn encode_with_tag_and_constraints<E: #crate_root::Encoder>(
&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)
Expand Down
2 changes: 1 addition & 1 deletion macros/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion src/ber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod tests {
fn encode_with_tag_and_constraints<EN: crate::Encoder>(
&self,
encoder: &mut EN,
tag: crate::Tag,
tag: crate::types::Tag,
_: Constraints,
) -> Result<(), EN::Error> {
encoder.encode_set::<Self, _>(tag, |encoder| {
Expand Down
4 changes: 1 addition & 3 deletions src/error/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl From<CodecDecodeError> 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)]
Expand Down
5 changes: 1 addition & 4 deletions src/error/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ impl From<CodecEncodeError> 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"))]
Expand Down
Loading

0 comments on commit f81211f

Please sign in to comment.