use std::{collections::HashMap, sync::Arc};
use crate::{Doc, Error, Node};
#[allow(clippy::module_name_repetitions)]
pub struct RenderContext {
ichar: char,
iwidth: usize,
dict: HashMap<String, Arc<Doc>>
}
impl Default for RenderContext {
fn default() -> Self {
Self {
ichar: ' ',
iwidth: 2,
dict: HashMap::new()
}
}
}
impl RenderContext {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[allow(clippy::needless_pass_by_value)]
pub fn doc<N: ToString>(&mut self, id: N, doc: Arc<Doc>) {
self.dict.insert(id.to_string(), Arc::clone(&doc));
}
pub fn render(&self, name: &str) -> Result<String, Error> {
struct IterNode<'a> {
lst: &'a Vec<Node>,
idx: usize
}
let mut iterstack = Vec::new();
let mut out = String::new();
let mut indent: usize = 0;
let istr = self.ichar.to_string().repeat(self.iwidth);
if let Some(dict) = self.dict.get(name) {
iterstack.push(IterNode {
lst: &dict.nodes,
idx: 0
});
} else {
return Err(Error::BadRef(format!("Missing root document '{name}'")));
}
'outer: while let Some(mut it) = iterstack.pop() {
while it.idx < it.lst.len() {
match &it.lst[it.idx] {
Node::BeginScope(s) => {
let is = istr.repeat(indent);
out.push_str(&is);
out.push_str(s);
out.push('\n');
indent += 1;
}
Node::EndScope(s) => {
indent -= 1;
if let Some(s) = s {
let is = istr.repeat(indent);
out.push_str(&is);
out.push_str(s);
out.push('\n');
}
}
Node::Line(s) => {
let is = istr.repeat(indent);
out.push_str(&is);
out.push_str(s);
out.push('\n');
}
Node::OptRef(name) => {
if let Some(dict) = self.dict.get(name) {
iterstack.push(IterNode {
lst: it.lst,
idx: it.idx + 1
});
iterstack.push(IterNode {
lst: &dict.nodes,
idx: 0
});
continue 'outer;
}
}
Node::ReqRef(name) => {
if let Some(dict) = self.dict.get(name) {
iterstack.push(IterNode {
lst: it.lst,
idx: it.idx + 1
});
iterstack.push(IterNode {
lst: &dict.nodes,
idx: 0
});
continue 'outer;
}
return Err(Error::BadRef(format!(
"Missing required document '{name}'"
)));
}
}
it.idx += 1;
}
}
Ok(out)
}
}