calcit-cli 0.58.1

CLI command handlers, static analysis, diffing, and native FFI host injection for Calcit
Documentation
pub mod call_graph_diff;
pub mod call_tree;
pub mod cli_args;
pub mod def_diff;
pub mod detailed_snapshot;
pub mod effects_graph;
pub mod program_diff;

#[cfg(not(target_arch = "wasm32"))]
pub mod injection;

#[cfg(not(target_arch = "wasm32"))]
pub mod analysis_cache;
#[cfg(not(target_arch = "wasm32"))]
pub mod cli_handlers;
#[cfg(not(target_arch = "wasm32"))]
pub mod deprecated_api;
#[cfg(not(target_arch = "wasm32"))]
pub mod public_api_check;
#[cfg(not(target_arch = "wasm32"))]
pub mod strict_check;
#[cfg(not(target_arch = "wasm32"))]
pub mod type_coverage;
#[cfg(not(target_arch = "wasm32"))]
pub mod verification;

#[cfg(not(target_arch = "wasm32"))]
pub static GLOBAL_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[cfg(not(target_arch = "wasm32"))]
static EMBEDDED_AGENTS_DOC_OVERRIDE: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();

/// Register the embedded `docs/CalcitAgent.md` guide used by `calcit docs agents`.
/// Library callers invoking `cli_handlers::handle_docs_command` without `--refresh`
/// must call this before loading the embedded guide.
#[cfg(not(target_arch = "wasm32"))]
pub fn set_embedded_agents_doc(doc: &'static str) {
  let _ = EMBEDDED_AGENTS_DOC_OVERRIDE.set(doc);
}

#[cfg(not(target_arch = "wasm32"))]
pub fn embedded_agents_doc() -> Option<&'static str> {
  if let Some(doc) = EMBEDDED_AGENTS_DOC_OVERRIDE.get() {
    return Some(*doc);
  }
  #[cfg(test)]
  {
    Some(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/CalcitAgent.md")))
  }
  #[cfg(not(test))]
  {
    None
  }
}

/// Tests load the same embedded core snapshot hundreds of times. Decode it once
/// per test process and let each test clone the files it needs.
#[cfg(not(target_arch = "wasm32"))]
pub fn cached_core_snapshot() -> &'static calcit_core::snapshot::Snapshot {
  calcit_core::core_snapshot().expect("core snapshot should load")
}

#[cfg(not(target_arch = "wasm32"))]
/// Compile both entry points with the normal type context, collecting open dispatches.
pub fn preprocess_dynamic_method_entries(
  entries: &calcit_core::ProgramEntries,
) -> Result<Vec<calcit_core::calcit::LocatedWarning>, String> {
  let warnings = std::cell::RefCell::new(Vec::new());
  for (ns, def) in [(&entries.init_ns, &entries.init_def), (&entries.reload_ns, &entries.reload_def)] {
    calcit_core::runner::preprocess::ensure_ns_def_compiled(ns, def, &warnings, &calcit_core::call_stack::CallStackList::default())
      .map_err(|failure| failure.to_string())?;
  }
  Ok(warnings.into_inner())
}

#[cfg(not(target_arch = "wasm32"))]
pub fn attach_missing_core_namespaces(snapshot: &mut calcit_core::snapshot::Snapshot, core_snapshot: calcit_core::snapshot::Snapshot) {
  for (namespace, file) in core_snapshot.files {
    snapshot.files.entry(namespace).or_insert(file);
  }
}

/// Strict diagnostics are the default, so a missing JS FFI policy becomes an
/// in-memory error default without rewriting an existing Snapshot.
/// An entry that deliberately declares allow/warn/error keeps that choice.
#[cfg(not(target_arch = "wasm32"))]
pub fn apply_strict_feature_policy_defaults(snapshot: &mut calcit_core::snapshot::Snapshot) -> Result<(), String> {
  let entry_name = snapshot.active_entry_name().to_owned();
  let entry = snapshot
    .entries
    .get_mut(&entry_name)
    .ok_or_else(|| format!("Entry '{entry_name}' not found"))?;
  entry
    .feature_policy
    .entry("js-ffi".to_owned())
    .or_insert(calcit_core::snapshot::FeaturePolicy::Error);
  Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
pub struct ProgramOutputGuard {
  redirect_stdout: bool,
  silence_program_output: bool,
}

#[cfg(not(target_arch = "wasm32"))]
impl ProgramOutputGuard {
  pub fn new(redirect_stdout: bool, silence_program_output: bool) -> Self {
    if redirect_stdout {
      injection::set_stdout_to_stderr(true);
    }
    if silence_program_output {
      injection::set_program_output_silenced(true);
    }
    Self {
      redirect_stdout,
      silence_program_output,
    }
  }
}

#[cfg(not(target_arch = "wasm32"))]
impl Drop for ProgramOutputGuard {
  fn drop(&mut self) {
    if self.redirect_stdout {
      injection::set_stdout_to_stderr(false);
    }
    if self.silence_program_output {
      injection::set_program_output_silenced(false);
    }
  }
}