From 0a41844cc4e645e87fe48aa31ed3a4fd40a6ab11 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 7 Nov 2022 14:30:50 +0100 Subject: Selectors --- src/model/func.rs | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/model/func.rs') 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 { - 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 { + 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 { + 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 { -- cgit v1.2.3