#[cfg(feature = "host")]
use super::TokenPath;
use super::{CapturedDelimiter, CapturedPayload, CapturedTokenTree};
use crate::identity::{encode_bytes, encode_length};
#[cfg(feature = "host")]
pub(crate) fn encode_token_path(path: &TokenPath, into: &mut Vec<u8>) {
encode_length(path.steps().len(), into);
for step in path.steps() {
into.extend_from_slice(&step.to_be_bytes());
}
}
pub(super) fn encode_captured(tree: &CapturedTokenTree, into: &mut Vec<u8>) {
match tree.payload() {
CapturedPayload::Word(word) => {
into.push(1);
encode_text(word, into);
}
CapturedPayload::Punct(mark) => {
into.push(2);
let mut buffer = [0u8; 4];
encode_text(mark.encode_utf8(&mut buffer), into);
}
CapturedPayload::Text(text) => {
into.push(3);
encode_text(text, into);
}
CapturedPayload::Number(number) => {
into.push(4);
encode_text(number, into);
}
CapturedPayload::Group { delimiter, trees } => {
into.push(5);
into.push(match delimiter {
CapturedDelimiter::Parenthesis => 0,
CapturedDelimiter::Brace => 1,
CapturedDelimiter::Bracket => 2,
CapturedDelimiter::Bare => 3,
});
encode_length(trees.len(), into);
for inner in trees.as_slice() {
encode_captured(inner, into);
}
}
CapturedPayload::ByteText(material) => {
into.push(6);
encode_bytes(material, into);
}
CapturedPayload::Character(character) => {
into.push(7);
let mut buffer = [0u8; 4];
encode_text(character.encode_utf8(&mut buffer), into);
}
CapturedPayload::Byte(byte) => {
into.push(8);
into.push(*byte);
}
CapturedPayload::NulTerminatedText(material) => {
into.push(9);
encode_bytes(material, into);
}
CapturedPayload::RawIdentifier(name) => {
into.push(10);
encode_text(name, into);
}
CapturedPayload::JointPunct(mark) => {
into.push(11);
let mut buffer = [0u8; 4];
encode_text(mark.encode_utf8(&mut buffer), into);
}
}
}
fn encode_text(text: &str, into: &mut Vec<u8>) {
encode_bytes(text.as_bytes(), into);
}