aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/src/lit.rs
blob: 1864d8fb76a6a148066fae134c911d3b5e4db84b (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use syn;
use proc_macro2;


pub fn lit_to_string(lit: &syn::Lit) -> Option<String> {
    match *lit {
        syn::Lit::Str(ref s) => Some(s.value()),
        _ => None,
    }
}

pub fn lit_to_int(lit: &syn::Lit) -> Option<u64> {
    match *lit {
        syn::Lit::Int(ref s) => Some(s.value()),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s) => Some(s.value().parse::<u64>().unwrap()),
        _ => None,
    }
}

pub fn lit_to_float(lit: &syn::Lit) -> Option<f64> {
    match *lit {
        syn::Lit::Float(ref s) => Some(s.value()),
        syn::Lit::Int(ref s) => Some(s.value() as f64),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s) => Some(s.value().parse::<f64>().unwrap()),
        _ => None,
    }
}

pub fn lit_to_bool(lit: &syn::Lit) -> Option<bool> {
    match *lit {
        syn::Lit::Bool(ref s) => Some(s.value),
        // TODO: remove when attr_literals is stable
        syn::Lit::Str(ref s) => if s.value() == "true" { Some(true) } else { Some(false) },
        _ => None,
    }
}

pub fn option_u64_to_tokens(opt: Option<u64>) -> proc_macro2::TokenStream {
    match opt {
        Some(ref t) => quote!(::std::option::Option::Some(#t)),
        None => quote!(::std::option::Option::None)
    }
}