diff options
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/content.rs | 21 | ||||
| -rw-r--r-- | src/model/styles.rs | 8 | ||||
| -rw-r--r-- | src/model/typeset.rs | 57 |
3 files changed, 43 insertions, 43 deletions
diff --git a/src/model/content.rs b/src/model/content.rs index 3c661929..5317236e 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -2,7 +2,6 @@ use std::any::TypeId; use std::fmt::{self, Debug, Formatter, Write}; use std::hash::{Hash, Hasher}; use std::iter::{self, Sum}; -use std::num::NonZeroUsize; use std::ops::{Add, AddAssign, Deref}; use ecow::{eco_format, EcoString, EcoVec}; @@ -10,10 +9,10 @@ use once_cell::sync::Lazy; use super::{ node, Behave, Behaviour, Fold, Guard, Locatable, Recipe, StableId, Style, StyleMap, - Synthesize, Vt, + Synthesize, }; use crate::diag::{SourceResult, StrResult}; -use crate::doc::{Location, Meta}; +use crate::doc::Meta; use crate::eval::{ cast_from_value, cast_to_value, Args, Cast, Func, FuncInfo, Str, Value, Vm, }; @@ -186,22 +185,6 @@ impl Content { self.field(field).ok_or_else(|| missing_field(field)) } - /// Determine the page of this content. - pub fn page(&self, vt: &Vt) -> StrResult<NonZeroUsize> { - match self.stable_id() { - Some(id) => Ok(vt.introspector.page(id)), - None => Err("this method can only be called on queried content".into()), - } - } - - /// Determine the location of this content. - pub fn location(&self, vt: &Vt) -> StrResult<Location> { - match self.stable_id() { - Some(id) => Ok(vt.introspector.location(id)), - None => Err("this method can only be called on queried content".into()), - } - } - /// The content's label. pub fn label(&self) -> Option<&Label> { match self.field("label")? { diff --git a/src/model/styles.rs b/src/model/styles.rs index 7b725af9..b7d09774 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -78,13 +78,7 @@ impl PartialEq for StyleMap { impl Debug for StyleMap { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - if let [style] = self.0.as_slice() { - return style.fmt(f); - } - - let pieces: Vec<_> = - self.0.iter().map(|value| eco_format!("{value:?}")).collect(); - f.write_str(&pretty_array_like(&pieces, false)) + f.pad("..") } } diff --git a/src/model/typeset.rs b/src/model/typeset.rs index 162a2a67..8216d7a8 100644 --- a/src/model/typeset.rs +++ b/src/model/typeset.rs @@ -1,3 +1,4 @@ +use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; use std::num::NonZeroUsize; @@ -6,7 +7,7 @@ use comemo::{Constraint, Track, Tracked, TrackedMut}; use super::{Content, Selector, StyleChain}; use crate::diag::SourceResult; use crate::doc::{Document, Element, Frame, Location, Meta}; -use crate::eval::Tracer; +use crate::eval::{cast_from_value, Tracer}; use crate::geom::{Point, Transform}; use crate::util::NonZeroExt; use crate::World; @@ -116,7 +117,7 @@ impl StabilityProvider { /// Stably identifies a call site across multiple layout passes. /// /// This struct is created by [`StabilityProvider::identify`]. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct StableId(u128, usize, usize); impl StableId { @@ -126,16 +127,27 @@ impl StableId { } } +impl Debug for StableId { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.pad("..") + } +} + +cast_from_value! { + StableId: "stable id", +} + /// Provides access to information about the document. pub struct Introspector { init: bool, + pages: usize, nodes: Vec<(Content, Location)>, } impl Introspector { /// Create a new introspector. pub fn new(frames: &[Frame]) -> Self { - let mut introspector = Self { init: false, nodes: vec![] }; + let mut introspector = Self { init: false, pages: frames.len(), nodes: vec![] }; for (i, frame) in frames.iter().enumerate() { let page = NonZeroUsize::new(1 + i).unwrap(); introspector.extract(frame, page, Transform::identity()); @@ -180,26 +192,37 @@ impl Introspector { self.init } - /// Query for all metadata matches for the given selector. + /// Query for all nodes for the given selector. pub fn query(&self, selector: Selector) -> Vec<Content> { self.all().filter(|node| selector.matches(node)).cloned().collect() } - /// Query for all metadata matches before the given id. - pub fn query_split( - &self, - selector: Selector, - id: StableId, - ) -> (Vec<Content>, Vec<Content>) { - let mut iter = self.all(); - let before = iter - .by_ref() - .take_while(|node| node.stable_id() != Some(id)) + /// Query for all nodes up to the given id. + pub fn query_before(&self, selector: Selector, id: StableId) -> Vec<Content> { + let mut matches = vec![]; + for node in self.all() { + if selector.matches(node) { + matches.push(node.clone()); + } + if node.stable_id() == Some(id) { + break; + } + } + matches + } + + /// Query for all nodes starting from the given id. + pub fn query_after(&self, selector: Selector, id: StableId) -> Vec<Content> { + self.all() + .skip_while(|node| node.stable_id() != Some(id)) .filter(|node| selector.matches(node)) .cloned() - .collect(); - let after = iter.filter(|node| selector.matches(node)).cloned().collect(); - (before, after) + .collect() + } + + /// The total number pages. + pub fn pages(&self) -> NonZeroUsize { + NonZeroUsize::new(self.pages).unwrap_or(NonZeroUsize::ONE) } /// Find the page number for the given stable id. |
