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
use serde::Serialize;
use serde_json;
use token::{Tk, Id};
pub fn json<'a, T: Serialize>(input: &'a T) -> String {
match serde_json::to_string_pretty(&input) {
Ok(string) => string,
Err(err) => format!("{:?}", err)
}
}
pub fn token(t: &Tk) -> String {
match t.id() {
Id::Space |
Id::Tab |
Id::BlockStart |
Id::BlockEnd |
Id::Newline => format!("{:>15} {:^20}{:>10}", format!("{:?}", t.id()), format!("{:?}", String::from_utf8_lossy(t.bytes())), format!("{:?}", t.tag())),
_ => format!("{:>15} {:^20}{:>10}", format!("{:?}", t.id()), String::from_utf8_lossy(t.bytes()), format!("{:?}", t.tag()))
}
}
pub fn tokens(tokens: &[Tk], filter_spaces: bool) -> String {
let result: Vec<String> = tokens.iter().enumerate().map(|(idx, t)| {
match (filter_spaces, t.id()) {
(true, Id::Space) => "".to_string(),
_ => format!("{:>3}: {}", idx, token(t))
}
}).filter(|s| !s.is_empty()).collect();
result.join("\n")
}