use crate::func::Function;
pub struct Library {
pub name: String,
pub namespace: bool,
pub(crate) libs: Vec<Library>,
pub(crate) funcs: Vec<Function>,
}
impl Library {
pub fn new(name: impl Into<String>, namespace: bool) -> Self {
Self {
name: name.into(),
namespace,
libs: Vec::new(),
funcs: Vec::new(),
}
}
pub fn add_lib(&mut self, lib: Library) {
let exists = {
let mut exists = false;
for l in &self.libs {
if l.name == lib.name { exists = true; }
}
exists
};
if !exists { self.libs.push(lib); }
}
pub fn add_func(&mut self, func: Function) {
let exists = {
let mut exists = false;
for f in &self.funcs {
if f.name == func.name { exists = true; }
}
exists
};
if !exists { self.funcs.push(func); }
}
pub fn get_lib(&self, name: impl Into<String>) -> Option<&Library> {
let name = name.into();
for lib in &self.libs {
if lib.name == name.clone() {
return Some(lib);
}
}
None
}
pub fn get_func(&self, name: impl Into<String>) -> Option<&Function> {
let name = name.into();
for func in &self.funcs {
if func.name == name.clone() {
return Some(func);
}
}
for lib in &self.libs {
if lib.namespace {
continue;
}
if let Some(func) = lib.get_func(name.clone()) {
return Some(func);
}
}
None
}
}