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
use super::*;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Attribute {
pub value: MetaItem,
pub is_sugared_doc: bool,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MetaItem {
Word(Ident),
List(Ident, Vec<MetaItem>),
NameValue(Ident, String),
}
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
use helper::escaped_string;
use ident::parsing::ident;
use nom::multispace;
named!(pub attribute<&str, Attribute>, alt_complete!(
do_parse!(
punct!("#") >>
punct!("[") >>
meta_item: meta_item >>
punct!("]") >>
(Attribute {
value: meta_item,
is_sugared_doc: false,
})
)
|
do_parse!(
punct!("///") >>
space: multispace >>
content: take_until_s!("\n") >>
(Attribute {
value: MetaItem::NameValue(
"doc".into(),
format!("///{}{}", space, content),
),
is_sugared_doc: true,
})
)
));
named!(meta_item<&str, MetaItem>, alt_complete!(
do_parse!(
id: ident >>
punct!("(") >>
inner: separated_list!(punct!(","), meta_item) >>
punct!(")") >>
(MetaItem::List(id, inner))
)
|
do_parse!(
id: ident >>
punct!("=") >>
string: quoted >>
(MetaItem::NameValue(id, string))
)
|
map!(ident, MetaItem::Word)
));
named!(quoted<&str, String>, delimited!(
punct!("\""),
escaped_string,
tag_s!("\"")
));
}
#[cfg(feature = "printing")]
mod printing {
use super::*;
use quote::{Tokens, ToTokens};
impl ToTokens for Attribute {
fn to_tokens(&self, tokens: &mut Tokens) {
tokens.append("#");
tokens.append("[");
self.value.to_tokens(tokens);
tokens.append("]");
}
}
impl ToTokens for MetaItem {
fn to_tokens(&self, tokens: &mut Tokens) {
match *self {
MetaItem::Word(ref ident) => {
ident.to_tokens(tokens);
}
MetaItem::List(ref ident, ref inner) => {
ident.to_tokens(tokens);
tokens.append("(");
tokens.append_separated(inner, ",");
tokens.append(")");
}
MetaItem::NameValue(ref name, ref value) => {
name.to_tokens(tokens);
tokens.append("=");
value.to_tokens(tokens);
}
}
}
}
}