use std::path::Path;
use super::{LeanLibrary, LeanModule};
use crate::error::LeanResult;
use crate::runtime::LeanRuntime;
pub use lean_toolchain::{LeanLibraryDependency, LeanModuleInitializer};
pub struct LeanLibraryBundle<'lean> {
primary: LeanLibrary<'lean>,
dependencies: Vec<LeanLibrary<'lean>>,
}
impl<'lean> LeanLibraryBundle<'lean> {
pub fn open(
runtime: &'lean LeanRuntime,
primary_path: impl AsRef<Path>,
dependencies: impl IntoIterator<Item = LeanLibraryDependency>,
) -> LeanResult<Self> {
let mut opened_dependencies = Vec::new();
for dependency in dependencies {
let library = if dependency.exports_symbols_for_dependents() {
LeanLibrary::open_globally(runtime, dependency.path_ref())?
} else {
LeanLibrary::open(runtime, dependency.path_ref())?
};
if let Some(initializer) = dependency.into_module_initializer() {
let _module = library.initialize_module(initializer.package_name(), initializer.module_name())?;
}
opened_dependencies.push(library);
}
let primary = LeanLibrary::open(runtime, primary_path)?;
Ok(Self {
primary,
dependencies: opened_dependencies,
})
}
pub fn initialize_module<'bundle>(
&'bundle self,
package: &str,
module: &str,
) -> LeanResult<LeanModule<'lean, 'bundle>> {
self.primary.initialize_module(package, module)
}
#[must_use]
pub fn library(&self) -> &LeanLibrary<'lean> {
&self.primary
}
#[must_use]
pub fn dependency_count(&self) -> usize {
self.dependencies.len()
}
}
impl std::fmt::Debug for LeanLibraryBundle<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LeanLibraryBundle")
.field("primary", &self.primary)
.field("dependency_count", &self.dependencies.len())
.finish()
}
}