@@ -104,6 +104,18 @@ pub mod __private {
104
104
pub use syn;
105
105
}
106
106
107
+ /// Helper trait providing the path for an attribute.
108
+ ///
109
+ /// Automatically derived with [`Attribute`], if `#[attribute(ident =
110
+ /// "some_ident")]` is provided.
111
+ /// ```
112
+ pub trait AttributeIdent : Sized {
113
+ const ATTRIBUTE_IDENT : & ' static str ;
114
+ fn get_attribute_ident ( ) -> & ' static str {
115
+ Self :: ATTRIBUTE_IDENT
116
+ }
117
+ }
118
+
107
119
/// The trait you actually derive on your attribute struct.
108
120
///
109
121
/// Basic gist is a struct like this:
@@ -132,7 +144,6 @@ pub mod __private {
132
144
/// #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()], some_flag)]
133
145
/// ```
134
146
pub trait Attribute : Sized {
135
- const IDENT : Option < & ' static str > ;
136
147
type Parser : TryExtendOne + Parse + Default ;
137
148
138
149
#[ doc( hidden) ]
@@ -162,12 +173,15 @@ pub trait Attribute: Sized {
162
173
/// - Invalid input is given for a parameter
163
174
/// - A non aggregating parameter is specified multiple times
164
175
/// - An attribute called [`IDENT`](Self::IDENT) has invalid syntax (e.g. `#attr(a: "a")`)
165
- fn from_attributes < ' a > ( attrs : impl IntoIterator < Item = & ' a syn:: Attribute > ) -> Result < Self > {
176
+ fn from_attributes < ' a > ( attrs : impl IntoIterator < Item = & ' a syn:: Attribute > ) -> Result < Self >
177
+ where
178
+ Self : AttributeIdent ,
179
+ {
166
180
attrs
167
181
. into_iter ( )
168
182
. filter_map ( |attr| {
169
183
attr. path
170
- . is_ident ( Self :: IDENT . expect ( r#"To use `from_attributes` you need to pass the attribute name while deriving with `#[attribute(ident="some_ident")]"# ) )
184
+ . is_ident ( Self :: ATTRIBUTE_IDENT )
171
185
. then ( || attr. parse_args :: < Self :: Parser > ( ) )
172
186
} )
173
187
. try_fold ( Self :: Parser :: default ( ) , |mut acc, item| {
@@ -201,11 +215,14 @@ pub trait Attribute: Sized {
201
215
/// - Invalid input is given for a parameter
202
216
/// - A non aggregating parameter is specified multiple times
203
217
/// - An attribute called [`IDENT`](Self::IDENT) has invalid syntax (e.g. `#attr(a: "a")`)
204
- fn remove_attributes ( attrs : & mut Vec < syn:: Attribute > ) -> Result < Self > {
218
+ fn remove_attributes ( attrs : & mut Vec < syn:: Attribute > ) -> Result < Self >
219
+ where
220
+ Self : AttributeIdent ,
221
+ {
205
222
let mut parser: Self :: Parser = Default :: default ( ) ;
206
223
let mut i = 0 ;
207
224
while i < attrs. len ( ) {
208
- if attrs[ i] . path . is_ident ( Self :: IDENT . expect ( r#"To use `remove_attributes` you need to pass the attribute name while deriving with `#[attribute(ident="some_ident")]"# ) ) {
225
+ if attrs[ i] . path . is_ident ( Self :: ATTRIBUTE_IDENT ) {
209
226
parser. try_extend_one ( attrs. remove ( i) . parse_args ( ) ?) ?;
210
227
} else {
211
228
i += 1 ;
0 commit comments