diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-09-20 19:49:47 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-09-20 19:49:47 +0200 |
| commit | 3760748fddd3b793c79c370398a9d4a3fc5afc04 (patch) | |
| tree | b1a615e510aa231cfe9757a9c0a35a375e32e3ba /src/model | |
| parent | 757a701c1aa2a6fb80033c7e75666661818da6f9 (diff) | |
Refactor error handling
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/content.rs | 28 | ||||
| -rw-r--r-- | src/model/layout.rs | 14 | ||||
| -rw-r--r-- | src/model/recipe.rs | 6 | ||||
| -rw-r--r-- | src/model/show.rs | 10 | ||||
| -rw-r--r-- | src/model/styles.rs | 8 |
5 files changed, 39 insertions, 27 deletions
diff --git a/src/model/content.rs b/src/model/content.rs index 8076eff9..dbea141c 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -23,7 +23,7 @@ use crate::World; /// Layout content into a collection of pages. /// /// Relayouts until all pinned locations are converged. -pub fn layout(world: &dyn World, content: &Content) -> TypResult<Vec<Frame>> { +pub fn layout(world: &dyn World, content: &Content) -> SourceResult<Vec<Frame>> { let styles = StyleChain::with_root(&world.config().styles); let scratch = Scratch::default(); @@ -235,7 +235,7 @@ impl Layout for Content { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { let scratch = Scratch::default(); let mut builder = Builder::new(world, &scratch, false); builder.accept(self, styles)?; @@ -369,7 +369,7 @@ impl<'a, 'w> Builder<'a, 'w> { fn into_doc( mut self, styles: StyleChain<'a>, - ) -> TypResult<(DocNode, StyleChain<'a>)> { + ) -> SourceResult<(DocNode, StyleChain<'a>)> { self.interrupt(Interruption::Page, styles, true)?; let (pages, shared) = self.doc.unwrap().pages.finish(); Ok((DocNode(pages), shared)) @@ -378,13 +378,17 @@ impl<'a, 'w> Builder<'a, 'w> { fn into_flow( mut self, styles: StyleChain<'a>, - ) -> TypResult<(FlowNode, StyleChain<'a>)> { + ) -> SourceResult<(FlowNode, StyleChain<'a>)> { self.interrupt(Interruption::Par, styles, false)?; let (children, shared) = self.flow.0.finish(); Ok((FlowNode(children), shared)) } - fn accept(&mut self, content: &'a Content, styles: StyleChain<'a>) -> TypResult<()> { + fn accept( + &mut self, + content: &'a Content, + styles: StyleChain<'a>, + ) -> SourceResult<()> { match content { Content::Empty => return Ok(()), Content::Text(text) => { @@ -430,7 +434,7 @@ impl<'a, 'w> Builder<'a, 'w> { Ok(()) } - fn show(&mut self, node: &ShowNode, styles: StyleChain<'a>) -> TypResult<()> { + fn show(&mut self, node: &ShowNode, styles: StyleChain<'a>) -> SourceResult<()> { if let Some(mut realized) = styles.apply(self.world, Target::Node(node))? { let mut map = StyleMap::new(); let barrier = Barrier::new(node.id()); @@ -447,7 +451,7 @@ impl<'a, 'w> Builder<'a, 'w> { &mut self, (content, map): &'a (Content, StyleMap), styles: StyleChain<'a>, - ) -> TypResult<()> { + ) -> SourceResult<()> { let stored = self.scratch.styles.alloc(styles); let styles = map.chain(stored); let intr = map.interruption(); @@ -470,7 +474,7 @@ impl<'a, 'w> Builder<'a, 'w> { intr: Interruption, styles: StyleChain<'a>, keep: bool, - ) -> TypResult<()> { + ) -> SourceResult<()> { if intr >= Interruption::List && !self.list.is_empty() { mem::take(&mut self.list).finish(self)?; } @@ -493,7 +497,11 @@ impl<'a, 'w> Builder<'a, 'w> { Ok(()) } - fn sequence(&mut self, seq: &'a [Content], styles: StyleChain<'a>) -> TypResult<()> { + fn sequence( + &mut self, + seq: &'a [Content], + styles: StyleChain<'a>, + ) -> SourceResult<()> { for content in seq { self.accept(content, styles)?; } @@ -738,7 +746,7 @@ impl<'a> ListBuilder<'a> { true } - fn finish(self, parent: &mut Builder<'a, '_>) -> TypResult<()> { + fn finish(self, parent: &mut Builder<'a, '_>) -> SourceResult<()> { let (items, shared) = self.items.finish(); let kind = match items.items().next() { Some(item) => item.kind, diff --git a/src/model/layout.rs b/src/model/layout.rs index 911cb4d5..68847471 100644 --- a/src/model/layout.rs +++ b/src/model/layout.rs @@ -6,7 +6,7 @@ use std::hash::Hash; use std::sync::Arc; use super::{Barrier, NodeId, Resolve, StyleChain, StyleEntry}; -use crate::diag::TypResult; +use crate::diag::SourceResult; use crate::eval::{RawAlign, RawLength}; use crate::frame::{Element, Frame}; use crate::geom::{ @@ -27,7 +27,7 @@ pub trait Layout: 'static { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>>; + ) -> SourceResult<Vec<Frame>>; /// Convert to a packed node. fn pack(self) -> LayoutNode @@ -219,7 +219,7 @@ impl Layout for LayoutNode { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { let barrier = StyleEntry::Barrier(Barrier::new(self.id())); let styles = barrier.chain(&styles); @@ -288,7 +288,7 @@ impl Layout for EmptyNode { _: &dyn World, regions: &Regions, _: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { Ok(vec![Frame::new( regions.expand.select(regions.first, Size::zero()), )]) @@ -310,7 +310,7 @@ impl Layout for SizedNode { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { // The "pod" is the region into which the child will be layouted. let pod = { // Resolve the sizing to a concrete size. @@ -357,7 +357,7 @@ impl Layout for FillNode { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { let mut frames = self.child.layout(world, regions, styles)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).filled(self.fill); @@ -382,7 +382,7 @@ impl Layout for StrokeNode { world: &dyn World, regions: &Regions, styles: StyleChain, - ) -> TypResult<Vec<Frame>> { + ) -> SourceResult<Vec<Frame>> { let mut frames = self.child.layout(world, regions, styles)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).stroked(self.stroke); diff --git a/src/model/recipe.rs b/src/model/recipe.rs index f5ca2cb9..980d939b 100644 --- a/src/model/recipe.rs +++ b/src/model/recipe.rs @@ -1,7 +1,7 @@ use std::fmt::{self, Debug, Formatter}; use super::{Content, Interruption, NodeId, Show, ShowNode, StyleChain, StyleEntry}; -use crate::diag::TypResult; +use crate::diag::SourceResult; use crate::eval::{Args, Func, Regex, Value}; use crate::library::structure::{EnumNode, ListNode}; use crate::syntax::Spanned; @@ -33,7 +33,7 @@ impl Recipe { styles: StyleChain, sel: Selector, target: Target, - ) -> TypResult<Option<Content>> { + ) -> SourceResult<Option<Content>> { let content = match (target, &self.pattern) { (Target::Node(node), &Pattern::Node(id)) if node.id() == id => { let node = node.unguard(sel); @@ -75,7 +75,7 @@ impl Recipe { } /// Call the recipe function, with the argument if desired. - fn call<F>(&self, world: &dyn World, arg: F) -> TypResult<Content> + fn call<F>(&self, world: &dyn World, arg: F) -> SourceResult<Content> where F: FnOnce() -> Value, { diff --git a/src/model/show.rs b/src/model/show.rs index e8d27977..56fb29ba 100644 --- a/src/model/show.rs +++ b/src/model/show.rs @@ -3,7 +3,7 @@ use std::hash::Hash; use std::sync::Arc; use super::{Content, NodeId, Selector, StyleChain}; -use crate::diag::TypResult; +use crate::diag::SourceResult; use crate::eval::Dict; use crate::util::Prehashed; use crate::World; @@ -18,7 +18,7 @@ pub trait Show: 'static { /// The base recipe for this node that is executed if there is no /// user-defined show rule. - fn realize(&self, world: &dyn World, styles: StyleChain) -> TypResult<Content>; + fn realize(&self, world: &dyn World, styles: StyleChain) -> SourceResult<Content>; /// Finalize this node given the realization of a base or user recipe. Use /// this for effects that should work even in the face of a user-defined @@ -33,7 +33,7 @@ pub trait Show: 'static { world: &dyn World, styles: StyleChain, realized: Content, - ) -> TypResult<Content> { + ) -> SourceResult<Content> { Ok(realized) } @@ -74,7 +74,7 @@ impl Show for ShowNode { self.0.encode(styles) } - fn realize(&self, world: &dyn World, styles: StyleChain) -> TypResult<Content> { + fn realize(&self, world: &dyn World, styles: StyleChain) -> SourceResult<Content> { self.0.realize(world, styles) } @@ -83,7 +83,7 @@ impl Show for ShowNode { world: &dyn World, styles: StyleChain, realized: Content, - ) -> TypResult<Content> { + ) -> SourceResult<Content> { self.0.finalize(world, styles, realized) } diff --git a/src/model/styles.rs b/src/model/styles.rs index 53ef926c..b61bd535 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -4,7 +4,7 @@ use std::iter; use std::marker::PhantomData; use super::{Barrier, Content, Key, Property, Recipe, Selector, Show, Target}; -use crate::diag::TypResult; +use crate::diag::SourceResult; use crate::frame::Role; use crate::library::text::{FontFamily, TextNode}; use crate::util::ReadableTypeId; @@ -277,7 +277,11 @@ impl<'a> StyleChain<'a> { } /// Apply show recipes in this style chain to a target. - pub fn apply(self, world: &dyn World, target: Target) -> TypResult<Option<Content>> { + pub fn apply( + self, + world: &dyn World, + target: Target, + ) -> SourceResult<Option<Content>> { // Find out how many recipes there any and whether any of their patterns // match. let mut n = 0; |
