summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-03-11 20:01:56 +0100
committerLaurenz <laurmaedje@gmail.com>2023-03-11 20:01:56 +0100
commit1a390deaea040191cf0e5937bd8e1427b49db71b (patch)
treec45b034b7f3179170c649ceac6124ed4666ffd5c /src
parent529d3e10c6b4d973e88b6c295eb22a45ea426e42 (diff)
Figures
Diffstat (limited to 'src')
-rw-r--r--src/eval/func.rs27
-rw-r--r--src/model/content.rs13
2 files changed, 27 insertions, 13 deletions
diff --git a/src/eval/func.rs b/src/eval/func.rs
index 8da5c6bc..26854240 100644
--- a/src/eval/func.rs
+++ b/src/eval/func.rs
@@ -142,6 +142,14 @@ impl Func {
self.select(Some(fields))
}
+ /// The node id of this function if it is an element function.
+ pub fn id(&self) -> Option<NodeId> {
+ match **self.0 {
+ Repr::Node(id) => Some(id),
+ _ => None,
+ }
+ }
+
/// Execute the function's set rule and return the resulting style map.
pub fn set(&self, mut args: Args) -> SourceResult<StyleMap> {
Ok(match &**self.0 {
@@ -156,16 +164,15 @@ impl Func {
/// Create a selector for this function's node type.
pub fn select(&self, fields: Option<Dict>) -> StrResult<Selector> {
- match **self.0 {
- Repr::Node(id) => {
- if id == item!(text_id) {
- Err("to select text, please use a string or regex instead")?;
- }
+ let Some(id) = self.id() else {
+ return Err("this function is not selectable".into());
+ };
- Ok(Selector::Node(id, fields))
- }
- _ => Err("this function is not selectable")?,
+ if id == item!(text_id) {
+ Err("to select text, please use a string or regex instead")?;
}
+
+ Ok(Selector::Node(id, fields))
}
}
@@ -196,10 +203,6 @@ impl From<NodeId> for Func {
}
}
-cast_to_value! {
- v: NodeId => Value::Func(v.into())
-}
-
/// A native Rust function.
pub struct NativeFunc {
/// The function's implementation.
diff --git a/src/model/content.rs b/src/model/content.rs
index be737331..012ad05f 100644
--- a/src/model/content.rs
+++ b/src/model/content.rs
@@ -10,7 +10,9 @@ use once_cell::sync::Lazy;
use super::{node, Guard, Recipe, Style, StyleMap};
use crate::diag::{SourceResult, StrResult};
-use crate::eval::{cast_from_value, Args, Cast, FuncInfo, Str, Value, Vm};
+use crate::eval::{
+ cast_from_value, cast_to_value, Args, Cast, Func, FuncInfo, Str, Value, Vm,
+};
use crate::syntax::Span;
use crate::util::pretty_array_like;
use crate::World;
@@ -382,6 +384,15 @@ impl Deref for NodeId {
}
}
+cast_from_value! {
+ NodeId,
+ v: Func => v.id().ok_or("this function is not an element")?
+}
+
+cast_to_value! {
+ v: NodeId => Value::Func(v.into())
+}
+
/// Static node for a node.
pub struct NodeMeta {
/// The node's name.