]> git.proxmox.com Git - rustc.git/blame - vendor/quote-0.3.15/src/tokens.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / vendor / quote-0.3.15 / src / tokens.rs
CommitLineData
83c7162d
XL
1use super::ToTokens;
2use std::fmt::{self, Display};
3use std::str::FromStr;
4
5/// Tokens produced by a `quote!(...)` invocation.
6#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Tokens(String);
8
9impl Tokens {
10 /// Empty tokens.
11 pub fn new() -> Self {
12 Tokens(String::new())
13 }
14
15 /// For use by `ToTokens` implementations.
16 ///
17 /// ```
18 /// # #[macro_use] extern crate quote;
19 /// # use quote::{Tokens, ToTokens};
20 /// # fn main() {
21 /// struct X;
22 ///
23 /// impl ToTokens for X {
24 /// fn to_tokens(&self, tokens: &mut Tokens) {
25 /// tokens.append("a");
26 /// tokens.append("b");
27 /// tokens.append("c");
28 /// }
29 /// }
30 ///
31 /// let tokens = quote!(#X);
32 /// assert_eq!(tokens.as_str(), "a b c");
33 /// # }
34 /// ```
35 pub fn append<T: AsRef<str>>(&mut self, token: T) {
36 if !self.0.is_empty() && !token.as_ref().is_empty() {
37 self.0.push(' ');
38 }
39 self.0.push_str(token.as_ref());
40 }
41
42 /// For use by `ToTokens` implementations.
43 ///
44 /// ```
45 /// # #[macro_use] extern crate quote;
46 /// # use quote::{Tokens, ToTokens};
47 /// # fn main() {
48 /// struct X;
49 ///
50 /// impl ToTokens for X {
51 /// fn to_tokens(&self, tokens: &mut Tokens) {
52 /// tokens.append_all(&[true, false]);
53 /// }
54 /// }
55 ///
56 /// let tokens = quote!(#X);
57 /// assert_eq!(tokens.as_str(), "true false");
58 /// # }
59 /// ```
60 pub fn append_all<T, I>(&mut self, iter: I)
61 where T: ToTokens,
62 I: IntoIterator<Item = T>
63 {
64 for token in iter {
65 token.to_tokens(self);
66 }
67 }
68
69 /// For use by `ToTokens` implementations.
70 ///
71 /// ```
72 /// # #[macro_use] extern crate quote;
73 /// # use quote::{Tokens, ToTokens};
74 /// # fn main() {
75 /// struct X;
76 ///
77 /// impl ToTokens for X {
78 /// fn to_tokens(&self, tokens: &mut Tokens) {
79 /// tokens.append_separated(&[true, false], ",");
80 /// }
81 /// }
82 ///
83 /// let tokens = quote!(#X);
84 /// assert_eq!(tokens.as_str(), "true , false");
85 /// # }
86 /// ```
87 pub fn append_separated<T, I, S: AsRef<str>>(&mut self, iter: I, sep: S)
88 where T: ToTokens,
89 I: IntoIterator<Item = T>
90 {
91 for (i, token) in iter.into_iter().enumerate() {
92 if i > 0 {
93 self.append(sep.as_ref());
94 }
95 token.to_tokens(self);
96 }
97 }
98
99 /// For use by `ToTokens` implementations.
100 ///
101 /// ```
102 /// # #[macro_use] extern crate quote;
103 /// # use quote::{Tokens, ToTokens};
104 /// # fn main() {
105 /// struct X;
106 ///
107 /// impl ToTokens for X {
108 /// fn to_tokens(&self, tokens: &mut Tokens) {
109 /// tokens.append_terminated(&[true, false], ",");
110 /// }
111 /// }
112 ///
113 /// let tokens = quote!(#X);
114 /// assert_eq!(tokens.as_str(), "true , false ,");
115 /// # }
116 /// ```
117 pub fn append_terminated<T, I, S: AsRef<str>>(&mut self, iter: I, term: S)
118 where T: ToTokens,
119 I: IntoIterator<Item = T>
120 {
121 for token in iter {
122 token.to_tokens(self);
123 self.append(term.as_ref());
124 }
125 }
126
127 pub fn as_str(&self) -> &str {
128 &self.0
129 }
130
131 pub fn into_string(self) -> String {
132 self.0
133 }
134
135 pub fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
136 FromStr::from_str(&self.0)
137 }
138}
139
140impl Default for Tokens {
141 fn default() -> Self {
142 Tokens::new()
143 }
144}
145
146impl Display for Tokens {
147 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
148 self.0.fmt(formatter)
149 }
150}
151
152impl AsRef<str> for Tokens {
153 fn as_ref(&self) -> &str {
154 &self.0
155 }
156}