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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use proc_macro2::TokenStream;
use quote::{TokenStreamExt, ToTokens};
use syn::{Ident, Path};
const DEFAULT_STRUCT_NAME: &str = "__default";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefaultExpression<'a> {
Inherit(&'a Ident),
Explicit(&'a Path),
Trait,
}
impl<'a> DefaultExpression<'a> {
pub fn as_declaration(&'a self) -> DefaultDeclaration<'a> {
DefaultDeclaration(self)
}
}
impl<'a> ToTokens for DefaultExpression<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(match *self {
DefaultExpression::Inherit(ident) => {
let dsn = Ident::new(DEFAULT_STRUCT_NAME, ::proc_macro2::Span::call_site());
quote!(#dsn.#ident)
}
DefaultExpression::Explicit(path) => quote!(#path()),
DefaultExpression::Trait => quote!(::darling::export::Default::default()),
});
}
}
pub struct DefaultDeclaration<'a>(&'a DefaultExpression<'a>);
impl<'a> ToTokens for DefaultDeclaration<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = Ident::new(DEFAULT_STRUCT_NAME, ::proc_macro2::Span::call_site());
let expr = self.0;
tokens.append_all(quote!(let #name: Self = #expr;));
}
}