codingest_cli/lib.rs
1//! Shared implementation of the `codingest` CLI.
2//!
3//! `codingest` builds/checks a `.kgl` code graph from a checkout or one or more
4//! git revisions and installs Codingest's code-review Agent Skill. The
5//! `CodeTreeCommand` variants are the binary's top-level commands.
6//!
7//! Pure-Rust over the sibling `codingest` builder + `kglite::api::io` (no
8//! libpython link in the standalone binary). The `pip install codingest` wheel
9//! links this same library through `_run_cli`, so command parsing and behavior
10//! cannot drift between the cargo binary and the console script.
11
12mod code_tree_cli;
13mod query;
14mod skill;
15mod skill_assets;
16
17use std::ffi::OsString;
18
19use anyhow::Result;
20use clap::Parser;
21
22pub use query::StaleGraph;
23
24/// Process exit code for an error returned by [`run`] — the CI contract.
25///
26/// `0` success and `2` usage errors are clap's, emitted before [`run`] is
27/// reached. `3` is reserved for a `--require-fresh` refusal so a pipeline can
28/// tell "the graph is stale" apart from "the query failed"; every other
29/// operational failure (missing artifact, bad Cypher, timeout, I/O) is `1`.
30///
31/// The `pip install codingest` wheel's `_run_cli` maps every error to
32/// `PyRuntimeError`, so a stale refusal exits `1` through the console script.
33pub fn exit_code_for(error: &anyhow::Error) -> i32 {
34 if error.downcast_ref::<StaleGraph>().is_some() {
35 3
36 } else {
37 1
38 }
39}
40
41#[derive(Parser, Debug)]
42#[command(name = "codingest", version, about)]
43struct Cli {
44 #[command(subcommand)]
45 command: code_tree_cli::CodeTreeCommand,
46}
47
48/// Run the CLI over an explicit argument vector, including the program name.
49///
50/// The standalone binary and the `pip install codingest` wheel shim both call
51/// this entry point, so command parsing and behavior cannot drift.
52pub fn run<I, T>(args: I) -> Result<()>
53where
54 I: IntoIterator<Item = T>,
55 T: Into<OsString> + Clone,
56{
57 let cli = Cli::parse_from(args);
58 code_tree_cli::run(&cli.command)
59}