diff options
| author | Vincent Prouillet | 2017-07-19 16:30:01 +0900 |
|---|---|---|
| committer | GitHub | 2017-07-19 16:30:01 +0900 |
| commit | 2ab1a0fa0fe8fe9ccc025cbd7228e6b8201b1f1f (patch) | |
| tree | e71b13aea39c61b19e8aec09468a8e5c4a291e16 /validator_derive/src/lit.rs | |
| parent | 69e35d6cc905b9d7a894af7e486237e376fae939 (diff) | |
| parent | b31fd25cc5cec96ccee737ba1313c6e9f702f32a (diff) | |
| download | validator-2ab1a0fa0fe8fe9ccc025cbd7228e6b8201b1f1f.tar.bz2 | |
Merge pull request #27 from Keats/revamp
Complete refactor
Diffstat (limited to 'validator_derive/src/lit.rs')
| -rw-r--r-- | validator_derive/src/lit.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/validator_derive/src/lit.rs b/validator_derive/src/lit.rs new file mode 100644 index 0000000..0753a17 --- /dev/null +++ b/validator_derive/src/lit.rs @@ -0,0 +1,61 @@ +use quote::{self, ToTokens}; +use syn; + + +pub fn lit_to_string(lit: &syn::Lit) -> Option<String> { + match *lit { + syn::Lit::Str(ref s, _) => Some(s.to_string()), + _ => None, + } +} + +pub fn lit_to_int(lit: &syn::Lit) -> Option<u64> { + match *lit { + syn::Lit::Int(ref s, _) => Some(*s), + // TODO: remove when attr_literals is stable + syn::Lit::Str(ref s, _) => Some(s.parse::<u64>().unwrap()), + _ => None, + } +} + +pub fn lit_to_float(lit: &syn::Lit) -> Option<f64> { + match *lit { + syn::Lit::Float(ref s, _) => Some(s.parse::<f64>().unwrap()), + syn::Lit::Int(ref s, _) => Some(*s as f64), + // TODO: remove when attr_literals is stable + syn::Lit::Str(ref s, _) => Some(s.parse::<f64>().unwrap()), + _ => None, + } +} + +pub fn lit_to_bool(lit: &syn::Lit) -> Option<bool> { + match *lit { + syn::Lit::Bool(ref s) => Some(*s), + // TODO: remove when attr_literals is stable + syn::Lit::Str(ref s, _) => if s == "true" { Some(true) } else { Some(false) }, + _ => None, + } +} + +pub fn option_u64_to_tokens(opt: Option<u64>) -> quote::Tokens { + let mut tokens = quote::Tokens::new(); + tokens.append("::"); + tokens.append("std"); + tokens.append("::"); + tokens.append("option"); + tokens.append("::"); + tokens.append("Option"); + tokens.append("::"); + match opt { + Some(ref t) => { + tokens.append("Some"); + tokens.append("("); + t.to_tokens(&mut tokens); + tokens.append(")"); + } + None => { + tokens.append("None"); + } + } + tokens +} |
