diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-03 14:27:20 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-03 14:27:20 +0100 |
| commit | f9d380249295280ebf84900d726c3baca565d511 (patch) | |
| tree | 6877449e47f2fd652f080fb78cf3eccf2a5ae20f /src | |
| parent | 20a1fd8bc7749f859aaef268d0add9c4bc11c4bd (diff) | |
Better debug representation for template
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/template.rs | 78 | ||||
| -rw-r--r-- | src/library/spacing.rs | 4 |
2 files changed, 57 insertions, 25 deletions
diff --git a/src/eval/template.rs b/src/eval/template.rs index 90a751f3..b15215ce 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -7,7 +7,6 @@ use std::ops::{Add, AddAssign}; use super::{Property, StyleMap, Styled}; use crate::diag::StrResult; -use crate::geom::SpecAxis; use crate::layout::{Layout, PackedNode}; use crate::library::prelude::*; use crate::library::{ @@ -41,26 +40,28 @@ use crate::Context; /// since we can just recurse into the nested sequences. Also, in theory, /// this allows better complexity when adding large sequence nodes just like /// for something like a text rope. -#[derive(Debug, PartialEq, Clone, Hash)] +#[derive(PartialEq, Clone, Hash)] pub enum Template { /// A word space. Space, /// A line break. Linebreak, - /// A paragraph break. - Parbreak, - /// A column break. - Colbreak, - /// A page break. - Pagebreak, + /// Horizontal spacing. + Horizontal(SpacingKind), /// Plain text. Text(EcoString), - /// Spacing. - Spacing(SpecAxis, SpacingKind), /// An inline node. Inline(PackedNode), + /// A paragraph break. + Parbreak, + /// Vertical spacing. + Vertical(SpacingKind), /// A block node. Block(PackedNode), + /// A column break. + Colbreak, + /// A page break. + Pagebreak, /// A page node. Page(PageNode), /// A template with attached styles. @@ -145,25 +146,56 @@ impl Default for Template { } } +impl Debug for Template { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Self::Space => f.pad("Space"), + Self::Linebreak => f.pad("Linebreak"), + Self::Horizontal(kind) => write!(f, "Horizontal({kind:?})"), + Self::Text(text) => write!(f, "Text({text:?})"), + Self::Inline(node) => { + f.write_str("Inline(")?; + node.fmt(f)?; + f.write_str(")") + } + Self::Parbreak => f.pad("Parbreak"), + Self::Vertical(kind) => write!(f, "Vertical({kind:?})"), + Self::Block(node) => { + f.write_str("Block(")?; + node.fmt(f)?; + f.write_str(")") + } + Self::Colbreak => f.pad("Colbreak"), + Self::Pagebreak => f.pad("Pagebreak"), + Self::Page(page) => page.fmt(f), + Self::Styled(sub, map) => { + map.fmt(f)?; + sub.fmt(f) + } + Self::Sequence(seq) => f.debug_list().entries(seq).finish(), + } + } +} + impl Add for Template { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Self::Sequence(match (self, rhs) { - (Self::Sequence(mut left), Self::Sequence(right)) => { - left.extend(right); - left + (Self::Sequence(mut lhs), Self::Sequence(rhs)) => { + lhs.extend(rhs); + lhs } - (Self::Sequence(mut left), right) => { - left.push(right); - left + (Self::Sequence(mut lhs), rhs) => { + lhs.push(rhs); + lhs } - (left, Self::Sequence(mut right)) => { - right.insert(0, left); - right + (lhs, Self::Sequence(mut rhs)) => { + rhs.insert(0, lhs); + rhs } - (left, right) => { - vec![left, right] + (lhs, rhs) => { + vec![lhs, rhs] } }) } @@ -276,14 +308,14 @@ impl Packer { Template::Text(text) => { self.push_inline(Styled::new(ParChild::text(text), styles)); } - Template::Spacing(SpecAxis::Horizontal, kind) => { + Template::Horizontal(kind) => { // Just like a line break, explicit horizontal spacing eats up // surrounding text spaces. self.par.last.hard(); self.push_inline(Styled::new(ParChild::Spacing(kind), styles)); self.par.last.hard(); } - Template::Spacing(SpecAxis::Vertical, kind) => { + Template::Vertical(kind) => { // Explicit vertical spacing ends the current paragraph and then // discards the paragraph break. self.parbreak(None, false); diff --git a/src/library/spacing.rs b/src/library/spacing.rs index 6c370d38..0b555510 100644 --- a/src/library/spacing.rs +++ b/src/library/spacing.rs @@ -8,7 +8,7 @@ pub struct HNode; #[class] impl HNode { fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> { - Ok(Template::Spacing(SpecAxis::Horizontal, args.expect("spacing")?)) + Ok(Template::Horizontal(args.expect("spacing")?)) } } @@ -18,7 +18,7 @@ pub struct VNode; #[class] impl VNode { fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> { - Ok(Template::Spacing(SpecAxis::Vertical, args.expect("spacing")?)) + Ok(Template::Vertical(args.expect("spacing")?)) } } |
