2
2
3
3
use crate :: { Asn1Type , Tag , TagMode , TagNumber } ;
4
4
use proc_macro2:: { Span , TokenStream } ;
5
- use quote:: quote;
5
+ use quote:: { quote, ToTokens } ;
6
6
use std:: { fmt:: Debug , str:: FromStr } ;
7
7
use syn:: punctuated:: Punctuated ;
8
8
use syn:: { parse:: Parse , parse:: ParseStream , Attribute , Ident , LitStr , Path , Token } ;
9
9
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
+
10
32
/// Attribute name.
11
33
pub ( crate ) const ATTR_NAME : & str = "asn1" ;
12
34
@@ -18,7 +40,7 @@ pub(crate) struct TypeAttrs {
18
40
///
19
41
/// The default value is `EXPLICIT`.
20
42
pub tag_mode : TagMode ,
21
- pub error : Option < Path > ,
43
+ pub error : ErrorType ,
22
44
}
23
45
24
46
impl TypeAttrs {
@@ -44,7 +66,7 @@ impl TypeAttrs {
44
66
abort ! ( attr, "duplicate ASN.1 `error` attribute" ) ;
45
67
}
46
68
47
- error = Some ( meta. value ( ) ?. parse ( ) ?) ;
69
+ error = Some ( ErrorType :: Custom ( meta. value ( ) ?. parse ( ) ?) ) ;
48
70
} else {
49
71
return Err ( syn:: Error :: new_spanned (
50
72
attr,
@@ -58,7 +80,7 @@ impl TypeAttrs {
58
80
59
81
Ok ( Self {
60
82
tag_mode : tag_mode. unwrap_or_default ( ) ,
61
- error,
83
+ error : error . unwrap_or_default ( ) ,
62
84
} )
63
85
}
64
86
}
0 commit comments