summaryrefslogtreecommitdiff
path: root/src/eval/func.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-06-06 21:13:59 +0200
committerLaurenz <laurmaedje@gmail.com>2023-06-06 22:06:16 +0200
commitfd417da04f7ca4b995de7f6510abafd3e9c31307 (patch)
tree3675529c75ca7363701ac8ea306de2cc1d3cbcb3 /src/eval/func.rs
parent168bdf35bd773e67343c965cb473492cc5cae9e7 (diff)
Improve value casting infrastructure
Diffstat (limited to 'src/eval/func.rs')
-rw-r--r--src/eval/func.rs36
1 files changed, 16 insertions, 20 deletions
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<T: IntoValue>(
&self,
vt: &mut Vt,
- args: impl IntoIterator<Item = Value>,
+ args: impl IntoIterator<Item = T>,
) -> SourceResult<Value> {
let route = Route::default();
let id = SourceId::detached();
@@ -233,13 +231,9 @@ impl From<&'static NativeFunc> for Func {
}
}
-impl<F> From<F> 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<ParamInfo>,
- /// 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<Closure> 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.