1 | use crate::{ |
2 | FromDeriveInput, FromField, FromGenericParam, FromGenerics, FromMeta, FromTypeParam, |
3 | FromVariant, Result, |
4 | }; |
5 | |
6 | /// A container to parse some syntax and retain access to the original. |
7 | #[derive (Debug, Clone, PartialEq, Eq)] |
8 | pub struct WithOriginal<T, O> { |
9 | pub parsed: T, |
10 | pub original: O, |
11 | } |
12 | |
13 | impl<T, O> WithOriginal<T, O> { |
14 | pub fn new(parsed: T, original: O) -> Self { |
15 | WithOriginal { parsed, original } |
16 | } |
17 | } |
18 | |
19 | macro_rules! with_original { |
20 | ($trayt:ident, $func:ident, $syn:path) => { |
21 | impl<T: $trayt> $trayt for WithOriginal<T, $syn> { |
22 | fn $func(value: &$syn) -> Result<Self> { |
23 | Ok(WithOriginal::new($trayt::$func(value)?, value.clone())) |
24 | } |
25 | } |
26 | }; |
27 | } |
28 | |
29 | with_original!(FromDeriveInput, from_derive_input, syn::DeriveInput); |
30 | with_original!(FromField, from_field, syn::Field); |
31 | with_original!(FromGenerics, from_generics, syn::Generics); |
32 | with_original!(FromGenericParam, from_generic_param, syn::GenericParam); |
33 | with_original!(FromMeta, from_meta, syn::Meta); |
34 | with_original!(FromTypeParam, from_type_param, syn::TypeParam); |
35 | with_original!(FromVariant, from_variant, syn::Variant); |
36 | |