Skip to main content

datex_core/traits/
apply.rs

1use core::fmt::Display;
2
3use crate::values::{
4    core_values::callable::error::CallableError,
5    value_container::ValueContainer,
6};
7use alloc::boxed::Box;
8
9#[derive(Debug)]
10pub enum ApplyError {
11    UnsupportedApply,
12    CallableError(Box<CallableError>),
13}
14impl Display for ApplyError {
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        match self {
17            ApplyError::UnsupportedApply => {
18                write!(f, "Value does not support apply operation")
19            }
20            ApplyError::CallableError(error) => {
21                write!(f, "Error during callable application: {}", error)
22            }
23        }
24    }
25}
26impl From<CallableError> for ApplyError {
27    fn from(error: CallableError) -> Self {
28        ApplyError::CallableError(Box::new(error))
29    }
30}
31
32// TODO #351: return ApplyErrors including call stack information (or store call stack directly in ExecutionError)
33pub trait Apply {
34    /// Applies multiple ValueContainer arguments to self
35    fn try_apply(
36        &self,
37        args: &[ValueContainer],
38    ) -> Result<Option<ValueContainer>, ApplyError>;
39    /// Applies a single ValueContainer argument to self
40    fn try_apply_single(
41        &self,
42        arg: &ValueContainer,
43    ) -> Result<Option<ValueContainer>, ApplyError>;
44}