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
use crate::Entry;
use crate::TokenStream;

use std::fmt;

impl fmt::Display for TokenStream {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut last_token_end = usize::MAX;
        let mut this_token_span;
        for token in &self.tokens {
            let string = match token {
                Entry::Error(_) => return Err(fmt::Error),
                Entry::Ident(ident) => {
                    this_token_span = ident.span.clone();
                    ident.string().to_owned()
                }
                Entry::Punct(punct) => {
                    this_token_span = punct.span.clone();
                    char::from(punct.kind).to_string()
                }
                Entry::WhiteSpace(whitespace) => {
                    this_token_span = whitespace.span().clone();
                    whitespace.display()
                }
                Entry::End => break,
            };
            for _ in last_token_end..this_token_span.start {
                write!(f, " ")?;
            }
            write!(f, "{string}")?;
            last_token_end = this_token_span.end;
        }

        Ok(())
    }
}