diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-07 14:30:50 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-07 14:43:13 +0100 |
| commit | 0a41844cc4e645e87fe48aa31ed3a4fd40a6ab11 (patch) | |
| tree | c7cac97079491c8a11afae1211d7a80415fe64ef /src/model/func.rs | |
| parent | efd1853d069fbd1476e82d015da4d0d04cfaccc0 (diff) | |
Selectors
Diffstat (limited to 'src/model/func.rs')
| -rw-r--r-- | src/model/func.rs | 37 |
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 { |
