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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::ToTokens;
use std::fmt::{self, Display};
use std::str::FromStr;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Tokens(String);
impl Tokens {
pub fn new() -> Self {
Tokens(String::new())
}
pub fn append<T: AsRef<str>>(&mut self, token: T) {
if !self.0.is_empty() && !token.as_ref().is_empty() {
self.0.push(' ');
}
self.0.push_str(token.as_ref());
}
pub fn append_all<T, I>(&mut self, iter: I)
where T: ToTokens,
I: IntoIterator<Item = T>
{
for token in iter {
token.to_tokens(self);
}
}
pub fn append_separated<T, I, S: AsRef<str>>(&mut self, iter: I, sep: S)
where T: ToTokens,
I: IntoIterator<Item = T>
{
for (i, token) in iter.into_iter().enumerate() {
if i > 0 {
self.append(sep.as_ref());
}
token.to_tokens(self);
}
}
pub fn append_terminated<T, I, S: AsRef<str>>(&mut self, iter: I, term: S)
where T: ToTokens,
I: IntoIterator<Item = T>
{
for token in iter {
token.to_tokens(self);
self.append(term.as_ref());
}
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
pub fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
FromStr::from_str(&self.0)
}
}
impl Default for Tokens {
fn default() -> Self {
Tokens::new()
}
}
impl Display for Tokens {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.0.fmt(formatter)
}
}
impl AsRef<str> for Tokens {
fn as_ref(&self) -> &str {
&self.0
}
}