use crate::fn_native::{CallableFunction, IteratorFn, Shared};
use crate::module::Module;
use crate::utils::StaticVec;
use crate::stdlib::any::TypeId;
pub(crate) mod arithmetic;
mod array_basic;
mod eval;
mod iter_basic;
mod logic;
mod map_basic;
mod math_basic;
mod pkg_core;
mod pkg_std;
mod string_basic;
mod string_more;
mod time_basic;
pub use arithmetic::ArithmeticPackage;
#[cfg(not(feature = "no_index"))]
pub use array_basic::BasicArrayPackage;
pub use eval::EvalPackage;
pub use iter_basic::BasicIteratorPackage;
pub use logic::LogicPackage;
#[cfg(not(feature = "no_object"))]
pub use map_basic::BasicMapPackage;
pub use math_basic::BasicMathPackage;
pub use pkg_core::CorePackage;
pub use pkg_std::StandardPackage;
pub use string_basic::BasicStringPackage;
pub use string_more::MoreStringPackage;
#[cfg(not(feature = "no_std"))]
pub use time_basic::BasicTimePackage;
pub trait Package {
fn init(lib: &mut Module);
fn get(&self) -> PackageLibrary;
}
pub type PackageLibrary = Shared<Module>;
#[derive(Clone, Default)]
pub(crate) struct PackagesCollection(StaticVec<PackageLibrary>);
impl PackagesCollection {
pub fn push(&mut self, package: PackageLibrary) {
self.0.insert(0, package);
}
pub fn contains_fn(&self, hash: u64) -> bool {
self.0.iter().any(|p| p.contains_fn(hash))
}
pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> {
self.0
.iter()
.map(|p| p.get_fn(hash))
.find(|f| f.is_some())
.flatten()
}
pub fn contains_iter(&self, id: TypeId) -> bool {
self.0.iter().any(|p| p.contains_iter(id))
}
pub fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
self.0
.iter()
.map(|p| p.get_iter(id))
.find(|f| f.is_some())
.flatten()
}
}
#[macro_export]
macro_rules! def_package {
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
#[doc=$comment]
pub struct $package($root::packages::PackageLibrary);
impl $root::packages::Package for $package {
fn get(&self) -> $root::packages::PackageLibrary {
self.0.clone()
}
fn init($lib: &mut $root::Module) {
$block
}
}
impl $package {
pub fn new() -> Self {
let mut module = $root::Module::new_with_capacity(512);
<Self as $root::packages::Package>::init(&mut module);
Self(module.into())
}
}
};
}