darling_core/codegen/
postfix_transform.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{Ident, Path};

/// A method invocation applied to a value.
///
/// This is used for `map` and `and_then` transforms in derivations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PostfixTransform {
    pub(crate) transformer: Ident,
    pub(crate) function: Path,
}

impl PostfixTransform {
    pub fn new(transformer: Ident, function: Path) -> Self {
        Self {
            transformer,
            function,
        }
    }
}

impl ToTokens for PostfixTransform {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let Self {
            transformer,
            function,
        } = self;
        tokens.append_all(quote!(.#transformer(#function)))
    }
}