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
use crate::Symbol;
use proc_macro2::TokenStream;
use std::fmt::Display;
use std::str::FromStr;
use syn::punctuated::Punctuated;
use syn::{
    Attribute, Error, GenericArgument, Lit, Meta, PathArguments, PathSegment, Type, TypePath,
};

#[inline]
pub fn unwrap_punctuated_first<T, P>(
    punctuated: &Punctuated<T, P>,
    error: Error,
) -> Result<&T, Error> {
    match punctuated.first() {
        Some(s) => Ok(s),
        None => Err(error),
    }
}

#[inline]
pub fn get_nested_type<'a>(
    segment: &'a PathSegment,
    message: &'static str,
) -> Result<&'a Type, Error> {
    let error = Error::new_spanned(segment, message);
    match &segment.arguments {
        PathArguments::AngleBracketed(argument) => {
            match unwrap_punctuated_first(&argument.args, error.clone())? {
                GenericArgument::Type(nested_type) => Ok(nested_type),
                _ => Err(error),
            }
        }
        _ => Err(error),
    }
}

pub fn get_nested_types<'a>(
    segment: &'a PathSegment,
    message: &'static str,
) -> Result<Vec<&'a Type>, Error> {
    let error = Error::new_spanned(segment, message);
    match &segment.arguments {
        PathArguments::AngleBracketed(arguments) => arguments
            .args
            .iter()
            .map(|argument| match argument {
                GenericArgument::Type(nested_type) => Ok(nested_type),
                _ => Err(error.clone()),
            })
            .collect(),
        _ => Err(error),
    }
}

#[inline]
pub fn unwrap_type_path<'a>(ty: &'a Type, message: &'static str) -> Result<&'a TypePath, Error> {
    match ty {
        Type::Path(type_path) => Ok(type_path),
        _ => Err(Error::new_spanned(ty, message)),
    }
}

#[inline]
pub fn get_lit_str<U: Display>(lit: &Lit, ident: &U) -> Result<String, Error> {
    match lit {
        Lit::Str(lit_str) => Ok(lit_str.value()),
        _ => Err(Error::new_spanned(
            lit,
            format!("expected {} lit to be a string", ident),
        )),
    }
}

#[inline]
pub fn get_lit_as_string<U: Display>(lit: &Lit, ident: &U) -> Result<String, Error> {
    match lit {
        Lit::Str(lit_str) => Ok(lit_str.value()),
        Lit::Int(lit_int) => Ok(lit_int.to_string()),
        Lit::Float(lit_float) => Ok(lit_float.to_string()),
        Lit::Bool(lit_bool) => Ok(lit_bool.value.to_string()),
        _ => Err(Error::new_spanned(
            lit,
            format!("expected {} lit to be a string/integer/float/boll", ident),
        )),
    }
}

#[inline]
pub fn get_lit_int<T: FromStr, U: Display>(lit: &Lit, ident: &U) -> Result<T, Error>
where
    <T as std::str::FromStr>::Err: std::fmt::Display,
{
    match lit {
        Lit::Int(lit_int) => Ok(lit_int.base10_parse().unwrap()),
        _ => Err(Error::new_spanned(
            lit,
            format!("expected {} lit to be a integer", ident),
        )),
    }
}

#[inline]
pub fn get_lit_float<T: FromStr, U: Display>(lit: &Lit, ident: &U) -> Result<T, Error>
where
    <T as std::str::FromStr>::Err: std::fmt::Display,
{
    match lit {
        Lit::Float(lit_float) => Ok(lit_float.base10_parse().unwrap()),
        _ => Err(Error::new_spanned(
            lit,
            format!("expected {} lit to be a float", ident),
        )),
    }
}

#[inline]
pub fn get_lit_bool<U: Display>(lit: &Lit, ident: &U) -> Result<bool, Error> {
    match lit {
        Lit::Bool(lit_bool) => Ok(lit_bool.value),
        _ => Err(Error::new_spanned(
            lit,
            format!("expected {} lit to be a bool", ident),
        )),
    }
}

pub fn get_mod_path(attrs: &[Attribute]) -> Result<Option<TokenStream>, Error> {
    let mut mod_path = None;
    for attr in attrs.iter() {
        if attr.path == Symbol::new("mod_path") {
            let meta = attr.parse_meta()?;
            mod_path = match &meta {
                Meta::NameValue(mod_path_value) => {
                    let mod_path_str = get_lit_str(
                        &mod_path_value.lit,
                        mod_path_value.path.get_ident().as_ref().unwrap(),
                    )?;

                    Some(
                        TokenStream::from_str(mod_path_str.as_str())
                            .map_err(|_| Error::new_spanned(&meta, "Invalid mod_path"))?,
                    )
                }
                _ => None,
            }
        }
    }

    Ok(mod_path)
}