use crate::util::{opt_u32, read_str, read_u32le, read_u8};
use std::convert::TryInto;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Read};
#[derive(Debug)]
pub struct Node {
parent_id: Option<u32>,
parent_line_no: u32,
node_type: NodeType,
quiet: bool,
}
impl Node {
pub(crate) fn read_from(mut input: impl Read) -> Result<Self, io::Error> {
let parent_id = opt_u32(read_u32le(&mut input)?);
let parent_line_no = read_u32le(&mut input)?;
let (node_type, quiet) = NodeType::read_from(input)?;
Ok(Node {
parent_id,
parent_line_no,
node_type,
quiet,
})
}
pub fn parent(&self) -> Option<(u32, u32)> {
self.parent_id.map(|id| (id, self.parent_line_no))
}
pub fn type_data(&self) -> &NodeType {
&self.node_type
}
pub fn is_quiet(&self) -> &bool {
&self.quiet
}
pub fn is_rept(&self) -> bool {
matches!(self.type_data(), NodeType::Rept(..))
}
}
#[derive(Debug)]
pub enum NodeType {
Rept(Vec<u32>),
File(Vec<u8>),
Macro(Vec<u8>),
}
impl NodeType {
fn read_from(mut input: impl Read) -> Result<(Self, bool), io::Error> {
let node_type = read_u8(&mut input)?;
let quiet = (node_type & 0x80) != 0;
match node_type & 0x7F {
0 => {
let depth = read_u32le(&mut input)?.try_into().unwrap();
let mut iters = Vec::with_capacity(depth);
for _ in 0..depth {
iters.push(read_u32le(&mut input)?);
}
Ok((NodeType::Rept(iters), quiet))
}
1 => Ok((NodeType::File(read_str(input)?), quiet)),
2 => Ok((NodeType::Macro(read_str(input)?), quiet)),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid fstack node type",
)),
}
}
}
impl Display for NodeType {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
use NodeType::*;
match self {
Rept(iters) => {
for iter in iters {
write!(fmt, "::REPT~{iter}")?;
}
}
File(name) | Macro(name) => write!(fmt, "{}", String::from_utf8_lossy(name))?,
};
Ok(())
}
}