From 671ce3dedd40067bb5cea84fe0739de013827053 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 31 Oct 2022 13:49:10 +0100 Subject: [PATCH] Replace `encode` with `field` --- src/library/math/mod.rs | 4 ++-- src/library/structure/heading.rs | 11 +++++------ src/library/structure/list.rs | 18 ++++++++---------- src/library/structure/reference.rs | 7 ++++--- src/library/structure/table.rs | 14 ++++++-------- src/library/text/deco.rs | 7 +++++-- src/library/text/link.rs | 13 +++++++------ src/library/text/mod.rs | 14 ++++++++++---- src/library/text/raw.rs | 13 +++++-------- src/library/text/shift.rs | 7 +++++-- src/model/content.rs | 8 ++++---- src/model/eval.rs | 8 ++++---- src/model/realize.rs | 2 +- src/model/recipe.rs | 9 ++------- src/model/show.rs | 10 +++++----- src/model/styles.rs | 4 +--- 16 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/library/math/mod.rs b/src/library/math/mod.rs index df00cb716..b23ed7cfe 100644 --- a/src/library/math/mod.rs +++ b/src/library/math/mod.rs @@ -84,8 +84,8 @@ impl Show for MathNode { ShowNode::new(self.clone()) } - fn encode(&self, _: StyleChain) -> Dict { - todo!() + fn field(&self, _: &str) -> Option { + None } fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index c967c6d7b..fa96248f5 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -73,12 +73,11 @@ impl Show for HeadingNode { Self { body: self.body.unguard(sel), ..*self }.pack() } - fn encode(&self, styles: StyleChain) -> Dict { - dict! { - "level" => Value::Int(self.level.get() as i64), - "body" => Value::Content(self.body.clone()), - "outlined" => Value::Bool(styles.get(Self::OUTLINED)), - "numbered" => Value::Bool(styles.get(Self::NUMBERED)), + fn field(&self, name: &str) -> Option { + match name { + "level" => Some(Value::Int(self.level.get() as i64)), + "body" => Some(Value::Content(self.body.clone())), + _ => None, } } diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 082c98c32..8a57069e0 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -90,16 +90,14 @@ impl Show for ListNode { .pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { - "tight" => Value::Bool(self.tight), - "attached" => Value::Bool(self.attached), - "items" => Value::Array( - self.items - .items() - .map(|item| item.encode()) - .collect() - ), + fn field(&self, name: &str) -> Option { + match name { + "tight" => Some(Value::Bool(self.tight)), + "attached" => Some(Value::Bool(self.attached)), + "items" => Some(Value::Array( + self.items.items().map(|item| item.encode()).collect(), + )), + _ => None, } } diff --git a/src/library/structure/reference.rs b/src/library/structure/reference.rs index 0a9f4f9ce..425ee5184 100644 --- a/src/library/structure/reference.rs +++ b/src/library/structure/reference.rs @@ -16,9 +16,10 @@ impl Show for RefNode { Self(self.0.clone()).pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { - "label" => Value::Str(self.0.clone().into()), + fn field(&self, name: &str) -> Option { + match name { + "label" => Some(Value::Str(self.0.clone().into())), + _ => None, } } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index a2f583f7e..41dcd104f 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -57,14 +57,12 @@ impl Show for TableNode { .pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { - "cells" => Value::Array( - self.cells - .iter() - .map(|cell| Value::Content(cell.clone())) - .collect() - ), + fn field(&self, name: &str) -> Option { + match name { + "cells" => Some(Value::Array( + self.cells.iter().cloned().map(Value::Content).collect(), + )), + _ => None, } } diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index 6a4905c83..ead928f63 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -44,8 +44,11 @@ impl Show for DecoNode { Self(self.0.unguard(sel)).pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { "body" => Value::Content(self.0.clone()) } + fn field(&self, name: &str) -> Option { + match name { + "body" => Some(Value::Content(self.0.clone())), + _ => None, + } } fn realize( diff --git a/src/library/text/link.rs b/src/library/text/link.rs index 7c4a95edf..98a867a89 100644 --- a/src/library/text/link.rs +++ b/src/library/text/link.rs @@ -58,16 +58,17 @@ impl Show for LinkNode { .pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { - "url" => match &self.dest { + fn field(&self, name: &str) -> Option { + match name { + "url" => Some(match &self.dest { Destination::Url(url) => Value::Str(url.clone().into()), Destination::Internal(loc) => Value::Dict(loc.encode()), - }, - "body" => match &self.body { + }), + "body" => Some(match &self.body { Some(body) => Value::Content(body.clone()), None => Value::None, - }, + }), + _ => None, } } diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index 2d302b9a9..d7d2ee387 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -515,8 +515,11 @@ impl Show for StrongNode { Self(self.0.unguard(sel)).pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { "body" => Value::Content(self.0.clone()) } + fn field(&self, name: &str) -> Option { + match name { + "body" => Some(Value::Content(self.0.clone())), + _ => None, + } } fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { @@ -540,8 +543,11 @@ impl Show for EmphNode { Self(self.0.unguard(sel)).pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { "body" => Value::Content(self.0.clone()) } + fn field(&self, name: &str) -> Option { + match name { + "body" => Some(Value::Content(self.0.clone())), + _ => None, + } } fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index d237aa92c..6d19deff5 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -46,14 +46,11 @@ impl Show for RawNode { Self { text: self.text.clone(), ..*self }.pack() } - fn encode(&self, styles: StyleChain) -> Dict { - dict! { - "text" => Value::Str(self.text.clone().into()), - "block" => Value::Bool(self.block), - "lang" => match styles.get(Self::LANG) { - Some(lang) => Value::Str(lang.clone().into()), - None => Value::None, - }, + fn field(&self, name: &str) -> Option { + match name { + "text" => Some(Value::Str(self.text.clone().into())), + "block" => Some(Value::Bool(self.block)), + _ => None, } } diff --git a/src/library/text/shift.rs b/src/library/text/shift.rs index adad5dc0d..e2a636a6a 100644 --- a/src/library/text/shift.rs +++ b/src/library/text/shift.rs @@ -38,8 +38,11 @@ impl Show for ShiftNode { Self(self.0.clone()).pack() } - fn encode(&self, _: StyleChain) -> Dict { - dict! { "body" => Value::Content(self.0.clone()) } + fn field(&self, name: &str) -> Option { + match name { + "body" => Some(Value::Content(self.0.clone())), + _ => None, + } } fn realize( diff --git a/src/model/content.rs b/src/model/content.rs index 530862a2d..3cdc6341d 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use comemo::Tracked; use super::{ - Builder, Dict, Key, Layout, LayoutNode, Property, Regions, Scratch, Selector, Show, + Builder, Key, Layout, LayoutNode, Property, Regions, Scratch, Selector, Show, ShowNode, StyleChain, StyleEntry, StyleMap, }; use crate::diag::{SourceResult, StrResult}; @@ -73,7 +73,7 @@ pub enum Content { Page(PageNode), /// A node that can be realized with styles, optionally with attached /// properties. - Show(ShowNode, Option), + Show(ShowNode), /// Content with attached styles. Styled(Arc<(Self, StyleMap)>), /// A sequence of multiple nodes. @@ -107,7 +107,7 @@ impl Content { where T: Show + Debug + Hash + Sync + Send + 'static, { - Self::Show(node.pack(), None) + Self::Show(node.pack()) } /// Create a new sequence node from multiples nodes. @@ -242,7 +242,7 @@ impl Debug for Content { Self::Item(item) => item.fmt(f), Self::Pagebreak { weak } => write!(f, "Pagebreak({weak})"), Self::Page(page) => page.fmt(f), - Self::Show(node, _) => node.fmt(f), + Self::Show(node) => node.fmt(f), Self::Styled(styled) => { let (sub, map) = styled.as_ref(); map.fmt(f)?; diff --git a/src/model/eval.rs b/src/model/eval.rs index 9051c9556..ca1c7974f 100644 --- a/src/model/eval.rs +++ b/src/model/eval.rs @@ -8,7 +8,7 @@ use unicode_segmentation::UnicodeSegmentation; use super::{ methods, ops, Arg, Args, Array, CapturesVisitor, Closure, Content, Dict, Flow, Func, - Pattern, Recipe, Scope, Scopes, StyleEntry, StyleMap, Value, Vm, + Pattern, Recipe, Scope, Scopes, Show, StyleEntry, StyleMap, Value, Vm, }; use crate::diag::{At, SourceResult, StrResult, Trace, Tracepoint}; use crate::geom::{Abs, Angle, Em, Fr, Ratio}; @@ -706,9 +706,9 @@ impl Eval for ast::FieldAccess { Ok(match object { Value::Dict(dict) => dict.get(&field).at(span)?.clone(), - Value::Content(Content::Show(_, Some(dict))) => dict - .get(&field) - .map_err(|_| format!("unknown field {field:?}")) + Value::Content(Content::Show(node)) => node + .field(&field) + .ok_or_else(|| format!("unknown field {field:?}")) .at(span)? .clone(), diff --git a/src/model/realize.rs b/src/model/realize.rs index 895c4f33a..69bae91fa 100644 --- a/src/model/realize.rs +++ b/src/model/realize.rs @@ -87,7 +87,7 @@ impl<'a> Builder<'a> { } } - Content::Show(node, _) => return self.show(node, styles), + Content::Show(node) => return self.show(node, styles), Content::Styled(styled) => return self.styled(styled, styles), Content::Sequence(seq) => return self.sequence(seq, styles), diff --git a/src/model/recipe.rs b/src/model/recipe.rs index 46dad6d1b..8124d4416 100644 --- a/src/model/recipe.rs +++ b/src/model/recipe.rs @@ -3,8 +3,7 @@ use std::fmt::{self, Debug, Formatter}; use comemo::Tracked; use super::{ - Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleChain, - StyleEntry, Value, + Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleEntry, Value, }; use crate::diag::SourceResult; use crate::library::structure::{DescNode, EnumNode, ListNode}; @@ -34,17 +33,13 @@ impl Recipe { pub fn apply( &self, world: Tracked, - styles: StyleChain, sel: Selector, target: Target, ) -> SourceResult> { let content = match (target, &self.pattern) { (Target::Node(node), &Pattern::Node(id)) if node.id() == id => { let node = node.unguard(sel); - self.call(world, || { - let dict = node.encode(styles); - Value::Content(Content::Show(node, Some(dict))) - })? + self.call(world, || Value::Content(Content::Show(node)))? } (Target::Text(text), Pattern::Regex(regex)) => { diff --git a/src/model/show.rs b/src/model/show.rs index bff694486..86659463e 100644 --- a/src/model/show.rs +++ b/src/model/show.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use comemo::{Prehashed, Tracked}; -use super::{Content, Dict, NodeId, Selector, StyleChain}; +use super::{Content, NodeId, Selector, StyleChain, Value}; use crate::diag::SourceResult; use crate::World; @@ -13,8 +13,8 @@ pub trait Show: 'static { /// Unguard nested content against recursive show rules. fn unguard(&self, sel: Selector) -> ShowNode; - /// Encode this node into a dictionary. - fn encode(&self, styles: StyleChain) -> Dict; + /// Access a field on this node. + fn field(&self, name: &str) -> Option; /// The base recipe for this node that is executed if there is no /// user-defined show rule. @@ -74,8 +74,8 @@ impl Show for ShowNode { self.0.unguard(sel) } - fn encode(&self, styles: StyleChain) -> Dict { - self.0.encode(styles) + fn field(&self, name: &str) -> Option { + self.0.field(name) } fn realize( diff --git a/src/model/styles.rs b/src/model/styles.rs index ddeeaa530..f3bd8c4ff 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -286,9 +286,7 @@ impl<'a> StyleChain<'a> { let sel = Selector::Nth(n); if self.guarded(sel) { guarded = true; - } else if let Some(content) = - recipe.apply(world, self, sel, target)? - { + } else if let Some(content) = recipe.apply(world, sel, target)? { realized = Some(content); break; }