use std::{collections::HashMap, fmt};
use crate::{func::Callable, Stack, ErrorKind};
#[allow(unused_imports)]
use crate::{Avid, Builder};
pub struct Promises<'f> {
pub(crate) fns: HashMap<String, Callable<'f, 'f>>,
}
impl Promises<'_> {
pub fn count(&self) -> usize {
self.fns.len()
}
}
impl fmt::Debug for Promises<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let new_fns = self
.fns
.keys()
.map(|x| (x, "<fn>"))
.collect::<HashMap<_, _>>();
write!(f, "{new_fns:?}")
}
}
#[derive(Default)]
pub struct PromiseBuilder<'f> {
pub(crate) fns: HashMap<String, Callable<'f, 'f>>,
}
impl<'f> PromiseBuilder<'f> {
pub fn build(self) -> Promises<'f> {
Promises { fns: self.fns }
}
pub fn new() -> Self {
Self {
fns: HashMap::new(),
}
}
pub fn add_promise<N, F>(mut self, name: N, func: F) -> Self
where
N: ToString,
F: 'f + FnMut(&mut Stack) -> crate::Result<(), ErrorKind>,
{
let name = name.to_string();
let func = Callable::Unknown {
name: Some(name.clone()),
func: Box::new(func),
};
self.fns.insert(name, func);
self
}
}