From a9fdff244aef859449a76e5f762ee7c343a8ddcc Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 10 Mar 2023 20:47:23 +0100 Subject: Expose content representation more --- src/eval/func.rs | 56 ++++++++++++++++++-------------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) (limited to 'src/eval/func.rs') diff --git a/src/eval/func.rs b/src/eval/func.rs index 79ae142c..8da5c6bc 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -13,7 +13,7 @@ use super::{ Vm, }; use crate::diag::{bail, SourceResult, StrResult}; -use crate::model::{Content, NodeId, Selector, StyleMap}; +use crate::model::{NodeId, Selector, StyleMap}; use crate::syntax::ast::{self, AstNode, Expr}; use crate::syntax::{SourceId, Span, SyntaxNode}; use crate::util::hash128; @@ -29,7 +29,7 @@ enum Repr { /// A native Rust function. Native(NativeFunc), /// A function for a node. - Node(NodeFunc), + Node(NodeId), /// A user-defined closure. Closure(Closure), /// A nested function with pre-applied arguments. @@ -156,13 +156,13 @@ impl Func { /// Create a selector for this function's node type. pub fn select(&self, fields: Option) -> StrResult { - match &**self.0 { - Repr::Node(node) => { - if node.id == item!(text_id) { + match **self.0 { + Repr::Node(id) => { + if id == item!(text_id) { Err("to select text, please use a string or regex instead")?; } - Ok(Selector::Node(node.id, fields)) + Ok(Selector::Node(id, fields)) } _ => Err("this function is not selectable")?, } @@ -172,8 +172,8 @@ impl Func { impl Debug for Func { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self.name() { - Some(name) => write!(f, ""), - None => f.write_str(""), + Some(name) => write!(f, "{name}"), + None => f.write_str("(..) => .."), } } } @@ -190,6 +190,16 @@ impl From for Func { } } +impl From for Func { + fn from(id: NodeId) -> Self { + Repr::Node(id).into() + } +} + +cast_to_value! { + v: NodeId => Value::Func(v.into()) +} + /// A native Rust function. pub struct NativeFunc { /// The function's implementation. @@ -223,36 +233,6 @@ where } } -/// A function defined by a native Rust node. -pub struct NodeFunc { - /// The node's id. - pub id: NodeId, - /// The node's constructor. - pub construct: fn(&Vm, &mut Args) -> SourceResult, - /// The node's set rule. - pub set: fn(&mut Args) -> SourceResult, - /// Details about the function. - pub info: Lazy, -} - -impl Hash for NodeFunc { - fn hash(&self, state: &mut H) { - self.id.hash(state); - (self.construct as usize).hash(state); - (self.set as usize).hash(state); - } -} - -impl From for Func { - fn from(node: NodeFunc) -> Self { - Repr::Node(node).into() - } -} - -cast_to_value! { - v: NodeFunc => Value::Func(v.into()) -} - /// Details about a function. #[derive(Debug, Clone)] pub struct FuncInfo { -- cgit v1.2.3