Make show rule fallible again

This commit is contained in:
Laurenz 2022-12-05 12:21:10 +01:00
parent 1bb05677fa
commit 1d324235bd
10 changed files with 39 additions and 24 deletions

View File

@ -71,12 +71,12 @@ impl Prepare for HeadingNode {
}
impl Show for HeadingNode {
fn show(&self, _: &mut Vt, this: &Content, _: StyleChain) -> Content {
fn show(&self, _: &mut Vt, this: &Content, _: StyleChain) -> SourceResult<Content> {
let mut realized = self.body.clone();
if let Some(Value::Str(numbering)) = this.field("numbers") {
realized = TextNode::packed(numbering) + SpaceNode.pack() + realized;
}
BlockNode(realized).pack()
Ok(BlockNode(realized).pack())
}
}

View File

@ -30,7 +30,7 @@ impl MathNode {
}
impl Show for MathNode {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> Content {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> {
let mut map = StyleMap::new();
map.set_family(FontFamily::new("NewComputerModernMath"), styles);
@ -44,7 +44,7 @@ impl Show for MathNode {
realized = realized.aligned(Axes::with_x(Some(Align::Center.into())))
}
realized
Ok(realized)
}
}

View File

@ -50,8 +50,8 @@ impl LinkNode {
}
impl Show for LinkNode {
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> Content {
self.body.clone()
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> SourceResult<Content> {
Ok(self.body.clone())
}
}

View File

@ -44,7 +44,12 @@ impl Prepare for OutlineNode {
}
impl Show for OutlineNode {
fn show(&self, vt: &mut Vt, _: &Content, styles: StyleChain) -> Content {
fn show(
&self,
vt: &mut Vt,
_: &Content,
styles: StyleChain,
) -> SourceResult<Content> {
let mut seq = vec![];
if let Some(title) = styles.get(Self::TITLE) {
let body = title.clone().unwrap_or_else(|| {
@ -137,6 +142,6 @@ impl Show for OutlineNode {
ancestors.push(node);
}
BlockNode(Content::sequence(seq)).pack()
Ok(BlockNode(Content::sequence(seq)).pack())
}
}

View File

@ -20,7 +20,7 @@ impl RefNode {
}
impl Show for RefNode {
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> Content {
TextNode::packed(format_eco!("@{}", self.0))
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> SourceResult<Content> {
Ok(TextNode::packed(format_eco!("@{}", self.0)))
}
}

View File

@ -47,8 +47,8 @@ impl<const L: DecoLine> DecoNode<L> {
}
impl<const L: DecoLine> Show for DecoNode<L> {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> Content {
self.0.clone().styled(
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> {
Ok(self.0.clone().styled(
TextNode::DECO,
Decoration {
line: L,
@ -57,7 +57,7 @@ impl<const L: DecoLine> Show for DecoNode<L> {
extent: styles.get(Self::EXTENT),
evade: styles.get(Self::EVADE),
},
)
))
}
}

View File

@ -62,8 +62,8 @@ impl StrongNode {
}
impl Show for StrongNode {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> Content {
self.0.clone().styled(TextNode::DELTA, Delta(styles.get(Self::DELTA)))
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> {
Ok(self.0.clone().styled(TextNode::DELTA, Delta(styles.get(Self::DELTA))))
}
}
@ -104,8 +104,8 @@ impl EmphNode {
}
impl Show for EmphNode {
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> Content {
self.0.clone().styled(TextNode::EMPH, Toggle)
fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> SourceResult<Content> {
Ok(self.0.clone().styled(TextNode::EMPH, Toggle))
}
}

View File

@ -56,7 +56,7 @@ impl Prepare for RawNode {
}
impl Show for RawNode {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> Content {
fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> {
let lang = styles.get(Self::LANG).as_ref().map(|s| s.to_lowercase());
let foreground = THEME
.settings
@ -109,7 +109,7 @@ impl Show for RawNode {
map.set(TextNode::SMART_QUOTES, false);
map.set_family(FontFamily::new("IBM Plex Mono"), styles);
realized.styled_with_map(map)
Ok(realized.styled_with_map(map))
}
}

View File

@ -43,7 +43,12 @@ impl<const S: ShiftKind> ShiftNode<S> {
}
impl<const S: ShiftKind> Show for ShiftNode<S> {
fn show(&self, vt: &mut Vt, _: &Content, styles: StyleChain) -> Content {
fn show(
&self,
vt: &mut Vt,
_: &Content,
styles: StyleChain,
) -> SourceResult<Content> {
let mut transformed = None;
if styles.get(Self::TYPOGRAPHIC) {
if let Some(text) = search_text(&self.0, S) {
@ -53,12 +58,12 @@ impl<const S: ShiftKind> Show for ShiftNode<S> {
}
};
transformed.unwrap_or_else(|| {
Ok(transformed.unwrap_or_else(|| {
let mut map = StyleMap::new();
map.set(TextNode::BASELINE, styles.get(Self::BASELINE));
map.set(TextNode::SIZE, styles.get(Self::SIZE));
self.0.clone().styled_with_map(map)
})
}))
}
}

View File

@ -47,7 +47,7 @@ pub fn realize(
if let Some(showable) = target.with::<dyn Show>() {
let guard = Guard::Base(target.id());
if realized.is_none() && !target.is_guarded(guard) {
realized = Some(showable.show(vt, target, styles));
realized = Some(showable.show(vt, target, styles)?);
}
}
@ -139,7 +139,12 @@ pub trait Prepare {
#[capability]
pub trait Show {
/// Execute the base recipe for this node.
fn show(&self, vt: &mut Vt, this: &Content, styles: StyleChain) -> Content;
fn show(
&self,
vt: &mut Vt,
this: &Content,
styles: StyleChain,
) -> SourceResult<Content>;
}
/// Post-process a node after it was realized.