22
33use crate :: { Asn1Type , Tag , TagMode , TagNumber } ;
44use proc_macro2:: { Span , TokenStream } ;
5- use quote:: quote;
5+ use quote:: { quote, ToTokens } ;
66use std:: { fmt:: Debug , str:: FromStr } ;
77use syn:: punctuated:: Punctuated ;
88use 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.
1133pub ( 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
2446impl 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}
0 commit comments