diff options
Diffstat (limited to 'src/eval/class.rs')
| -rw-r--r-- | src/eval/class.rs | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/eval/class.rs b/src/eval/class.rs index 4307eecb..acdf38e6 100644 --- a/src/eval/class.rs +++ b/src/eval/class.rs @@ -6,15 +6,15 @@ use crate::diag::TypResult; /// A class of nodes. /// /// You can [construct] an instance of a class in Typst code by invoking the -/// class as a callable. This always produces a template, but not necessarily a -/// simple inline or block node. For example, the `text` constructor does not -/// actually create a [`TextNode`]. Instead it applies styling to whatever node -/// you pass in and returns it structurally unchanged. +/// class as a callable. This always produces a template value, but not +/// necessarily a simple inline or block node. For example, the `text` +/// constructor does not actually create a [`TextNode`]. Instead it applies +/// styling to whatever node you pass in and returns it structurally unchanged. /// /// The arguments you can pass to a class constructor fall into two categories: -/// Data that is inherent to the instance (e.g. the text of a heading) and style -/// properties (e.g. the fill color of a heading). As the latter are often -/// shared by many instances throughout a document, they can also be +/// Data that is inherent to the instance (e.g. the text/content of a heading) +/// and style properties (e.g. the fill color of a heading). As the latter are +/// often shared by many instances throughout a document, they can also be /// conveniently configured through class's [`set`] rule. Then, they apply to /// all nodes that are instantiated into the template where the `set` was /// executed. @@ -62,25 +62,30 @@ impl Class { self.name } + /// Return the class constructor as a function. + pub fn constructor(&self) -> Func { + Func::native(self.name, self.construct) + } + /// Construct an instance of the class. /// - /// This parses both property and data arguments (in this order) and styles - /// the template constructed from the data with the style properties. - pub fn construct(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { - (self.construct)(ctx, args) + /// This parses both property and data arguments (in this order), styles the + /// template constructed from the data with the style properties and wraps + /// it in a value. + pub fn construct(&self, ctx: &mut EvalContext, mut args: Args) -> TypResult<Value> { + let value = (self.construct)(ctx, &mut args)?; + args.finish()?; + Ok(value) } /// Execute the class's set rule. /// - /// This parses property arguments and writes the resulting styles into the - /// given style map. There are no further side effects. - pub fn set(&self, args: &mut Args, styles: &mut StyleMap) -> TypResult<()> { - (self.set)(args, styles) - } - - /// Return the class constructor as a function. - pub fn constructor(&self) -> Func { - Func::native(self.name, self.construct) + /// This parses property arguments and return the resulting styles. + pub fn set(&self, mut args: Args) -> TypResult<StyleMap> { + let mut styles = StyleMap::new(); + (self.set)(&mut args, &mut styles)?; + args.finish()?; + Ok(styles) } } |
