From 9bc90c371fb41a2d6dc08eb4673e5be15f829514 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 2 Dec 2022 15:41:39 +0100 Subject: Introspection --- src/model/cast.rs | 14 +++-- src/model/content.rs | 67 +++++++++++++------- src/model/ops.rs | 1 - src/model/realize.rs | 13 +++- src/model/styles.rs | 13 ++-- src/model/typeset.rs | 173 +++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 245 insertions(+), 36 deletions(-) (limited to 'src/model') diff --git a/src/model/cast.rs b/src/model/cast.rs index 6a78eebd..bfde1bdd 100644 --- a/src/model/cast.rs +++ b/src/model/cast.rs @@ -255,17 +255,23 @@ castable! { } castable! { - Destination, - Expected: "string or dictionary with `page`, `x`, and `y` keys", - Value::Str(string) => Self::Url(string.into()), + Location, + Expected: "dictionary with `page`, `x`, and `y` keys", Value::Dict(dict) => { let page = dict.get("page")?.clone().cast()?; let x: Length = dict.get("x")?.clone().cast()?; let y: Length = dict.get("y")?.clone().cast()?; - Self::Internal(Location { page, pos: Point::new(x.abs, y.abs) }) + Self { page, pos: Point::new(x.abs, y.abs) } }, } +castable! { + Destination, + Expected: "string or dictionary with `page`, `x`, and `y` keys", + Value::Str(string) => Self::Url(string.into()), + v @ Value::Dict(_) => Self::Internal(v.cast()?), +} + castable! { FontStyle, Expected: "string", diff --git a/src/model/content.rs b/src/model/content.rs index 2f7e7671..f261f9b1 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -21,14 +21,16 @@ use crate::World; pub struct Content { obj: Arc, span: Option, - meta: ThinVec, + modifiers: ThinVec, } -/// Metadata that can be attached to content. +/// Modifiers that can be attached to content. #[derive(Debug, Clone, PartialEq, Hash)] -enum Meta { +enum Modifier { + Prepared, Guard(Guard), Label(Label), + Field(EcoString, Value), } impl Content { @@ -62,17 +64,22 @@ impl Content { /// Attach a label to the content. pub fn labelled(mut self, label: Label) -> Self { - for meta in &mut self.meta { - if let Meta::Label(prev) = meta { + for modifier in &mut self.modifiers { + if let Modifier::Label(prev) = modifier { *prev = label; return self; } } - self.meta.push(Meta::Label(label)); + self.modifiers.push(Modifier::Label(label)); self } + /// Attach a field to the content. + pub fn push_field(&mut self, name: impl Into, value: Value) { + self.modifiers.push(Modifier::Field(name.into(), value)); + } + /// Style this content with a single style property. pub fn styled(self, key: K, value: K::Value) -> Self { self.styled_with_entry(Style::Property(Property::new(key, value))) @@ -146,8 +153,8 @@ impl Content { /// The content's label. pub fn label(&self) -> Option<&Label> { - self.meta.iter().find_map(|meta| match meta { - Meta::Label(label) => Some(label), + self.modifiers.iter().find_map(|modifier| match modifier { + Modifier::Label(label) => Some(label), _ => None, }) } @@ -161,6 +168,14 @@ impl Content { }); } + for modifier in &self.modifiers { + if let Modifier::Field(other, value) = modifier { + if name == other.as_str() { + return Some(value.clone()); + } + } + } + self.obj.field(name) } @@ -201,10 +216,23 @@ impl Content { /// Disable a show rule recipe. #[doc(hidden)] pub fn guarded(mut self, id: Guard) -> Self { - self.meta.push(Meta::Guard(id)); + self.modifiers.push(Modifier::Guard(id)); self } + /// Mark this content as prepared. + #[doc(hidden)] + pub fn prepared(mut self) -> Self { + self.modifiers.push(Modifier::Prepared); + self + } + + /// Whether this node was prepared. + #[doc(hidden)] + pub fn is_prepared(&self) -> bool { + self.modifiers.contains(&Modifier::Prepared) + } + /// Whether a label can be attached to the content. pub(super) fn labellable(&self) -> bool { !self.has::() @@ -212,18 +240,21 @@ impl Content { /// Whether no show rule was executed for this node so far. pub(super) fn is_pristine(&self) -> bool { - !self.meta.iter().any(|meta| matches!(meta, Meta::Guard(_))) + !self + .modifiers + .iter() + .any(|modifier| matches!(modifier, Modifier::Guard(_))) } /// Check whether a show rule recipe is disabled. pub(super) fn is_guarded(&self, id: Guard) -> bool { - self.meta.contains(&Meta::Guard(id)) + self.modifiers.contains(&Modifier::Guard(id)) } - /// Copy the metadata from other content. - pub(super) fn copy_meta(&mut self, from: &Content) { + /// Copy the modifiers from another piece of content. + pub(super) fn copy_modifiers(&mut self, from: &Content) { self.span = from.span; - self.meta = from.meta.clone(); + self.modifiers = from.modifiers.clone(); } } @@ -239,12 +270,6 @@ impl Default for Content { } } -impl PartialEq for Content { - fn eq(&self, other: &Self) -> bool { - (*self.obj).hash128() == (*other.obj).hash128() - } -} - impl Add for Content { type Output = Self; @@ -371,7 +396,7 @@ pub trait Node: 'static + Capable { Content { obj: Arc::new(self), span: None, - meta: ThinVec::new(), + modifiers: ThinVec::new(), } } diff --git a/src/model/ops.rs b/src/model/ops.rs index 60b1c449..1a8dcb6b 100644 --- a/src/model/ops.rs +++ b/src/model/ops.rs @@ -309,7 +309,6 @@ pub fn equal(lhs: &Value, rhs: &Value) -> bool { (Color(a), Color(b)) => a == b, (Str(a), Str(b)) => a == b, (Label(a), Label(b)) => a == b, - (Content(a), Content(b)) => a == b, (Array(a), Array(b)) => a == b, (Dict(a), Dict(b)) => a == b, (Func(a), Func(b)) => a == b, diff --git a/src/model/realize.rs b/src/model/realize.rs index 46e0d678..abe20901 100644 --- a/src/model/realize.rs +++ b/src/model/realize.rs @@ -3,6 +3,10 @@ use crate::diag::SourceResult; /// Whether the target is affected by show rules in the given style chain. pub fn applicable(target: &Content, styles: StyleChain) -> bool { + if target.has::() && !target.is_prepared() { + return true; + } + // Find out how many recipes there are. let mut n = styles.recipes().count(); @@ -90,7 +94,7 @@ fn try_apply( let make = |s| { let mut content = item!(text)(s); - content.copy_meta(&target); + content.copy_modifiers(&target); content }; @@ -124,6 +128,13 @@ fn try_apply( } } +/// Preparations before execution of any show rule. +#[capability] +pub trait Prepare { + /// Prepare the node for show rule application. + fn prepare(&self, vt: &mut Vt, this: Content, styles: StyleChain) -> Content; +} + /// The base recipe for a node. #[capability] pub trait Show { diff --git a/src/model/styles.rs b/src/model/styles.rs index 80ec0d1e..37596b8d 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -17,7 +17,7 @@ use crate::util::ReadableTypeId; use crate::World; /// A map of style properties. -#[derive(Default, Clone, PartialEq, Hash)] +#[derive(Default, Clone, Hash)] pub struct StyleMap(Vec