about summary refs log tree commit diff
path: root/tvix/nix-compat-derive/src/ser.rs
blob: 52e777c28cf46fccf16a13800d9b677b884127e0 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::{DeriveInput, Generics, Path, Type};

use crate::internal::attrs::Default;
use crate::internal::inputs::RemoteInput;
use crate::internal::{attrs, Container, Context, Data, Field, Remote, Style, Variant};

pub fn expand_nix_serialize(crate_path: Path, input: &mut DeriveInput) -> syn::Result<TokenStream> {
    let cx = Context::new();
    let cont = Container::from_ast(&cx, crate_path, input);
    cx.check()?;
    let cont = cont.unwrap();

    let ty = cont.ident_type();
    let body = nix_serialize_body(&cont);
    let crate_path = cont.crate_path();

    Ok(nix_serialize_impl(
        crate_path,
        &ty,
        &cont.original.generics,
        body,
    ))
}

pub fn expand_nix_serialize_remote(
    crate_path: Path,
    input: &RemoteInput,
) -> syn::Result<TokenStream> {
    let cx = Context::new();
    let remote = Remote::from_ast(&cx, crate_path, input);
    if let Some(attrs) = remote.as_ref().map(|r| &r.attrs) {
        if attrs.display.is_none() && attrs.type_into.is_none() && attrs.type_try_into.is_none() {
            cx.error_spanned(input, "Missing into, try_into or display attribute");
        }
    }
    cx.check()?;
    let remote = remote.unwrap();

    let crate_path = remote.crate_path();
    let body = nix_serialize_body_into(crate_path, &remote.attrs).expect("From tokenstream");
    let generics = Generics::default();
    Ok(nix_serialize_impl(crate_path, remote.ty, &generics, body))
}

fn nix_serialize_impl(
    crate_path: &Path,
    ty: &Type,
    generics: &Generics,
    body: TokenStream,
) -> TokenStream {
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    quote! {
        #[automatically_derived]
        impl #impl_generics #crate_path::wire::ser::NixSerialize for #ty #ty_generics
            #where_clause
        {
            async fn serialize<W>(&self, writer: &mut W) -> std::result::Result<(), W::Error>
                where W: #crate_path::wire::ser::NixWrite
            {
                use #crate_path::wire::ser::Error as _;
                #body
            }
        }
    }
}

fn nix_serialize_body_into(
    crate_path: &syn::Path,
    attrs: &attrs::Container,
) -> Option<TokenStream> {
    if let Default::Default(span) = &attrs.display {
        Some(nix_serialize_display(span.span()))
    } else if let Default::Path(path) = &attrs.display {
        Some(nix_serialize_display_path(path))
    } else if let Some(type_into) = attrs.type_into.as_ref() {
        Some(nix_serialize_into(type_into))
    } else {
        attrs
            .type_try_into
            .as_ref()
            .map(|type_try_into| nix_serialize_try_into(crate_path, type_try_into))
    }
}

fn nix_serialize_body(cont: &Container) -> TokenStream {
    if let Some(tokens) = nix_serialize_body_into(cont.crate_path(), &cont.attrs) {
        tokens
    } else {
        match &cont.data {
            Data::Struct(_style, fields) => nix_serialize_struct(fields),
            Data::Enum(variants) => nix_serialize_enum(variants),
        }
    }
}

fn nix_serialize_struct(fields: &[Field<'_>]) -> TokenStream {
    let write_fields = fields.iter().map(|f| {
        let field = &f.member;
        let ty = f.ty;
        let write_value = quote_spanned! {
            ty.span()=> writer.write_value(&self.#field).await?
        };
        if let Some(version) = f.attrs.version.as_ref() {
            quote! {
                if (#version).contains(&writer.version().minor()) {
                    #write_value;
                }
            }
        } else {
            quote! {
                #write_value;
            }
        }
    });

    quote! {
        #(#write_fields)*
        Ok(())
    }
}

fn nix_serialize_variant(variant: &Variant<'_>) -> TokenStream {
    let ident = variant.ident;
    let write_fields = variant.fields.iter().map(|f| {
        let field = f.var_ident();
        let ty = f.ty;
        let write_value = quote_spanned! {
            ty.span()=> writer.write_value(#field).await?
        };
        if let Some(version) = f.attrs.version.as_ref() {
            quote! {
                if (#version).contains(&writer.version().minor()) {
                    #write_value;
                }
            }
        } else {
            quote! {
                #write_value;
            }
        }
    });
    let field_names = variant.fields.iter().map(|f| f.var_ident());
    let destructure = match variant.style {
        Style::Struct => {
            quote! {
                Self::#ident { #(#field_names),* }
            }
        }
        Style::Tuple => {
            quote! {
                Self::#ident(#(#field_names),*)
            }
        }
        Style::Unit => quote!(Self::#ident),
    };
    let ignore = match variant.style {
        Style::Struct => {
            quote! {
                Self::#ident { .. }
            }
        }
        Style::Tuple => {
            quote! {
                Self::#ident(_, ..)
            }
        }
        Style::Unit => quote!(Self::#ident),
    };
    let version = &variant.attrs.version;
    quote! {
        #destructure if (#version).contains(&writer.version().minor()) => {
            #(#write_fields)*
        }
        #ignore => {
            return Err(W::Error::invalid_enum(format!("{} is not valid for version {}", stringify!(#ident), writer.version())));
        }
    }
}

fn nix_serialize_enum(variants: &[Variant<'_>]) -> TokenStream {
    let match_variant = variants
        .iter()
        .map(|variant| nix_serialize_variant(variant));
    quote! {
        match self {
            #(#match_variant)*
        }
        Ok(())
    }
}

fn nix_serialize_into(ty: &Type) -> TokenStream {
    quote_spanned! {
        ty.span() =>
        {
            let other : #ty = <Self as Clone>::clone(self).into();
            writer.write_value(&other).await
        }
    }
}

fn nix_serialize_try_into(crate_path: &Path, ty: &Type) -> TokenStream {
    quote_spanned! {
        ty.span() =>
        {
            use #crate_path::wire::ser::Error;
            let other : #ty = <Self as Clone>::clone(self).try_into().map_err(Error::unsupported_data)?;
            writer.write_value(&other).await
        }
    }
}

fn nix_serialize_display(span: Span) -> TokenStream {
    quote_spanned! {
        span => writer.write_display(self).await
    }
}

fn nix_serialize_display_path(path: &syn::ExprPath) -> TokenStream {
    quote_spanned! {
        path.span() => writer.write_display(#path(self)).await
    }
}