use core::fmt;
use crate::host::host::LeanHost;
use crate::host::session::{LeanSession, SessionSymbols};
use lean_rs::error::LeanResult;
use lean_rs::module::LeanLibrary;
pub struct LeanCapabilities<'lean, 'h> {
host: &'h LeanHost<'lean>,
user_library: LeanLibrary<'lean>,
#[allow(dead_code, reason = "Drop releases the dylib; field is structurally required")]
shim_library: LeanLibrary<'lean>,
symbols: SessionSymbols,
}
impl fmt::Debug for LeanCapabilities<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LeanCapabilities").finish_non_exhaustive()
}
}
impl<'lean, 'h> LeanCapabilities<'lean, 'h> {
pub(crate) fn new(
host: &'h LeanHost<'lean>,
user_library: LeanLibrary<'lean>,
package: &str,
lib_name: &str,
) -> LeanResult<Self> {
let shim_dylib_path = host.project().shim_dylib()?;
let shim_library = LeanLibrary::open_globally(host.runtime(), &shim_dylib_path)?;
let _shim_module =
shim_library.initialize_module(crate::host::lake::SHIM_PACKAGE_NAME, crate::host::lake::SHIM_LIB_NAME)?;
let _user_module = user_library.initialize_module(package, lib_name)?;
let symbols = SessionSymbols::resolve(&shim_library)?;
Ok(Self {
host,
user_library,
shim_library,
symbols,
})
}
pub fn session<'c>(&'c self, imports: &[&str]) -> LeanResult<LeanSession<'lean, 'c>> {
LeanSession::import(self, imports)
}
pub(crate) fn host(&self) -> &'h LeanHost<'lean> {
self.host
}
pub(crate) fn symbols(&self) -> &SessionSymbols {
&self.symbols
}
pub(crate) fn library(&self) -> &LeanLibrary<'lean> {
&self.user_library
}
}