-
Notifications
You must be signed in to change notification settings - Fork 139
/
meta_helpers.rs
63 lines (55 loc) · 1.81 KB
/
meta_helpers.rs
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
use syn::{Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path};
pub trait MetaHelpers {
fn expect_metalist(&self, msg: &str) -> syn::Result<&MetaList>;
fn expect_path(&self, msg: &str) -> syn::Result<&Path>;
fn expect_namevalue(&self, msg: &str) -> syn::Result<&MetaNameValue>;
}
impl MetaHelpers for Meta {
fn expect_metalist(&self, msg: &str) -> syn::Result<&MetaList> {
match self {
Meta::List(list) => Ok(list),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
fn expect_path(&self, msg: &str) -> syn::Result<&Path> {
match self {
Meta::Path(path) => Ok(path),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
fn expect_namevalue(&self, msg: &str) -> syn::Result<&MetaNameValue> {
match self {
Meta::NameValue(pair) => Ok(pair),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
}
pub trait NestedMetaHelpers {
fn expect_meta(&self, msg: &str) -> syn::Result<&Meta>;
fn expect_lit(&self, msg: &str) -> syn::Result<&Lit>;
}
impl NestedMetaHelpers for NestedMeta {
fn expect_meta(&self, msg: &str) -> syn::Result<&Meta> {
match self {
NestedMeta::Meta(m) => Ok(m),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
fn expect_lit(&self, msg: &str) -> syn::Result<&Lit> {
match self {
NestedMeta::Lit(l) => Ok(l),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
}
pub trait LitHelpers {
fn expect_string(&self, msg: &str) -> syn::Result<String>;
}
impl LitHelpers for Lit {
fn expect_string(&self, msg: &str) -> syn::Result<String> {
match self {
Lit::Str(s) => Ok(s.value()),
_ => Err(syn::Error::new_spanned(self, msg)),
}
}
}