aboutsummaryrefslogtreecommitdiff
path: root/alacritty_config_derive/src/lib.rs
blob: af8f2e7f31123d15caa6178885701a3de78fed05 (plain)
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
#![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use)]
#![cfg_attr(feature = "cargo-clippy", deny(warnings))]

use proc_macro::TokenStream;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Error, Fields, Path};

mod de_enum;
mod de_struct;

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

#[proc_macro_derive(ConfigDeserialize, attributes(config))]
pub fn derive_config_deserialize(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, data_enum),
        _ => Error::new(input.ident.span(), UNSUPPORTED_ERROR).to_compile_error().into(),
    }
}

/// Verify that a token path ends with a specific segment.
pub(crate) fn path_ends_with(path: &Path, segment: &str) -> bool {
    let segments = path.segments.iter();
    segments.last().map_or(false, |s| s.ident == segment)
}