use std::fmt::Debug;
use crate::Text;
use super::*;
#[derive(Clone)]
pub struct HardBlock<'a, T> {
pub indent: usize,
pub lhs: &'static str,
pub rhs: &'static str,
pub joint: PrettyTree<'a, T>,
}
impl<'a, T: Text<'a> + Debug> Debug for HardBlock<'a, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HardBlock")
.field("indent", &self.indent)
.field("lhs", &self.lhs)
.field("rhs", &self.rhs)
.field("joint", &self.joint)
.finish()
}
}
impl<'a, T: Text<'a>> HardBlock<'a, T> {
pub fn new(lhs: &'static str, rhs: &'static str) -> Self {
Self { lhs, rhs, indent: 4, joint: PrettyTree::line_or_space() }
}
pub fn parentheses() -> Self {
Self::new("(", ")")
}
pub fn brackets() -> Self {
Self::new("[", "]")
}
pub fn curly_braces() -> Self {
Self::new("{", "}")
}
pub fn with_joint(self, joint: PrettyTree<'a, T>) -> Self {
Self { joint, ..self }
}
}
impl<'a, T: Clone + Text<'a>> HardBlock<'a, T> {
pub fn join_slice<P: PrettyPrint<'a, T>>(&self, slice: &[P], theme: &PrettyProvider) -> PrettyTree<'a, T> {
let mut outer = PrettySequence::new(5);
outer += self.lhs;
outer += PrettyTree::Hardline;
let mut inner = PrettySequence::new(slice.len() * 2);
for (idx, term) in slice.iter().enumerate() {
if idx != 0 {
inner += self.joint.clone();
}
inner += term.pretty(theme);
}
outer += inner.indent(self.indent);
outer += PrettyTree::Hardline;
outer += self.rhs;
outer.into()
}
}