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 lakefile_toml;
30mod limits;
31mod loader;
32pub mod manifest_validation;
33mod modules;
34
35pub use build_helpers::{
36    BuiltLeanCapability, CAPABILITY_MANIFEST_SCHEMA_VERSION, CargoLeanCapability, build_lake_target,
37    build_lake_target_quiet, capability_env_var, capability_manifest_env_var, emit_lean_link_directives,
38    emit_lean_link_directives_checked,
39};
40pub use built_capability::{BuiltCapabilityArtifact, LeanBuiltCapability, LeanBuiltCapabilityError};
41pub use diagnostics::LinkDiagnostics;
42pub use discover::{DiscoverOptions, DiscoverySource, ToolchainInfo, discover_toolchain};
43pub use fingerprint::{HOST_TRIPLE, LAKE_FIXTURE_DIGEST, ToolchainFingerprint};
44pub use lean_rs_sys::{LEAN_HEADER_DIGEST, LEAN_HEADER_PATH, LEAN_VERSION};
45pub use limits::{
46    LEAN_DIAGNOSTIC_BYTE_LIMIT_DEFAULT, LEAN_DIAGNOSTIC_BYTE_LIMIT_MAX, LEAN_HEARTBEAT_LIMIT_DEFAULT,
47    LEAN_HEARTBEAT_LIMIT_MAX,
48};
49pub use loader::{
50    LOADER_DIAGNOSTIC_TEXT_LIMIT, LeanLibraryDependency, LeanLoaderCheck, LeanLoaderDiagnosticCode, LeanLoaderReport,
51    LeanLoaderSeverity, LeanModuleInitializer, bound_loader_text,
52};
53pub use manifest_validation::CapabilityManifest;
54pub use modules::{
55    LeanLakeProjectModules, LeanModuleDescriptor, LeanModuleDiscoveryDiagnostic, LeanModuleDiscoveryOptions,
56    LeanModuleSetFingerprint, discover_lake_modules,
57};
58
59/// Version of the `lean-toolchain` crate, matching `Cargo.toml`.
60pub const VERSION: &str = env!("CARGO_PKG_VERSION");
61
62/// Curated allowlist of `LEAN_EXPORT` symbols the workspace relies on.
63///
64/// Returns [`lean_rs_sys::REQUIRED_SYMBOLS`] directly — the allowlist lives in
65/// exactly one place. Use this through `lean-toolchain` so consumer crates do
66/// not also need a direct `lean-rs-sys` dependency just to enumerate symbol
67/// names.
68#[must_use]
69pub fn required_symbols() -> &'static [&'static str] {
70    lean_rs_sys::REQUIRED_SYMBOLS
71}
72
73#[cfg(test)]
74mod tests {
75    use super::{LEAN_HEADER_DIGEST, LEAN_VERSION, VERSION, required_symbols};
76
77    #[test]
78    fn version_constant_matches_package() {
79        assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
80    }
81
82    #[test]
83    fn required_symbols_is_nonempty() {
84        assert!(!required_symbols().is_empty());
85    }
86
87    #[test]
88    fn lean_metadata_is_baked() {
89        assert!(!LEAN_VERSION.is_empty());
90        assert_eq!(LEAN_HEADER_DIGEST.len(), 64);
91        assert!(LEAN_HEADER_DIGEST.chars().all(|c| c.is_ascii_hexdigit()));
92    }
93}