Skip to main content

lean_toolchain/
lib.rs

1//! Lean 4 toolchain discovery, fingerprinting, allowlist re-export, and build-script helpers.
2//!
3//! Sits one layer above the workspace crate [`lean_rs_sys`], which owns the raw `extern "C"`
4//! declarations, the hand-written refcount inline helpers, the signature-checked symbol
5//! allowlist, the header SHA-256 digest, and the link directives. This crate composes on top:
6//! a typed [`ToolchainFingerprint`], the workspace-only Lake [`LAKE_FIXTURE_DIGEST`], a layered
7//! [`LinkDiagnostics`] error type, and reusable build-script helpers
8//! ([`emit_lean_link_directives_checked`], [`build_lake_target`],
9//! [`build_lake_target_quiet`]) that downstream embedders and higher layers can use to get
10//! consistent link/rerun directives and Lake dylib paths without duplicating policy.
11//!
12//! ## Single typed entry point
13//!
14//! [`LEAN_VERSION`], [`LEAN_HEADER_PATH`], and [`LEAN_HEADER_DIGEST`] are re-exported from
15//! [`lean_rs_sys`] so embedders that depend on this crate need only one import for build
16//! metadata. The allowlist comes through [`required_symbols`] (no copy).
17//!
18//! ## Layering
19//!
20//! `lean-rs-sys → lean-toolchain → lean-rs`. Raw `lean_*` symbols never appear in this
21//! crate's public surface — they remain in `lean-rs-sys` and reach the safe layers through
22//! `lean-rs`'s `pub(crate)` modules.
23
24mod build_helpers;
25mod built_capability;
26mod diagnostics;
27mod discover;
28mod fingerprint;
29mod limits;
30mod loader;
31pub mod manifest_validation;
32mod modules;
33
34pub use build_helpers::{
35    BuiltLeanCapability, CAPABILITY_MANIFEST_SCHEMA_VERSION, CargoLeanCapability, build_lake_target,
36    build_lake_target_quiet, capability_env_var, capability_manifest_env_var, emit_lean_link_directives,
37    emit_lean_link_directives_checked,
38};
39pub use built_capability::{BuiltCapabilityArtifact, LeanBuiltCapability, LeanBuiltCapabilityError};
40pub use diagnostics::LinkDiagnostics;
41pub use discover::{DiscoverOptions, DiscoverySource, ToolchainInfo, discover_toolchain};
42pub use fingerprint::{HOST_TRIPLE, LAKE_FIXTURE_DIGEST, ToolchainFingerprint};
43pub use lean_rs_sys::{LEAN_HEADER_DIGEST, LEAN_HEADER_PATH, LEAN_VERSION};
44pub use limits::{
45    LEAN_DIAGNOSTIC_BYTE_LIMIT_DEFAULT, LEAN_DIAGNOSTIC_BYTE_LIMIT_MAX, LEAN_HEARTBEAT_LIMIT_DEFAULT,
46    LEAN_HEARTBEAT_LIMIT_MAX,
47};
48pub use loader::{
49    LOADER_DIAGNOSTIC_TEXT_LIMIT, LeanLibraryDependency, LeanLoaderCheck, LeanLoaderDiagnosticCode, LeanLoaderReport,
50    LeanLoaderSeverity, LeanModuleInitializer, bound_loader_text,
51};
52pub use manifest_validation::CapabilityManifest;
53pub use modules::{
54    LeanLakeProjectModules, LeanModuleDescriptor, LeanModuleDiscoveryDiagnostic, LeanModuleDiscoveryOptions,
55    LeanModuleSetFingerprint, discover_lake_modules,
56};
57
58/// Version of the `lean-toolchain` crate, matching `Cargo.toml`.
59pub const VERSION: &str = env!("CARGO_PKG_VERSION");
60
61/// Curated allowlist of `LEAN_EXPORT` symbols the workspace relies on.
62///
63/// Returns [`lean_rs_sys::REQUIRED_SYMBOLS`] directly — the allowlist lives in
64/// exactly one place. Use this through `lean-toolchain` so consumer crates do
65/// not also need a direct `lean-rs-sys` dependency just to enumerate symbol
66/// names.
67#[must_use]
68pub fn required_symbols() -> &'static [&'static str] {
69    lean_rs_sys::REQUIRED_SYMBOLS
70}
71
72#[cfg(test)]
73mod tests {
74    use super::{LEAN_HEADER_DIGEST, LEAN_VERSION, VERSION, required_symbols};
75
76    #[test]
77    fn version_constant_matches_package() {
78        assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
79    }
80
81    #[test]
82    fn required_symbols_is_nonempty() {
83        assert!(!required_symbols().is_empty());
84    }
85
86    #[test]
87    fn lean_metadata_is_baked() {
88        assert!(!LEAN_VERSION.is_empty());
89        assert_eq!(LEAN_HEADER_DIGEST.len(), 64);
90        assert!(LEAN_HEADER_DIGEST.chars().all(|c| c.is_ascii_hexdigit()));
91    }
92}