Skip to main content

yomi_core/package/
functions.rs

1//! The types of command functions, and conversion of return values.
2
3use super::*;
4
5/// The return value of a command function (`T` or `Result<T, CommandError>`).
6pub trait IntoReturn {
7    /// The return type as seen from scripts.
8    fn ret_ty() -> Ty;
9    /// Makes a value.
10    fn into_return(self) -> Result<Value, CommandError>;
11}
12
13impl<T: IntoValue> IntoReturn for T {
14    fn ret_ty() -> Ty {
15        T::ty()
16    }
17    fn into_return(self) -> Result<Value, CommandError> {
18        Ok(self.into_value())
19    }
20}
21
22impl<T: IntoValue> IntoReturn for Result<T, CommandError> {
23    fn ret_ty() -> Ty {
24        T::ty()
25    }
26    fn into_return(self) -> Result<Value, CommandError> {
27        self.map(IntoValue::into_value)
28    }
29}
30
31/// The error when the number or types of arguments do not match the registration (the code is `core.bad_args`).
32pub(super) fn bad_args(name: &str) -> CommandError {
33    CommandError::with_code("core.bad_args", [("name", name.into())])
34}
35
36/// The function of an effect command (`Fn(&mut Ctx<P>, args...) -> R`; 0 to 6 arguments).
37pub trait EffectFn<P: Package, Args, R> {
38    /// The argument types.
39    fn arg_types() -> Vec<Ty>;
40    /// The return type.
41    fn ret_ty() -> Ty;
42    /// Calls it.
43    fn call(&self, ctx: &mut Ctx<'_, P>, args: &[Value]) -> Result<Value, CommandError>;
44}
45
46/// The function of a query command (`Fn(&Ctx<P>, args...) -> R`; 0 to 6 arguments).
47pub trait QueryFn<P: Package, Args, R> {
48    /// The argument types.
49    fn arg_types() -> Vec<Ty>;
50    /// The return type.
51    fn ret_ty() -> Ty;
52    /// Calls it.
53    fn call(&self, ctx: &Ctx<'_, P>, args: &[Value]) -> Result<Value, CommandError>;
54}
55
56/// A macro that implements `EffectFn`, `QueryFn` and others for the types of command functions (0 to 6 arguments).
57macro_rules! impl_fns {
58    ($n:expr; $($A:ident $a:ident $i:tt),*) => {
59        impl<P: Package, F, R, $($A),*> EffectFn<P, ($($A,)*), R> for F
60        where
61            F: Fn(&mut Ctx<'_, P>, $($A),*) -> R,
62            R: IntoReturn,
63            $($A: FromValue,)*
64        {
65            fn arg_types() -> Vec<Ty> {
66                vec![$($A::ty()),*]
67            }
68            fn ret_ty() -> Ty {
69                R::ret_ty()
70            }
71            fn call(&self, ctx: &mut Ctx<'_, P>, args: &[Value]) -> Result<Value, CommandError> {
72                if args.len() != $n {
73                    return Err(bad_args(P::NAME));
74                }
75                $(let $a = $A::from_value(&args[$i]).ok_or_else(|| bad_args(P::NAME))?;)*
76                self(ctx, $($a),*).into_return()
77            }
78        }
79
80        impl<P: Package, F, R, $($A),*> QueryFn<P, ($($A,)*), R> for F
81        where
82            F: Fn(&Ctx<'_, P>, $($A),*) -> R,
83            R: IntoReturn,
84            $($A: FromValue,)*
85        {
86            fn arg_types() -> Vec<Ty> {
87                vec![$($A::ty()),*]
88            }
89            fn ret_ty() -> Ty {
90                R::ret_ty()
91            }
92            fn call(&self, ctx: &Ctx<'_, P>, args: &[Value]) -> Result<Value, CommandError> {
93                if args.len() != $n {
94                    return Err(bad_args(P::NAME));
95                }
96                $(let $a = $A::from_value(&args[$i]).ok_or_else(|| bad_args(P::NAME))?;)*
97                self(ctx, $($a),*).into_return()
98            }
99        }
100    };
101}
102
103impl_fns!(0;);
104impl_fns!(1; A1 a1 0);
105impl_fns!(2; A1 a1 0, A2 a2 1);
106impl_fns!(3; A1 a1 0, A2 a2 1, A3 a3 2);
107impl_fns!(4; A1 a1 0, A2 a2 1, A3 a3 2, A4 a4 3);
108impl_fns!(5; A1 a1 0, A2 a2 1, A3 a3 2, A4 a4 3, A5 a5 4);
109impl_fns!(6; A1 a1 0, A2 a2 1, A3 a3 2, A4 a4 3, A5 a5 4, A6 a6 5);