use super::{GeneratedDelimiter, GeneratedSpacing, GeneratedToken};
pub(super) fn inspect_token(token: &GeneratedToken, into: &mut String) {
match token {
GeneratedToken::Word(word) => {
into.push_str(word);
into.push(' ');
}
GeneratedToken::RawIdentifier(name) => {
into.push_str("r#");
into.push_str(name);
into.push(' ');
}
GeneratedToken::Punct { mark, spacing } => {
into.push(*mark);
if *spacing == GeneratedSpacing::Alone {
into.push(' ');
}
}
GeneratedToken::Text(text) => {
into.push('"');
for character in text.chars() {
if character == '"' || character == '\\' {
into.push('\\');
}
into.push(character);
}
into.push('"');
into.push(' ');
}
GeneratedToken::Group { delimiter, tokens } => {
let (open, close) = match delimiter {
GeneratedDelimiter::Parenthesis => ('(', ')'),
GeneratedDelimiter::Brace => ('{', '}'),
GeneratedDelimiter::Bracket => ('[', ']'),
};
into.push(open);
into.push(' ');
for inner in tokens.as_slice() {
inspect_token(inner, into);
}
into.push(close);
into.push(' ');
}
GeneratedToken::ByteText(material) => {
into.push('b');
into.push('"');
for byte in material {
inspect_byte(*byte, into);
}
into.push('"');
into.push(' ');
}
GeneratedToken::Number(value) => {
into.push_str(&value.to_string());
into.push(' ');
}
}
}
fn inspect_byte(byte: u8, into: &mut String) {
match byte {
b'"' | b'\\' => {
into.push('\\');
into.push(char::from(byte));
}
0x20..=0x7E => into.push(char::from(byte)),
_ => {
into.push('\\');
into.push('x');
into.push(hex_digit(byte >> 4));
into.push(hex_digit(byte & 0x0F));
}
}
}
const fn hex_digit(nibble: u8) -> char {
match nibble {
0 => '0',
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
10 => 'A',
11 => 'B',
12 => 'C',
13 => 'D',
14 => 'E',
_ => 'F',
}
}