use std::path::{Path, PathBuf};
use super::preflight::{CapabilityManifest, LeanCapabilityPreflight, manifest_error_to_lean_error};
use super::{LeanLibrary, LeanLibraryBundle, LeanLibraryDependency, LeanModule};
use crate::error::{LeanError, LeanResult};
use crate::runtime::LeanRuntime;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LeanBuiltCapability {
dylib_path: Option<PathBuf>,
env_var: Option<String>,
manifest_path: Option<PathBuf>,
manifest_env_var: Option<String>,
package: Option<String>,
module: Option<String>,
dependencies: Vec<LeanLibraryDependency>,
}
impl LeanBuiltCapability {
#[must_use]
pub fn path(path: impl Into<PathBuf>) -> Self {
Self {
dylib_path: Some(path.into()),
env_var: None,
manifest_path: None,
manifest_env_var: None,
package: None,
module: None,
dependencies: Vec::new(),
}
}
#[must_use]
pub fn env(env_var: impl Into<String>) -> Self {
Self {
dylib_path: None,
env_var: Some(env_var.into()),
manifest_path: None,
manifest_env_var: None,
package: None,
module: None,
dependencies: Vec::new(),
}
}
#[must_use]
pub fn manifest_path(path: impl Into<PathBuf>) -> Self {
Self {
dylib_path: None,
env_var: None,
manifest_path: Some(path.into()),
manifest_env_var: None,
package: None,
module: None,
dependencies: Vec::new(),
}
}
#[must_use]
pub fn manifest_env(env_var: impl Into<String>) -> Self {
Self {
dylib_path: None,
env_var: None,
manifest_path: None,
manifest_env_var: Some(env_var.into()),
package: None,
module: None,
dependencies: Vec::new(),
}
}
#[must_use]
pub fn env_var(mut self, env_var: impl Into<String>) -> Self {
self.env_var = Some(env_var.into());
self
}
#[must_use]
pub fn manifest_env_var(mut self, env_var: impl Into<String>) -> Self {
self.manifest_env_var = Some(env_var.into());
self
}
#[must_use]
pub fn package(mut self, package: impl Into<String>) -> Self {
self.package = Some(package.into());
self
}
#[must_use]
pub fn module(mut self, module: impl Into<String>) -> Self {
self.module = Some(module.into());
self
}
#[must_use]
pub fn dependency(mut self, dependency: LeanLibraryDependency) -> Self {
self.dependencies.push(dependency);
self
}
#[must_use]
pub fn dependencies(mut self, dependencies: impl IntoIterator<Item = LeanLibraryDependency>) -> Self {
self.dependencies.extend(dependencies);
self
}
#[must_use]
pub fn package_name(&self) -> Option<&str> {
self.package.as_deref()
}
#[must_use]
pub fn module_name(&self) -> Option<&str> {
self.module.as_deref()
}
#[must_use]
pub fn dependency_descriptors(&self) -> &[LeanLibraryDependency] {
&self.dependencies
}
pub fn dylib_path(&self) -> LeanResult<PathBuf> {
if let Some(path) = &self.dylib_path {
return Ok(path.clone());
}
let env_var = self.env_var.as_deref().ok_or_else(|| {
LeanError::module_init("LeanBuiltCapability needs either a dylib path or an environment variable")
})?;
std::env::var_os(env_var).map(PathBuf::from).ok_or_else(|| {
LeanError::module_init(format!(
"environment variable {env_var} is not set for Lean capability dylib"
))
})
}
pub fn resolved_manifest_path(&self) -> LeanResult<PathBuf> {
if let Some(path) = &self.manifest_path {
return Ok(path.clone());
}
let env_var = self.manifest_env_var.as_deref().ok_or_else(|| {
LeanError::module_init("LeanBuiltCapability needs either a manifest path or manifest environment variable")
})?;
std::env::var_os(env_var).map(PathBuf::from).ok_or_else(|| {
LeanError::module_init(format!(
"environment variable {env_var} is not set for Lean capability manifest"
))
})
}
}
impl From<&lean_toolchain::BuiltLeanCapability> for LeanBuiltCapability {
fn from(value: &lean_toolchain::BuiltLeanCapability) -> Self {
Self {
dylib_path: Some(value.dylib_path().to_path_buf()),
env_var: Some(value.env_var().to_owned()),
manifest_path: Some(value.manifest_path().to_path_buf()),
manifest_env_var: Some(value.manifest_env_var().to_owned()),
package: Some(value.package().to_owned()),
module: Some(value.module().to_owned()),
dependencies: Vec::new(),
}
}
}
pub struct LeanCapability<'lean> {
bundle: LeanLibraryBundle<'lean>,
package: String,
module: String,
}
impl<'lean> LeanCapability<'lean> {
#[allow(clippy::needless_pass_by_value)]
pub fn from_build_manifest(runtime: &'lean LeanRuntime, spec: LeanBuiltCapability) -> LeanResult<Self> {
let report = LeanCapabilityPreflight::new(spec.clone()).check();
if !report.is_ok() {
return Err(report.into_error());
}
let manifest_path = spec.resolved_manifest_path()?;
let manifest = CapabilityManifest::read(&manifest_path).map_err(manifest_error_to_lean_error)?;
Self::open_with_dependencies(
runtime,
manifest.primary_dylib,
manifest.package,
manifest.module,
manifest.dependencies,
)
}
pub fn from_build_env(runtime: &'lean LeanRuntime, mut spec: LeanBuiltCapability) -> LeanResult<Self> {
let dylib_path = spec.dylib_path()?;
let package = spec.package.take().ok_or_else(|| {
LeanError::linking("LeanBuiltCapability is missing the Lake package name; call `.package(...)`")
})?;
let module = spec.module.take().ok_or_else(|| {
LeanError::linking("LeanBuiltCapability is missing the root Lean module name; call `.module(...)`")
})?;
Self::open_with_dependencies(runtime, dylib_path, package, module, spec.dependencies)
}
pub fn open(
runtime: &'lean LeanRuntime,
dylib_path: impl AsRef<Path>,
package: impl Into<String>,
module: impl Into<String>,
) -> LeanResult<Self> {
let package = package.into();
let module = module.into();
Self::open_with_dependencies(runtime, dylib_path, package, module, [])
}
pub fn open_with_dependencies(
runtime: &'lean LeanRuntime,
dylib_path: impl AsRef<Path>,
package: impl Into<String>,
module: impl Into<String>,
dependencies: impl IntoIterator<Item = LeanLibraryDependency>,
) -> LeanResult<Self> {
let package = package.into();
let module = module.into();
let bundle = LeanLibraryBundle::open(runtime, dylib_path, dependencies)?;
let _module = bundle.initialize_module(&package, &module)?;
Ok(Self {
bundle,
package,
module,
})
}
pub fn module(&self) -> LeanResult<LeanModule<'lean, '_>> {
self.bundle.initialize_module(&self.package, &self.module)
}
#[must_use]
pub fn library(&self) -> &LeanLibrary<'lean> {
self.bundle.library()
}
#[must_use]
pub fn bundle(&self) -> &LeanLibraryBundle<'lean> {
&self.bundle
}
#[must_use]
pub fn package_name(&self) -> &str {
&self.package
}
#[must_use]
pub fn module_name(&self) -> &str {
&self.module
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic)]
mod tests {
use super::{CapabilityManifest, LeanBuiltCapability, LeanLibraryDependency};
use std::fs;
use std::path::PathBuf;
#[test]
fn built_capability_path_is_resolved_without_runtime_env() {
let spec = LeanBuiltCapability::path("/tmp/libcap.so")
.env_var("LEAN_RS_CAPABILITY_CAP_DYLIB")
.package("pkg")
.module("Cap");
let path = match spec.dylib_path() {
Ok(path) => path,
Err(err) => panic!("expected path, got {err}"),
};
assert_eq!(path, std::path::PathBuf::from("/tmp/libcap.so"));
assert_eq!(spec.package_name(), Some("pkg"));
assert_eq!(spec.module_name(), Some("Cap"));
}
#[test]
fn missing_runtime_env_is_typed() {
let spec = LeanBuiltCapability::env("LEAN_RS_TEST_MISSING_CAPABILITY_DYLIB")
.package("pkg")
.module("Cap");
let err = match spec.dylib_path() {
Ok(path) => panic!("expected missing env error, got {}", path.display()),
Err(err) => err,
};
assert_eq!(err.code(), crate::LeanDiagnosticCode::ModuleInit);
}
#[test]
fn missing_runtime_manifest_env_is_typed() {
let spec = LeanBuiltCapability::manifest_env("LEAN_RS_TEST_MISSING_CAPABILITY_MANIFEST");
let err = match spec.resolved_manifest_path() {
Ok(path) => panic!("expected missing manifest env error, got {}", path.display()),
Err(err) => err,
};
assert_eq!(err.code(), crate::LeanDiagnosticCode::ModuleInit);
}
#[test]
fn manifest_descriptor_parses_dependencies() {
let path = temp_manifest_path("manifest_descriptor_parses_dependencies");
write_manifest(
&path,
r#"{
"schema_version": 1,
"target_name": "Cap",
"package": "pkg",
"module": "Cap",
"primary_dylib": "/tmp/libcap.so",
"dependencies": [
{
"dylib_path": "/tmp/libdep.so",
"export_symbols_for_dependents": true,
"initializer": { "package": "dep_pkg", "module": "Dep" }
}
]
}"#,
);
let manifest = match CapabilityManifest::read(&path) {
Ok(manifest) => manifest,
Err(err) => panic!("expected manifest to parse, got {err}"),
};
assert_eq!(manifest.primary_dylib, PathBuf::from("/tmp/libcap.so"));
assert_eq!(manifest.package, "pkg");
assert_eq!(manifest.module, "Cap");
assert_eq!(manifest.dependencies.len(), 1);
let Some(dependency) = manifest.dependencies.first() else {
panic!("expected one dependency");
};
assert!(dependency.exports_symbols_for_dependents());
assert_eq!(dependency.path_ref(), std::path::Path::new("/tmp/libdep.so"));
let Some(initializer) = dependency.module_initializer() else {
panic!("expected dependency initializer");
};
assert_eq!(initializer.package_name(), "dep_pkg");
assert_eq!(initializer.module_name(), "Dep");
}
#[test]
fn unsupported_manifest_schema_is_typed() {
let path = temp_manifest_path("unsupported_manifest_schema_is_typed");
write_manifest(
&path,
r#"{
"schema_version": 999,
"package": "pkg",
"module": "Cap",
"primary_dylib": "/tmp/libcap.so"
}"#,
);
let Err(err) = CapabilityManifest::read(&path) else {
panic!("expected unsupported schema error");
};
assert_eq!(err.code(), crate::LeanLoaderDiagnosticCode::UnsupportedManifestSchema);
assert!(err.message().contains("unsupported Lean capability manifest schema"));
}
#[test]
fn built_capability_records_dependency_descriptors() {
let spec = LeanBuiltCapability::path("/tmp/libcap.so").dependency(
LeanLibraryDependency::path("/tmp/libdep.so")
.export_symbols_for_dependents()
.initializer("dep_pkg", "Dep"),
);
let dependencies = spec.dependency_descriptors();
assert_eq!(dependencies.len(), 1);
let Some(dependency) = dependencies.first() else {
panic!("expected one dependency descriptor");
};
assert!(dependency.exports_symbols_for_dependents());
let Some(initializer) = dependency.module_initializer() else {
panic!("dependency initializer is recorded");
};
assert_eq!(initializer.package_name(), "dep_pkg");
assert_eq!(initializer.module_name(), "Dep");
}
fn temp_manifest_path(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("lean-rs-manifest-{}-{name}", std::process::id()));
drop(fs::remove_dir_all(&dir));
fs::create_dir_all(&dir).expect("create manifest test dir");
dir.join("capability.json")
}
fn write_manifest(path: &std::path::Path, contents: &str) {
fs::write(path, contents).expect("write manifest fixture");
}
}