use super::{Doc, Elem};
mod fits;
mod fixup;
mod layout;
pub use layout::RenderConfig;
type Look<'a> = &'a [&'a [Elem]];
struct LookIter<'a> {
stack: Vec<(&'a [Elem], usize)>,
}
impl<'a> LookIter<'a> {
fn new(chain: Look<'a>) -> Self {
let mut stack: Vec<(&'a [Elem], usize)> = Vec::with_capacity(chain.len());
for s in chain.iter().rev() {
if !s.is_empty() {
stack.push((s, 0));
}
}
LookIter { stack }
}
fn push_front(&mut self, s: &'a [Elem]) {
if !s.is_empty() {
self.stack.push((s, 0));
}
}
fn remaining(&self) -> impl Iterator<Item = &'a [Elem]> + '_ {
self.stack.iter().rev().map(|(s, i)| &s[*i..])
}
}
impl<'a> Iterator for LookIter<'a> {
type Item = &'a Elem;
fn next(&mut self) -> Option<&'a Elem> {
while let Some((s, i)) = self.stack.last_mut() {
if *i < s.len() {
let e = &s[*i];
*i += 1;
return Some(e);
}
self.stack.pop();
}
None
}
}
impl Doc {
pub fn render(self, config: &RenderConfig) -> String {
layout::layout_greedy(config.width, config.indent_width, self)
}
pub fn fixup(mut self) -> Self {
fixup::fixup_mut(&mut self.0, 0, 0, false);
self
}
}