check operator and define common method to get attribute value
This commit is contained in:
parent
c8f3dffec1
commit
f5a0a53d27
@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
|
use proc_macro2::TokenTreeIter;
|
||||||
use proc_macro2::TokenNode::*;
|
use proc_macro2::TokenNode::*;
|
||||||
|
use proc_macro2::Spacing;
|
||||||
use proc_macro2::Delimiter::Parenthesis;
|
use proc_macro2::Delimiter::Parenthesis;
|
||||||
use syn::Attribute;
|
use syn::Attribute;
|
||||||
|
|
||||||
@ -11,7 +13,22 @@ pub struct YaSerdeAttribute {
|
|||||||
pub text: bool,
|
pub text: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_value(iter: &mut TokenTreeIter) -> Option<String> {
|
||||||
|
match (iter.next(), iter.next()) {
|
||||||
|
(Some(operator), Some(value)) => {
|
||||||
|
match (operator.kind, value.kind) {
|
||||||
|
(Op('=', Spacing::Alone), Literal(l)) => {
|
||||||
|
Some(l.to_string().replace("\"", ""))
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl YaSerdeAttribute {
|
impl YaSerdeAttribute {
|
||||||
|
|
||||||
pub fn parse(attrs: &Vec<Attribute>) -> YaSerdeAttribute {
|
pub fn parse(attrs: &Vec<Attribute>) -> YaSerdeAttribute {
|
||||||
|
|
||||||
let mut root = None;
|
let mut root = None;
|
||||||
@ -32,26 +49,10 @@ impl YaSerdeAttribute {
|
|||||||
Term(t) => {
|
Term(t) => {
|
||||||
match t.as_str() {
|
match t.as_str() {
|
||||||
"root" => {
|
"root" => {
|
||||||
attr_iter.next();
|
root = get_value(&mut attr_iter);
|
||||||
let v = attr_iter.next().map(|s|
|
|
||||||
match s.kind {
|
|
||||||
Literal(l) => {
|
|
||||||
Some(l.to_string().replace("\"", ""))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
});
|
|
||||||
root = v.unwrap_or(None);
|
|
||||||
},
|
},
|
||||||
"rename" => {
|
"rename" => {
|
||||||
attr_iter.next();
|
rename = get_value(&mut attr_iter);
|
||||||
let v = attr_iter.next().map(|s|
|
|
||||||
match s.kind {
|
|
||||||
Literal(l) => {
|
|
||||||
Some(l.to_string().replace("\"", ""))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
});
|
|
||||||
rename = v.unwrap_or(None);
|
|
||||||
},
|
},
|
||||||
"attribute" => {
|
"attribute" => {
|
||||||
attribute = true;
|
attribute = true;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user