about summary refs log tree commit diff
path: root/tvix/nix-compat-derive/src/internal/mod.rs
blob: 20b2432216191008afa8438361f3fe3877fe1d5e (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
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Token;

pub mod attrs;
mod ctx;
pub mod inputs;
mod symbol;

pub use ctx::Context;

pub struct Field<'a> {
    pub member: syn::Member,
    pub ty: &'a syn::Type,
    pub attrs: attrs::Field,
    pub original: &'a syn::Field,
}

impl<'a> Field<'a> {
    pub fn from_ast(ctx: &Context, idx: usize, field: &'a syn::Field) -> Field<'a> {
        let attrs = attrs::Field::from_ast(ctx, &field.attrs);
        let member = match &field.ident {
            Some(id) => syn::Member::Named(id.clone()),
            None => syn::Member::Unnamed(idx.into()),
        };
        Field {
            member,
            attrs,
            ty: &field.ty,
            original: field,
        }
    }

    pub fn var_ident(&self) -> syn::Ident {
        match &self.member {
            syn::Member::Named(name) => name.clone(),
            syn::Member::Unnamed(idx) => {
                syn::Ident::new(&format!("field{}", idx.index), self.original.span())
            }
        }
    }
}

pub struct Variant<'a> {
    pub ident: &'a syn::Ident,
    pub attrs: attrs::Variant,
    pub style: Style,
    pub fields: Vec<Field<'a>>,
    //pub original: &'a syn::Variant,
}

impl<'a> Variant<'a> {
    pub fn from_ast(ctx: &Context, variant: &'a syn::Variant) -> Self {
        let attrs = attrs::Variant::from_ast(ctx, &variant.attrs);
        let (style, fields) = match &variant.fields {
            syn::Fields::Named(fields) => (Style::Struct, fields_ast(ctx, &fields.named)),
            syn::Fields::Unnamed(fields) => (Style::Tuple, fields_ast(ctx, &fields.unnamed)),
            syn::Fields::Unit => (Style::Unit, Vec::new()),
        };
        Variant {
            ident: &variant.ident,
            attrs,
            style,
            fields,
            //original: variant,
        }
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum Style {
    Struct,
    Tuple,
    Unit,
}

pub enum Data<'a> {
    Enum(Vec<Variant<'a>>),
    Struct(Style, Vec<Field<'a>>),
}

pub struct Container<'a> {
    pub ident: &'a syn::Ident,
    pub attrs: attrs::Container,
    pub data: Data<'a>,
    pub crate_path: syn::Path,
    pub original: &'a syn::DeriveInput,
}

impl<'a> Container<'a> {
    pub fn from_ast(
        ctx: &Context,
        crate_path: syn::Path,
        input: &'a mut syn::DeriveInput,
    ) -> Option<Container<'a>> {
        let attrs = attrs::Container::from_ast(ctx, &input.attrs);
        let data = match &input.data {
            syn::Data::Struct(s) => match &s.fields {
                syn::Fields::Named(fields) => {
                    Data::Struct(Style::Struct, fields_ast(ctx, &fields.named))
                }
                syn::Fields::Unnamed(fields) => {
                    Data::Struct(Style::Tuple, fields_ast(ctx, &fields.unnamed))
                }
                syn::Fields::Unit => Data::Struct(Style::Unit, Vec::new()),
            },
            syn::Data::Enum(e) => {
                let variants = e
                    .variants
                    .iter()
                    .map(|variant| Variant::from_ast(ctx, variant))
                    .collect();
                Data::Enum(variants)
            }
            syn::Data::Union(u) => {
                ctx.error_spanned(u.union_token, "Union not supported by nixrs");
                return None;
            }
        };
        Some(Container {
            ident: &input.ident,
            attrs,
            data,
            crate_path,
            original: input,
        })
    }

    pub fn crate_path(&self) -> &syn::Path {
        if let Some(crate_path) = self.attrs.crate_path.as_ref() {
            crate_path
        } else {
            &self.crate_path
        }
    }

    pub fn ident_type(&self) -> syn::Type {
        let path: syn::Path = self.ident.clone().into();
        let tp = syn::TypePath { qself: None, path };
        tp.into()
    }
}

pub struct Remote<'a> {
    pub attrs: attrs::Container,
    pub ty: &'a syn::Type,
    pub crate_path: syn::Path,
}

impl<'a> Remote<'a> {
    pub fn from_ast(
        ctx: &Context,
        crate_path: syn::Path,
        input: &'a inputs::RemoteInput,
    ) -> Option<Remote<'a>> {
        let attrs = attrs::Container::from_ast(ctx, &input.attrs);
        if attrs.from_str.is_none() && attrs.type_from.is_none() && attrs.type_try_from.is_none() {
            ctx.error_spanned(input, "Missing from_str, from or try_from attribute");
            return None;
        }
        Some(Remote {
            ty: &input.ident,
            attrs,
            crate_path,
        })
    }

    pub fn crate_path(&self) -> &syn::Path {
        if let Some(crate_path) = self.attrs.crate_path.as_ref() {
            crate_path
        } else {
            &self.crate_path
        }
    }
}

fn fields_ast<'a>(ctx: &Context, fields: &'a Punctuated<syn::Field, Token![,]>) -> Vec<Field<'a>> {
    fields
        .iter()
        .enumerate()
        .map(|(idx, field)| Field::from_ast(ctx, idx, field))
        .collect()
}