Skip to main content

spyne_syntax/
tok_gen.rs

1use std::{borrow::Cow, collections::{BTreeMap, BTreeSet, HashMap, HashSet}, marker::PhantomData};
2
3use crate::token::{Delimiter, Span, TokenTree};
4
5pub trait ToTokens {
6    fn to_tokens(&self, tokens: &mut Vec<TokenTree>);
7}
8impl ToTokens for TokenTree {
9    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
10        tokens.push(self.to_owned());
11    }
12}
13impl ToTokens for u8 {
14    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
15        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
16    }
17}
18impl ToTokens for u16 {
19    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
20        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
21    }
22}
23impl ToTokens for u32 {
24    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
25        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
26    }
27}
28impl ToTokens for u64 {
29    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
30        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
31    }
32}
33impl ToTokens for u128 {
34    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
35        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
36    }
37}
38impl ToTokens for usize {
39    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
40        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
41    }
42}
43impl ToTokens for i8 {
44    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
45        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
46    }
47}
48impl ToTokens for i16 {
49    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
50        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
51    }
52}
53impl ToTokens for i32 {
54    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
55        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
56    }
57}
58impl ToTokens for i64 {
59    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
60        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
61    }
62}
63impl ToTokens for i128 {
64    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
65        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
66    }
67}
68impl ToTokens for isize {
69    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
70        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
71    }
72}
73impl ToTokens for f32 {
74    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
75        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
76    }
77}
78impl ToTokens for f64 {
79    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
80        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
81    }
82}
83impl ToTokens for bool {
84    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
85        tokens.push(TokenTree::Literal(format!("{}", self), Span::default()));
86    }
87}
88impl ToTokens for char {
89    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
90        tokens.push(TokenTree::Literal(format!("{:?}", self), Span::default()));
91    }
92}
93impl<T: ToTokens> ToTokens for &[T] {
94    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
95        for item in *self {
96            item.to_tokens(tokens);
97        }
98    }
99}
100impl<T: ToTokens, const N: usize> ToTokens for &[T; N] {
101    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
102        for item in *self {
103            item.to_tokens(tokens);
104        }
105    }
106}
107impl ToTokens for str {
108    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
109        tokens.push(TokenTree::Literal(format!("{:?}", self), Span::default()));
110    }
111}
112impl ToTokens for &str {
113    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
114        tokens.push(TokenTree::Literal(format!("{:?}", self), Span::default()));
115    }
116}
117impl ToTokens for String {
118    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
119        tokens.push(TokenTree::Literal(format!("{:?}", self), Span::default()));
120    }
121}
122impl<T: ToTokens> ToTokens for &T {
123    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
124        T::to_tokens(self, tokens);
125    }
126}
127impl<T: ToTokens> ToTokens for Vec<T> {
128    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
129        for item in self {
130            item.to_tokens(tokens);
131        }
132    }
133}
134impl<T: ToTokens> ToTokens for Option<T> {
135    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
136        if let Some(data) = self {
137            data.to_tokens(tokens);
138        }
139    }
140}
141impl<T: ToTokens, E: ToTokens> ToTokens for Result<T, E> {
142    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
143        match self {
144            Ok(ok) => ok.to_tokens(tokens),
145            Err(err) => err.to_tokens(tokens),
146        }
147    }
148}
149impl ToTokens for () {
150    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
151        tokens.push(TokenTree::Group(Delimiter::Parenthesis, vec![], Span::default()));
152    }
153}
154impl<T: ToTokens> ToTokens for PhantomData<T> {
155    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
156        tokens.push(TokenTree::Ident(format!("PhantomData"), Span::default()));
157    }
158}
159impl<T: ToTokens> ToTokens for Box<T> {
160    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
161        T::to_tokens(&self, tokens);
162    }
163}
164impl<'a, B> ToTokens for Cow<'a, B>
165where B: ToOwned + ?Sized, B: ToTokens {
166    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
167        B::to_tokens(&self, tokens);
168    }
169}
170impl<T> ToTokens for HashSet<T>
171where T: Ord, T: ToTokens {
172    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
173        let mut hs_vec: Vec<_> = self.iter().collect();
174        hs_vec.sort();
175        for item in hs_vec {
176            item.to_tokens(tokens);
177        }
178    }
179}
180impl<K, V> ToTokens for HashMap<K, V>
181where K: Ord, K: ToTokens, V: ToTokens {
182    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
183        let mut hm_vec: Vec<_> = self.iter().collect();
184        hm_vec.sort_by(|a, b| a.0.cmp(b.0));
185        for (k, v) in hm_vec {
186            k.to_tokens(tokens);
187            v.to_tokens(tokens);
188        }
189    }
190}
191impl<T: ToTokens> ToTokens for BTreeSet<T> {
192    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
193        for item in self {
194            item.to_tokens(tokens);
195        }
196    }
197}
198impl<K: ToTokens, V: ToTokens> ToTokens for BTreeMap<K, V> {
199    fn to_tokens(&self, tokens: &mut Vec<TokenTree>) {
200        for (k, v) in self {
201            k.to_tokens(tokens);
202            v.to_tokens(tokens);
203        }
204    }
205}