attribute_derive/
syn_impls.rs

1use proc_macro2::{Group, Literal, Punct, TokenStream, TokenTree};
2use syn::token::{
3    Abstract, And, AndAnd, AndEq, As, Async, At, Auto, Await, Become, Break, Caret, CaretEq, Colon,
4    Comma, Const, Continue, Crate, Do, Dollar, Dot, DotDot, DotDotDot, DotDotEq, Dyn, Else, Enum,
5    Eq, EqEq, Extern, FatArrow, Final, Fn, For, Ge, Gt, If, Impl, In, LArrow, Le, Let, Loop, Lt,
6    Match, Minus, MinusEq, Mod, Move, Mut, Ne, Not, Or, OrEq, OrOr, Override, PathSep, Percent,
7    PercentEq, Plus, PlusEq, Pound, Priv, Pub, Question, RArrow, Ref, Return, SelfType, SelfValue,
8    Semi, Shl, ShlEq, Shr, ShrEq, Slash, SlashEq, Star, StarEq, Static, Struct, Super, Tilde,
9    Trait, Try, Typeof, Underscore, Union, Unsafe, Unsized, Use, Virtual, Where, While, Yield,
10};
11use syn::{
12    Abi, AngleBracketedGenericArguments, BareFnArg, BinOp, BoundLifetimes, ConstParam, DeriveInput,
13    Expr, FieldsNamed, FieldsUnnamed, GenericArgument, GenericParam, Generics, Ident, Index,
14    Lifetime, Lit, LitBool, LitByteStr, LitChar, LitFloat, LitInt, LitStr, Member, Meta, MetaList,
15    MetaNameValue, ParenthesizedGenericArguments, Path, PathSegment, ReturnType, TraitBound,
16    TraitBoundModifier, Type, TypeArray, TypeBareFn, TypeGroup, TypeImplTrait, TypeInfer,
17    TypeMacro, TypeNever, TypeParam, TypeParamBound, TypeParen, TypePath, TypePtr, TypeReference,
18    TypeSlice, TypeTraitObject, TypeTuple, UnOp, Variant, Visibility, WhereClause, WherePredicate,
19};
20
21use crate::parsing::*;
22use crate::*;
23
24/// Macro to easily implement [`AttributeValue`] for types implementing
25/// [`Parse`] and [`ToTokens`].
26#[macro_export]
27macro_rules! impl_Attribute_for_Parse_and_ToTokens {
28    ($($type:ty),+ $(,)?) => {$(
29        impl $crate::parsing::AttributeBase for $type {
30            type Partial = Self;
31        }
32
33        impl $crate::parsing::AttributeValue for $type {
34            fn parse_value(input: $crate::__private::syn::parse::ParseStream) -> $crate::__private::syn::Result<$crate::parsing::SpannedValue<Self::Partial>> {
35                input.parse().map($crate::parsing::SpannedValue::from_to_tokens)
36            }
37        }
38
39        impl $crate::parsing::PositionalValue for $type {}
40
41    )*}
42}
43
44impl AttributeBase for TokenStream {
45    type Partial = Self;
46}
47
48impl AttributeMeta for TokenStream {
49    fn parse_inner(input: ParseStream) -> Result<Self::Partial> {
50        input.parse()
51    }
52}
53
54// /// Macro to easily implement [`ConvertParsed`] for syn types.
55// macro_rules! ParseToTokensAttribute {
56//     ($(#[$meta:meta])* $type:path) => {
57//         $(#[$meta])*
58//         impl ConvertParsed for $type {
59//             type Type = $type;
60//             fn convert(s: Self) -> Result<Self> {
61//                 Ok(s)
62//             }
63//         }
64//     };
65//     [$($type:path),* $(,)?] => {
66//         $(
67//         impl ConvertParsed for $type {
68//             type Type = $type;
69//             fn convert(s: Self) -> Result<Self> {
70//                 Ok(s)
71//             }
72//             }
73//         )*
74//     };
75//     ($from:path => $to:path) => {
76//         impl ConvertParsed<$from> for $to {
77//             fn convert(value: $from) -> Result<$to> {
78//                 Ok(value.into())
79//             }
80//         }
81//     };
82//     ($from:path => $($to:path),+ : $with:path ) => {
83//         $(
84//             impl ConvertParsed for $to {
85//                 type Type = $from;
86//                 fn convert(value: $from) -> Result<$to> {
87//                     Ok($with(&value))
88//                 }
89//             }
90//         )*
91//     };
92//     ($from:path => $($to:path),+ :? $with:path ) => {
93//         $(
94//             impl ConvertParsed for $to {
95//                 type Type = $from;
96//                 fn convert(value: $from) -> Result<$to> {
97//                     $with(&value)
98//                 }
99//             }
100//         )*
101//     };
102// }
103
104impl_Attribute_for_Parse_and_ToTokens!(Type);
105impl_Attribute_for_Parse_and_ToTokens!(Path);
106impl_Attribute_for_Parse_and_ToTokens!(Lit);
107impl_Attribute_for_Parse_and_ToTokens![LitStr, LitByteStr, LitChar, LitInt, LitFloat, LitBool];
108impl_Attribute_for_Parse_and_ToTokens!(Expr);
109impl_Attribute_for_Parse_and_ToTokens![TokenTree, Group, Punct, Literal];
110
111// // TODO make this warning better visable
112// ParseToTokensAttribute! {
113//     /// Try to avoid using this, as it will consume everything behind, so it
114// needs to be defined as the     /// last parameter.
115//     ///
116//     /// In the future there might be something to allow better handling of
117// this (maybe by putting it     /// into `()`)
118//     TokenStream
119// }
120
121// Some probably useless stuff
122impl_Attribute_for_Parse_and_ToTokens![
123    Abi,
124    Abstract,
125    Plus,
126    PlusEq,
127    And,
128    AndAnd,
129    AndEq,
130    AngleBracketedGenericArguments,
131    As,
132    Async,
133    At,
134    Auto,
135    Await,
136    Not,
137    BareFnArg,
138    Become,
139    BinOp,
140    BoundLifetimes,
141    Break,
142    Caret,
143    CaretEq,
144    Colon,
145    PathSep,
146    Comma,
147    Const,
148    ConstParam,
149    Continue,
150    Crate,
151    DeriveInput,
152    Slash,
153    SlashEq,
154    Do,
155    Dollar,
156    Dot,
157    DotDot,
158    DotDotDot,
159    DotDotEq,
160    Dyn,
161    Else,
162    Enum,
163    Eq,
164    EqEq,
165    Extern,
166    FatArrow,
167    FieldsNamed,
168    FieldsUnnamed,
169    Final,
170    Fn,
171    For,
172    Ge,
173    GenericArgument,
174    GenericParam,
175    Generics,
176    Gt,
177    Ident,
178    If,
179    Impl,
180    In,
181    Index,
182    LArrow,
183    Le,
184    Let,
185    Lifetime,
186    Loop,
187    Lt,
188    Match,
189    Member,
190    Meta,
191    MetaList,
192    MetaNameValue,
193    Mod,
194    Move,
195    StarEq,
196    Mut,
197    Ne,
198    Or,
199    OrEq,
200    OrOr,
201    Override,
202    ParenthesizedGenericArguments,
203    PathSegment,
204    Pound,
205    Priv,
206    Pub,
207    Question,
208    RArrow,
209    Ref,
210    Percent,
211    PercentEq,
212    Return,
213    ReturnType,
214    SelfType,
215    SelfValue,
216    Semi,
217    Shl,
218    ShlEq,
219    Shr,
220    ShrEq,
221    Star,
222    Static,
223    Struct,
224    Minus,
225    MinusEq,
226    Super,
227    Tilde,
228    Trait,
229    TraitBound,
230    TraitBoundModifier,
231    Try,
232    TypeArray,
233    TypeBareFn,
234    TypeGroup,
235    TypeImplTrait,
236    TypeInfer,
237    TypeMacro,
238    TypeNever,
239    TypeParam,
240    TypeParamBound,
241    TypeParen,
242    TypePath,
243    TypePtr,
244    TypeReference,
245    TypeSlice,
246    TypeTraitObject,
247    TypeTuple,
248    Typeof,
249    UnOp,
250    Underscore,
251    Union,
252    Unsafe,
253    Unsized,
254    Use,
255    Variant,
256    Virtual,
257    Visibility,
258    Where,
259    WhereClause,
260    WherePredicate,
261    While,
262    Yield,
263    syn::Macro,
264    syn::token::Box,
265    syn::token::Default,
266    syn::token::Macro,
267    syn::token::Type,
268];
269
270#[cfg(feature = "syn-full")]
271mod syn_full {
272    use syn::{
273        Arm, Block, ExprArray, ExprAssign, ExprAsync, ExprBinary, ExprBlock, ExprBreak, ExprCall,
274        ExprCast, ExprClosure, ExprContinue, ExprField, ExprForLoop, ExprIf, ExprIndex, ExprLet,
275        ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprParen, ExprPath, ExprRange,
276        ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple,
277        ExprUnary, ExprUnsafe, ExprWhile, ExprYield, FieldValue, File, FnArg, ForeignItem,
278        ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType, ImplItem,
279        ImplItemConst, ImplItemMacro, ImplItemType, Item, ItemConst, ItemEnum, ItemExternCrate,
280        ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait,
281        ItemTraitAlias, ItemType, ItemUnion, ItemUse, Label, RangeLimits, Receiver, Signature,
282        Stmt, TraitItem, TraitItemConst, TraitItemMacro, TraitItemType, UseTree,
283    };
284
285    use super::*;
286
287    impl_Attribute_for_Parse_and_ToTokens![
288        Arm,
289        Block,
290        ExprArray,
291        ExprAssign,
292        ExprAsync,
293        ExprBinary,
294        ExprBlock,
295        ExprBreak,
296        ExprCall,
297        ExprCast,
298        ExprClosure,
299        ExprContinue,
300        ExprField,
301        ExprForLoop,
302        ExprIf,
303        ExprIndex,
304        ExprLet,
305        ExprLit,
306        ExprLoop,
307        ExprMacro,
308        ExprMatch,
309        ExprMethodCall,
310        ExprParen,
311        ExprPath,
312        ExprRange,
313        ExprReference,
314        ExprRepeat,
315        ExprReturn,
316        ExprStruct,
317        ExprTry,
318        ExprTryBlock,
319        ExprTuple,
320        ExprUnary,
321        ExprUnsafe,
322        ExprWhile,
323        ExprYield,
324        FieldValue,
325        File,
326        FnArg,
327        ForeignItem,
328        ForeignItemFn,
329        ForeignItemMacro,
330        ForeignItemStatic,
331        ForeignItemType,
332        ImplItem,
333        ImplItemConst,
334        ImplItemMacro,
335        ImplItemType,
336        Item,
337        ItemConst,
338        ItemEnum,
339        ItemExternCrate,
340        ItemFn,
341        ItemForeignMod,
342        ItemImpl,
343        ItemMacro,
344        ItemMod,
345        ItemStatic,
346        ItemStruct,
347        ItemTrait,
348        ItemTraitAlias,
349        ItemType,
350        ItemUnion,
351        ItemUse,
352        Label,
353        RangeLimits,
354        Receiver,
355        Signature,
356        Stmt,
357        TraitItem,
358        TraitItemConst,
359        TraitItemMacro,
360        TraitItemType,
361        UseTree,
362    ];
363}