aboutsummaryrefslogtreecommitdiff
path: root/alacritty_config_derive/src/config_deserialize/mod.rs
blob: b19233775b04daea52842295ee578e75d7e51d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use proc_macro::TokenStream;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Error, Fields};

/// Error if the derive was used on an unsupported type.
const UNSUPPORTED_ERROR: &str = "ConfigDeserialize must be used on an enum or struct with fields";

mod de_enum;
mod de_struct;

pub fn derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    match input.data {
        Data::Struct(DataStruct { fields: Fields::Named(fields), .. }) => {
            de_struct::derive_deserialize(input.ident, input.generics, fields.named)
        },
        Data::Enum(data_enum) => {
            de_enum::derive_deserialize(input.ident, input.generics, data_enum)
        },
        _ => Error::new(input.ident.span(), UNSUPPORTED_ERROR).to_compile_error().into(),
    }
}