diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-01-31 17:57:20 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-01 12:26:13 +0100 |
| commit | 6a6753cb69f7c29e857fd465eecf66a02ff76aa3 (patch) | |
| tree | e157752f30f5c493ee045d98039cfd3a94cdff22 /src/eval/class.rs | |
| parent | 20b1a38414101f842a6d9201133a5aaaa45a7cec (diff) | |
Better function representation
Diffstat (limited to 'src/eval/class.rs')
| -rw-r--r-- | src/eval/class.rs | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/eval/class.rs b/src/eval/class.rs index 307bebcc..91a9daef 100644 --- a/src/eval/class.rs +++ b/src/eval/class.rs @@ -1,8 +1,7 @@ use std::fmt::{self, Debug, Formatter, Write}; -use super::{Args, EvalContext, Node, StyleMap}; +use super::{Args, EvalContext, Func, Node, StyleMap, Value}; use crate::diag::TypResult; -use crate::util::EcoString; /// A class of [nodes](Node). /// @@ -35,38 +34,40 @@ use crate::util::EcoString; /// [`set`]: Self::set #[derive(Clone)] pub struct Class { - name: EcoString, - construct: fn(&mut EvalContext, &mut Args) -> TypResult<Node>, + name: &'static str, + construct: fn(&mut EvalContext, &mut Args) -> TypResult<Value>, set: fn(&mut Args, &mut StyleMap) -> TypResult<()>, } impl Class { /// Create a new class. - pub fn new<T>(name: EcoString) -> Self + pub fn new<T>(name: &'static str) -> Self where T: Construct + Set + 'static, { Self { name, - construct: T::construct, + construct: |ctx, args| { + let mut styles = StyleMap::new(); + T::set(args, &mut styles)?; + let node = T::construct(ctx, args)?; + Ok(Value::Node(node.styled_with_map(styles.scoped()))) + }, set: T::set, } } /// The name of the class. - pub fn name(&self) -> &EcoString { - &self.name + pub fn name(&self) -> &'static str { + self.name } /// Construct an instance of the class. /// /// This parses both property and data arguments (in this order) and styles /// the node constructed from the data with the style properties. - pub fn construct(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Node> { - let mut styles = StyleMap::new(); - self.set(args, &mut styles)?; - let node = (self.construct)(ctx, args)?; - Ok(node.styled_with_map(styles.scoped())) + pub fn construct(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { + (self.construct)(ctx, args) } /// Execute the class's set rule. @@ -76,6 +77,11 @@ impl Class { 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) + } } impl Debug for Class { |
