Skip to content

Commit cef6205

Browse files
authored
Merge pull request RustCrypto#1 from baloo/baloo/der_derive/error-type
2 parents 87bd37f + 4e31347 commit cef6205

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

der_derive/src/attributes.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@
22
33
use crate::{Asn1Type, Tag, TagMode, TagNumber};
44
use proc_macro2::{Span, TokenStream};
5-
use quote::quote;
5+
use quote::{quote, ToTokens};
66
use std::{fmt::Debug, str::FromStr};
77
use syn::punctuated::Punctuated;
88
use syn::{parse::Parse, parse::ParseStream, Attribute, Ident, LitStr, Path, Token};
99

10+
/// Error type used by the structure
11+
#[derive(Debug, Clone, Default, Eq, PartialEq)]
12+
pub(crate) enum ErrorType {
13+
/// Represents the ::der::Error type
14+
#[default]
15+
Der,
16+
/// Represents an error designed by Path
17+
Custom(Path),
18+
}
19+
20+
impl ToTokens for ErrorType {
21+
fn to_tokens(&self, tokens: &mut TokenStream) {
22+
match self {
23+
Self::Der => {
24+
let err = quote! { ::der::Error };
25+
err.to_tokens(tokens)
26+
}
27+
Self::Custom(path) => path.to_tokens(tokens),
28+
}
29+
}
30+
}
31+
1032
/// Attribute name.
1133
pub(crate) const ATTR_NAME: &str = "asn1";
1234

@@ -18,7 +40,7 @@ pub(crate) struct TypeAttrs {
1840
///
1941
/// The default value is `EXPLICIT`.
2042
pub tag_mode: TagMode,
21-
pub error: Option<Path>,
43+
pub error: ErrorType,
2244
}
2345

2446
impl TypeAttrs {
@@ -44,7 +66,7 @@ impl TypeAttrs {
4466
abort!(attr, "duplicate ASN.1 `error` attribute");
4567
}
4668

47-
error = Some(meta.value()?.parse()?);
69+
error = Some(ErrorType::Custom(meta.value()?.parse()?));
4870
} else {
4971
return Err(syn::Error::new_spanned(
5072
attr,
@@ -58,7 +80,7 @@ impl TypeAttrs {
5880

5981
Ok(Self {
6082
tag_mode: tag_mode.unwrap_or_default(),
61-
error,
83+
error: error.unwrap_or_default(),
6284
})
6385
}
6486
}

der_derive/src/choice.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
mod variant;
66

77
use self::variant::ChoiceVariant;
8-
use crate::{default_lifetime, TypeAttrs};
8+
use crate::{default_lifetime, ErrorType, TypeAttrs};
99
use proc_macro2::TokenStream;
1010
use quote::{quote, ToTokens};
11-
use syn::{DeriveInput, GenericParam, Generics, Ident, LifetimeParam, Path};
11+
use syn::{DeriveInput, GenericParam, Generics, Ident, LifetimeParam};
1212

1313
/// Derive the `Choice` trait for an enum.
1414
pub(crate) struct DeriveChoice {
@@ -22,7 +22,7 @@ pub(crate) struct DeriveChoice {
2222
variants: Vec<ChoiceVariant>,
2323

2424
/// Error type for `DecodeValue` implementation.
25-
error: Option<Path>,
25+
error: ErrorType,
2626
}
2727

2828
impl DeriveChoice {
@@ -36,7 +36,7 @@ impl DeriveChoice {
3636
),
3737
};
3838

39-
let mut type_attrs = TypeAttrs::parse(&input.attrs)?;
39+
let type_attrs = TypeAttrs::parse(&input.attrs)?;
4040
let variants = data
4141
.variants
4242
.iter()
@@ -47,7 +47,7 @@ impl DeriveChoice {
4747
ident: input.ident,
4848
generics: input.generics.clone(),
4949
variants,
50-
error: type_attrs.error.take(),
50+
error: type_attrs.error.clone(),
5151
})
5252
}
5353

@@ -88,11 +88,7 @@ impl DeriveChoice {
8888
tagged_body.push(variant.to_tagged_tokens());
8989
}
9090

91-
let error = self
92-
.error
93-
.as_ref()
94-
.map(ToTokens::to_token_stream)
95-
.unwrap_or_else(|| quote! { ::der::Error });
91+
let error = self.error.to_token_stream();
9692

9793
quote! {
9894
impl #impl_generics ::der::Choice<#lifetime> for #ident #ty_generics #where_clause {

der_derive/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod value_ord;
144144

145145
use crate::{
146146
asn1_type::Asn1Type,
147-
attributes::{FieldAttrs, TypeAttrs, ATTR_NAME},
147+
attributes::{ErrorType, FieldAttrs, TypeAttrs, ATTR_NAME},
148148
choice::DeriveChoice,
149149
enumerated::DeriveEnumerated,
150150
sequence::DeriveSequence,

der_derive/src/sequence.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
44
mod field;
55

6-
use crate::{default_lifetime, TypeAttrs};
6+
use crate::{default_lifetime, ErrorType, TypeAttrs};
77
use field::SequenceField;
88
use proc_macro2::TokenStream;
99
use quote::{quote, ToTokens};
10-
use syn::{DeriveInput, GenericParam, Generics, Ident, LifetimeParam, Path};
10+
use syn::{DeriveInput, GenericParam, Generics, Ident, LifetimeParam};
1111

1212
/// Derive the `Sequence` trait for a struct
1313
pub(crate) struct DeriveSequence {
@@ -21,7 +21,7 @@ pub(crate) struct DeriveSequence {
2121
fields: Vec<SequenceField>,
2222

2323
/// Error type for `DecodeValue` implementation.
24-
error: Option<Path>,
24+
error: ErrorType,
2525
}
2626

2727
impl DeriveSequence {
@@ -35,7 +35,7 @@ impl DeriveSequence {
3535
),
3636
};
3737

38-
let mut type_attrs = TypeAttrs::parse(&input.attrs)?;
38+
let type_attrs = TypeAttrs::parse(&input.attrs)?;
3939

4040
let fields = data
4141
.fields
@@ -47,7 +47,7 @@ impl DeriveSequence {
4747
ident: input.ident,
4848
generics: input.generics.clone(),
4949
fields,
50-
error: type_attrs.error.take(),
50+
error: type_attrs.error.clone(),
5151
})
5252
}
5353

@@ -88,11 +88,7 @@ impl DeriveSequence {
8888
encode_fields.push(quote!(#field.encode(writer)?;));
8989
}
9090

91-
let error = self
92-
.error
93-
.as_ref()
94-
.map(ToTokens::to_token_stream)
95-
.unwrap_or_else(|| quote! { ::der::Error });
91+
let error = self.error.to_token_stream();
9692

9793
quote! {
9894
impl #impl_generics ::der::DecodeValue<#lifetime> for #ident #ty_generics #where_clause {

0 commit comments

Comments
 (0)