summaryrefslogtreecommitdiff
path: root/src/model/func.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/func.rs')
-rw-r--r--src/model/func.rs37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/model/func.rs b/src/model/func.rs
index 8cedb158..15434bbf 100644
--- a/src/model/func.rs
+++ b/src/model/func.rs
@@ -4,10 +4,12 @@ use std::sync::Arc;
use comemo::{Track, Tracked};
-use super::{Args, Eval, Flow, Node, NodeId, Route, Scope, Scopes, StyleMap, Value, Vm};
+use super::{
+ Args, Eval, Flow, Node, NodeId, Route, Scope, Scopes, Selector, StyleMap, Value, Vm,
+};
use crate::diag::{bail, SourceResult, StrResult};
use crate::syntax::ast::{self, Expr, TypedNode};
-use crate::syntax::{SourceId, SyntaxNode};
+use crate::syntax::{SourceId, Span, SyntaxNode};
use crate::util::EcoString;
use crate::World;
@@ -54,11 +56,6 @@ impl Func {
Self(Arc::new(Repr::Closure(closure)))
}
- /// Apply the given arguments to the function.
- pub fn with(self, args: Args) -> Self {
- Self(Arc::new(Repr::With(self, args)))
- }
-
/// The name of the function.
pub fn name(&self) -> Option<&str> {
match self.0.as_ref() {
@@ -106,12 +103,18 @@ impl Func {
self.call(&mut vm, args)
}
+ /// Apply the given arguments to the function.
+ pub fn with(self, args: Args) -> Self {
+ Self(Arc::new(Repr::With(self, args)))
+ }
+
/// Execute the function's set rule and return the resulting style map.
- pub fn set(&self, mut args: Args) -> SourceResult<StyleMap> {
- let styles = match self.0.as_ref() {
- Repr::Native(Native { set: Some(set), .. }) => set(&mut args)?,
- _ => StyleMap::new(),
+ pub fn set(&self, mut args: Args, span: Span) -> SourceResult<StyleMap> {
+ let Repr::Native(Native { set: Some(set), .. }) = self.0.as_ref() else {
+ bail!(span, "this function cannot be customized with set");
};
+
+ let styles = set(&mut args)?;
args.finish()?;
Ok(styles)
}
@@ -123,6 +126,18 @@ impl Func {
_ => Err("this function cannot be customized with show")?,
}
}
+
+ /// Create a selector from this node and the given arguments.
+ pub fn where_(self, args: &mut Args) -> StrResult<Selector> {
+ match self.0.as_ref() {
+ Repr::Native(Native { node: Some(id), .. }) => {
+ let named = args.to_named();
+ args.items.retain(|arg| arg.name.is_none());
+ Ok(Selector::Node(*id, Some(named)))
+ }
+ _ => Err("this function is not selectable")?,
+ }
+ }
}
impl Debug for Func {