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
use syn::{Error, Lit, Meta, Result};

/// a trait for extracting a value from `Option<syn::Meta>`
pub trait FromMeta: Sized {
    /// extract the value
    ///
    /// # Errors
    /// may return an Error if the meta doesn't contain the correct value
    fn from(meta: Option<Meta>) -> Result<Self>;
}

impl FromMeta for Option<()> {
    fn from(meta: Option<Meta>) -> Result<Self> {
        meta.map_or(Ok(None), |m| {
            if matches!(m, Meta::Path(_)) {
                Ok(Some(()))
            } else {
                Err(Error::new_spanned(m, "unexpected value"))
            }
        })
    }
}

/// a trait for extracting a value from a literal
///
/// [`FromMeta`] is automatically implemented for all implementations
pub trait FromLit: Sized {
    /// extract the value
    ///
    /// # Errors
    /// may return an Error if the literal doesn't contain the correct value
    fn from(lit: Option<Lit>) -> Result<Self>;
}

impl<F: FromLit> FromMeta for F {
    fn from(meta: Option<Meta>) -> Result<Self> {
        let lit = meta
            .map(|m| match m {
                Meta::NameValue(mnv) => Ok(mnv.lit),
                _ => Err(Error::new_spanned(m, "expected named value")),
            })
            .transpose()?;
        Self::from(lit)
    }
}

impl FromLit for Option<Lit> {
    fn from(lit: Option<Lit>) -> Result<Self> {
        Ok(lit)
    }
}

impl FromLit for String {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Str(v)) = lit {
            Ok(v.value())
        } else {
            Err(Error::new_spanned(lit, "expected string"))
        }
    }
}

impl FromLit for Vec<u8> {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::ByteStr(v)) = lit {
            Ok(v.value())
        } else {
            Err(Error::new_spanned(lit, "expected bytes"))
        }
    }
}

impl FromLit for u8 {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Byte(v)) = lit {
            Ok(v.value())
        } else {
            Err(Error::new_spanned(lit, "expected byte"))
        }
    }
}

impl FromLit for char {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Char(v)) = lit {
            Ok(v.value())
        } else {
            Err(Error::new_spanned(lit, "expected char"))
        }
    }
}

impl FromLit for i32 {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Int(v)) = &lit {
            v.base10_parse()
        } else {
            Err(Error::new_spanned(lit, "expected i32"))
        }
    }
}

impl FromLit for f32 {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Float(v)) = &lit {
            v.base10_parse()
        } else {
            Err(Error::new_spanned(lit, "expected f32"))
        }
    }
}

impl FromLit for bool {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if let Some(Lit::Bool(v)) = lit {
            Ok(v.value)
        } else {
            Err(Error::new_spanned(lit, "expected bool"))
        }
    }
}

impl<V: FromLit> FromLit for Option<V> {
    fn from(lit: Option<Lit>) -> Result<Self> {
        if lit.is_some() {
            Some(V::from(lit)).transpose()
        } else {
            Ok(None)
        }
    }
}