#![allow(clippy::print_stdout, clippy::expect_used)]
use std::path::PathBuf;
use std::process::ExitCode;
use lean_rs::{LeanResult, LeanRuntime};
use lean_rs_host::meta::{LeanMetaOptions, LeanMetaResponse, infer_type};
use lean_rs_host::{LeanElabOptions, LeanHost};
fn main() -> ExitCode {
install_tracing();
match run() {
Ok(()) => {
println!("ok");
ExitCode::SUCCESS
}
Err(err) => {
eprintln!("[{}] {err}", err.code());
ExitCode::FAILURE
}
}
}
fn install_tracing() {
let filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn"));
let _result = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW)
.try_init();
}
#[allow(
clippy::unwrap_in_result,
reason = "elaboration of `(Nat.succ 0 : Nat)` against the prelude is a fixture invariant; an inner Err here means the toolchain itself has diverged"
)]
fn run() -> LeanResult<()> {
let runtime = LeanRuntime::init()?;
let host = LeanHost::from_lake_project(runtime, lake_project_root())?;
let caps = host.load_capabilities("lean_rs_fixture", "LeanRsFixture")?;
let mut session = caps.session(&["LeanRsHostShims.Meta", "LeanRsHostShims.Elaboration"])?;
let elab_opts = LeanElabOptions::new();
let expr = session
.elaborate("(Nat.succ 0 : Nat)", None, &elab_opts)?
.expect("(Nat.succ 0 : Nat) elaborates against the prelude");
let meta_opts = LeanMetaOptions::new();
let response = session.run_meta(&infer_type(), expr, &meta_opts)?;
match response {
LeanMetaResponse::Ok(_inferred_expr) => {
println!("status=Ok service=infer_type");
}
LeanMetaResponse::Failed(failure) => {
println!("status=Failed: {failure}");
}
LeanMetaResponse::TimeoutOrHeartbeat(failure) => {
println!("status=TimeoutOrHeartbeat: {failure}");
}
LeanMetaResponse::Unsupported(failure) => {
println!("status=Unsupported: {failure}");
}
other => {
println!("status=<non-exhaustive>: {other:?}");
}
}
Ok(())
}
fn lake_project_root() -> PathBuf {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
manifest_dir.parent().and_then(std::path::Path::parent).map_or_else(
|| PathBuf::from("fixtures/lean"),
|workspace| workspace.join("fixtures").join("lean"),
)
}