use core::fmt::Display;
use crate::{
global::stack_index::StackIndex,
prelude::*,
runtime::Runtime,
types::type_definition::callable::InvalidArgumentError,
values::{
core_values::callable::error::CallableError, value::Value,
value_container::ValueContainer,
},
};
use alloc::boxed::Box;
#[derive(Debug, PartialEq, Eq)]
pub enum ApplyError {
UnsupportedApply,
AsyncCallableRequiresAsyncExecution,
CallableError(Box<CallableError>),
InvalidArgumentError(Box<InvalidArgumentError>),
}
impl Display for ApplyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ApplyError::UnsupportedApply => {
write!(f, "Value does not support apply operation")
}
ApplyError::CallableError(error) => {
write!(f, "Error during callable application: {}", error)
}
ApplyError::AsyncCallableRequiresAsyncExecution => {
write!(f, "Async callable requires async execution")
}
ApplyError::InvalidArgumentError(error) => {
write!(f, "{}", error)
}
}
}
}
impl From<CallableError> for ApplyError {
fn from(error: CallableError) -> Self {
ApplyError::CallableError(Box::new(error))
}
}
impl From<InvalidArgumentError> for ApplyError {
fn from(error: InvalidArgumentError) -> Self {
ApplyError::InvalidArgumentError(Box::new(error))
}
}
#[derive(Debug)]
pub struct ApplyArgument {
pub value: ValueContainer,
pub passed_as_ref: bool,
}
impl ApplyArgument {
pub fn referenced<T: Into<ValueContainer>>(value: T) -> Self {
ApplyArgument {
value: value.into(),
passed_as_ref: true,
}
}
pub fn owned<T: Into<ValueContainer>>(value: T) -> Self {
ApplyArgument {
value: value.into(),
passed_as_ref: false,
}
}
}
impl From<ValueContainer> for ApplyArgument {
fn from(value: ValueContainer) -> Self {
ApplyArgument::owned(value)
}
}
impl<T: Into<Value>> From<T> for ApplyArgument {
fn from(value: T) -> Self {
ApplyArgument {
value: ValueContainer::Local(value.into()),
passed_as_ref: false,
}
}
}
impl From<ApplyArgument> for ValueContainer {
fn from(arg: ApplyArgument) -> Self {
arg.value
}
}
pub fn get_borrowed_apply_argument_values(
values: Vec<ApplyArgument>,
) -> Vec<ValueContainer> {
values
.into_iter()
.filter(|arg| arg.passed_as_ref)
.map(|arg| arg.value)
.collect()
}
pub fn into_apply_arguments_with_stack_indices(
values: Vec<ValueContainer>,
borrowed_args_stack_indices: &[Option<StackIndex>],
) -> Vec<ApplyArgument> {
values
.into_iter()
.enumerate()
.map(|(i, v)| ApplyArgument {
value: v,
passed_as_ref: borrowed_args_stack_indices[i].is_some(),
})
.collect()
}
pub trait Apply {
fn try_apply_sync_checked(
&self,
runtime: &Runtime,
args: Vec<ApplyArgument>,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError> {
let expected_ref_count =
args.iter().filter(|arg| arg.passed_as_ref).count();
let res = self.try_apply_sync(runtime, args)?;
if res.1.len() != expected_ref_count {
return Err(ApplyError::CallableError(Box::new(
CallableError::LostBorrowedArguments {
expected: expected_ref_count,
actual: res.1.len(),
},
)));
}
Ok(res)
}
async fn try_apply_async_checked(
&self,
runtime: &Runtime,
args: Vec<ApplyArgument>,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError> {
let expected_ref_count =
args.iter().filter(|arg| arg.passed_as_ref).count();
let res = self.try_apply_async(runtime, args).await?;
if res.1.len() != expected_ref_count {
return Err(ApplyError::CallableError(Box::new(
CallableError::LostBorrowedArguments {
expected: expected_ref_count,
actual: res.1.len(),
},
)));
}
Ok(res)
}
fn try_apply_sync(
&self,
runtime: &Runtime,
args: Vec<ApplyArgument>,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError>;
async fn try_apply_async(
&self,
runtime: &Runtime,
args: Vec<ApplyArgument>,
) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError>;
}