Skip to main content

calcit_cli/
lib.rs

1pub mod call_graph_diff;
2pub mod call_tree;
3pub mod cli_args;
4pub mod def_diff;
5pub mod detailed_snapshot;
6pub mod effects_graph;
7pub mod program_diff;
8
9#[cfg(not(target_arch = "wasm32"))]
10pub mod injection;
11
12#[cfg(not(target_arch = "wasm32"))]
13pub mod analysis_cache;
14#[cfg(not(target_arch = "wasm32"))]
15pub mod cli_handlers;
16#[cfg(not(target_arch = "wasm32"))]
17pub mod deprecated_api;
18#[cfg(not(target_arch = "wasm32"))]
19pub mod public_api_check;
20#[cfg(not(target_arch = "wasm32"))]
21pub mod strict_check;
22#[cfg(not(target_arch = "wasm32"))]
23pub mod type_coverage;
24#[cfg(not(target_arch = "wasm32"))]
25pub mod verification;
26
27#[cfg(not(target_arch = "wasm32"))]
28pub static GLOBAL_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
29
30#[cfg(not(target_arch = "wasm32"))]
31static EMBEDDED_AGENTS_DOC_OVERRIDE: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();
32
33/// Register the embedded `docs/CalcitAgent.md` guide used by `calcit docs agents`.
34/// Library callers invoking `cli_handlers::handle_docs_command` without `--refresh`
35/// must call this before loading the embedded guide.
36#[cfg(not(target_arch = "wasm32"))]
37pub fn set_embedded_agents_doc(doc: &'static str) {
38  let _ = EMBEDDED_AGENTS_DOC_OVERRIDE.set(doc);
39}
40
41#[cfg(not(target_arch = "wasm32"))]
42pub fn embedded_agents_doc() -> Option<&'static str> {
43  if let Some(doc) = EMBEDDED_AGENTS_DOC_OVERRIDE.get() {
44    return Some(*doc);
45  }
46  #[cfg(test)]
47  {
48    Some(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/CalcitAgent.md")))
49  }
50  #[cfg(not(test))]
51  {
52    None
53  }
54}
55
56/// Tests load the same embedded core snapshot hundreds of times. Decode it once
57/// per test process and let each test clone the files it needs.
58#[cfg(not(target_arch = "wasm32"))]
59pub fn cached_core_snapshot() -> &'static calcit_core::snapshot::Snapshot {
60  calcit_core::core_snapshot().expect("core snapshot should load")
61}
62
63#[cfg(not(target_arch = "wasm32"))]
64/// Compile both entry points with the normal type context, collecting open dispatches.
65pub fn preprocess_dynamic_method_entries(
66  entries: &calcit_core::ProgramEntries,
67) -> Result<Vec<calcit_core::calcit::LocatedWarning>, String> {
68  let warnings = std::cell::RefCell::new(Vec::new());
69  for (ns, def) in [(&entries.init_ns, &entries.init_def), (&entries.reload_ns, &entries.reload_def)] {
70    calcit_core::runner::preprocess::ensure_ns_def_compiled(ns, def, &warnings, &calcit_core::call_stack::CallStackList::default())
71      .map_err(|failure| failure.to_string())?;
72  }
73  Ok(warnings.into_inner())
74}
75
76#[cfg(not(target_arch = "wasm32"))]
77pub fn attach_missing_core_namespaces(snapshot: &mut calcit_core::snapshot::Snapshot, core_snapshot: calcit_core::snapshot::Snapshot) {
78  for (namespace, file) in core_snapshot.files {
79    snapshot.files.entry(namespace).or_insert(file);
80  }
81}
82
83/// Strict diagnostics are the default, so a missing JS FFI policy becomes an
84/// in-memory error default without rewriting an existing Snapshot.
85/// An entry that deliberately declares allow/warn/error keeps that choice.
86#[cfg(not(target_arch = "wasm32"))]
87pub fn apply_strict_feature_policy_defaults(snapshot: &mut calcit_core::snapshot::Snapshot) -> Result<(), String> {
88  let entry_name = snapshot.active_entry_name().to_owned();
89  let entry = snapshot
90    .entries
91    .get_mut(&entry_name)
92    .ok_or_else(|| format!("Entry '{entry_name}' not found"))?;
93  entry
94    .feature_policy
95    .entry("js-ffi".to_owned())
96    .or_insert(calcit_core::snapshot::FeaturePolicy::Error);
97  Ok(())
98}
99
100#[cfg(not(target_arch = "wasm32"))]
101pub struct ProgramOutputGuard {
102  redirect_stdout: bool,
103  silence_program_output: bool,
104}
105
106#[cfg(not(target_arch = "wasm32"))]
107impl ProgramOutputGuard {
108  pub fn new(redirect_stdout: bool, silence_program_output: bool) -> Self {
109    if redirect_stdout {
110      injection::set_stdout_to_stderr(true);
111    }
112    if silence_program_output {
113      injection::set_program_output_silenced(true);
114    }
115    Self {
116      redirect_stdout,
117      silence_program_output,
118    }
119  }
120}
121
122#[cfg(not(target_arch = "wasm32"))]
123impl Drop for ProgramOutputGuard {
124  fn drop(&mut self) {
125    if self.redirect_stdout {
126      injection::set_stdout_to_stderr(false);
127    }
128    if self.silence_program_output {
129      injection::set_program_output_silenced(false);
130    }
131  }
132}