darling_core/util/
with_original.rs

1use 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)]
8pub struct WithOriginal<T, O> {
9    pub parsed: T,
10    pub original: O,
11}
12
13impl<T, O> WithOriginal<T, O> {
14    pub fn new(parsed: T, original: O) -> Self {
15        WithOriginal { parsed, original }
16    }
17}
18
19macro_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
29with_original!(FromDeriveInput, from_derive_input, syn::DeriveInput);
30with_original!(FromField, from_field, syn::Field);
31with_original!(FromGenerics, from_generics, syn::Generics);
32with_original!(FromGenericParam, from_generic_param, syn::GenericParam);
33with_original!(FromMeta, from_meta, syn::Meta);
34with_original!(FromTypeParam, from_type_param, syn::TypeParam);
35with_original!(FromVariant, from_variant, syn::Variant);