rune_macros/
to_value.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::context::{Context, Tokens};
use proc_macro2::TokenStream;
use quote::quote;

struct Expander<'cx> {
    cx: &'cx Context,
    tokens: Tokens,
}

impl Expander<'_> {
    /// Expand on a struct.
    fn expand_struct(
        &mut self,
        input: &syn::DeriveInput,
        st: &syn::DataStruct,
    ) -> Result<TokenStream, ()> {
        let inner = self.expand_fields(&st.fields)?;

        let ident = &input.ident;

        let Tokens {
            value,
            to_value,
            result,
            runtime_error,
            ..
        } = &self.tokens;

        Ok(quote! {
            #[automatically_derived]
            impl #to_value for #ident {
                fn to_value(self) -> #result<#value, #runtime_error> {
                    #inner
                }
            }
        })
    }

    /// Expand field decoding.
    fn expand_fields(&mut self, fields: &syn::Fields) -> Result<TokenStream, ()> {
        match fields {
            syn::Fields::Unnamed(named) => self.expand_unnamed(named),
            syn::Fields::Named(named) => self.expand_named(named),
            syn::Fields::Unit => {
                self.cx.error(syn::Error::new_spanned(
                    fields,
                    "unit structs are not supported",
                ));
                Err(())
            }
        }
    }

    /// Expand unnamed fields.
    fn expand_unnamed(&mut self, unnamed: &syn::FieldsUnnamed) -> Result<TokenStream, ()> {
        let mut to_values = Vec::new();

        let Tokens {
            to_value,
            value,
            owned_tuple,
            result,
            try_from,
            vec,
            ..
        } = &self.tokens;

        for (index, f) in unnamed.unnamed.iter().enumerate() {
            _ = self.cx.field_attrs(&f.attrs);
            let index = syn::Index::from(index);
            to_values.push(quote!(#vec::try_push(&mut tuple, #to_value::to_value(self.#index)?)?));
        }

        let cap = unnamed.unnamed.len();

        Ok(quote! {
            let mut tuple = #vec::try_with_capacity(#cap)?;
            #(#to_values;)*
            let tuple = <#owned_tuple as #try_from<_>>::try_from(tuple)?;
            #result::Ok(<#value as #try_from<_>>::try_from(tuple)?)
        })
    }

    /// Expand named fields.
    fn expand_named(&mut self, named: &syn::FieldsNamed) -> Result<TokenStream, ()> {
        let Tokens {
            to_value,
            value,
            object,
            result,
            string,
            try_from,
            ..
        } = &self.tokens;

        let mut to_values = Vec::new();

        for f in &named.named {
            let ident = self.cx.field_ident(f)?;
            _ = self.cx.field_attrs(&f.attrs);

            let name = syn::LitStr::new(&ident.to_string(), ident.span());

            to_values.push(quote! {
                object.insert(<#string as #try_from<_>>::try_from(#name)?, #to_value::to_value(self.#ident)?)?
            });
        }

        Ok(quote! {
            let mut object = <#object>::new();
            #(#to_values;)*
            #result::Ok(<#value as #try_from<_>>::try_from(object)?)
        })
    }
}

pub(super) fn expand(cx: &Context, input: &syn::DeriveInput) -> Result<TokenStream, ()> {
    let attr = cx.type_attrs(&input.attrs);
    let tokens = cx.tokens_with_module(attr.module.as_ref());

    let mut expander = Expander { cx, tokens };

    match &input.data {
        syn::Data::Struct(st) => {
            if let Ok(expanded) = expander.expand_struct(input, st) {
                return Ok(expanded);
            }
        }
        syn::Data::Enum(en) => {
            expander.cx.error(syn::Error::new_spanned(
                en.enum_token,
                "not supported on enums",
            ));
        }
        syn::Data::Union(un) => {
            expander.cx.error(syn::Error::new_spanned(
                un.union_token,
                "not supported on unions",
            ));
        }
    }

    Err(())
}