From fd417da04f7ca4b995de7f6510abafd3e9c31307 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 6 Jun 2023 21:13:59 +0200 Subject: Improve value casting infrastructure --- src/eval/func.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'src/eval/func.rs') diff --git a/src/eval/func.rs b/src/eval/func.rs index a224c5b8..86070fc7 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -1,5 +1,3 @@ -pub use typst_macros::func; - use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::sync::Arc; @@ -9,7 +7,7 @@ use ecow::eco_format; use once_cell::sync::Lazy; use super::{ - cast_to_value, Args, CastInfo, Eval, Flow, Route, Scope, Scopes, Tracer, Value, Vm, + cast, Args, CastInfo, Eval, Flow, IntoValue, Route, Scope, Scopes, Tracer, Value, Vm, }; use crate::diag::{bail, SourceResult, StrResult}; use crate::model::{ElemFunc, Introspector, Locator, Vt}; @@ -119,10 +117,10 @@ impl Func { /// Call the function with a Vt. #[tracing::instrument(skip_all)] - pub fn call_vt( + pub fn call_vt( &self, vt: &mut Vt, - args: impl IntoIterator, + args: impl IntoIterator, ) -> SourceResult { let route = Route::default(); let id = SourceId::detached(); @@ -233,13 +231,9 @@ impl From<&'static NativeFunc> for Func { } } -impl From for Value -where - F: Fn() -> &'static NativeFunc, -{ - fn from(f: F) -> Self { - Value::Func(f().into()) - } +cast! { + &'static NativeFunc, + self => Value::Func(self.into()), } /// Details about a function. @@ -249,16 +243,16 @@ pub struct FuncInfo { pub name: &'static str, /// The display name of the function. pub display: &'static str, - /// A string of keywords. + /// A string of search keywords. pub keywords: Option<&'static str>, + /// Which category the function is part of. + pub category: &'static str, /// Documentation for the function. pub docs: &'static str, /// Details about the function's parameters. pub params: Vec, - /// Valid types for the return value. - pub returns: Vec<&'static str>, - /// Which category the function is part of. - pub category: &'static str, + /// Valid values for the return value. + pub returns: CastInfo, /// The function's own scope of fields and sub-functions. pub scope: Scope, } @@ -311,6 +305,7 @@ pub(super) struct Closure { pub body: Expr, } +/// A closure parameter. #[derive(Hash)] pub enum Param { /// A positional parameter: `x`. @@ -362,7 +357,7 @@ impl Closure { // Parse the arguments according to the parameter list. let num_pos_params = closure.params.iter().filter(|p| matches!(p, Param::Pos(_))).count(); - let num_pos_args = args.to_pos().len() as usize; + let num_pos_args = args.to_pos().len(); let sink_size = num_pos_args.checked_sub(num_pos_params); let mut sink = None; @@ -425,8 +420,9 @@ impl From for Func { } } -cast_to_value! { - v: Closure => Value::Func(v.into()) +cast! { + Closure, + self => Value::Func(self.into()), } /// A visitor that determines which variables to capture for a closure. -- cgit v1.2.3