Skip to main content

cc_toolgate/
lib.rs

1//! cc-toolgate: a PreToolUse hook for Claude Code that gates Bash commands.
2//!
3//! This crate evaluates shell commands against configurable rules and returns
4//! one of three decisions: [`eval::Decision::Allow`], [`eval::Decision::Ask`],
5//! or [`eval::Decision::Deny`]. Commands are parsed into an AST using
6//! tree-sitter-bash, split into segments, and each segment is evaluated
7//! against a [`CommandRegistry`](crate::eval::CommandRegistry) built from configuration.
8//!
9//! # Architecture
10//!
11//! - **`agent-shell-parser`** — Shell parsing: tree-sitter-bash AST walker, shlex tokenizer, type definitions (external crate).
12//! - **[`eval`]** — Evaluation engine: command registry, decision types, per-segment context.
13//! - **[`commands`]** — Command specs: per-tool evaluation logic (git, cargo, kubectl, gh, etc.).
14//! - **[`config`]** — Configuration loading: embedded defaults + user overlay merge.
15//! - **[`logging`]** — Decision logging to `~/.local/share/cc-toolgate/decisions.log`.
16
17/// Command spec trait and per-tool implementations.
18pub mod commands;
19/// Configuration types, loading, and overlay merge logic.
20pub mod config;
21/// Evaluation engine: registry, decision aggregation, command context.
22pub mod eval;
23/// File-based decision logging.
24pub mod logging;
25
26use eval::RuleMatch;
27
28/// Build the registry from default config and evaluate a command string.
29///
30/// This is the main entry point for tests and simple usage.
31/// For CLI usage with --escalate-deny or user config, build the registry directly.
32pub fn evaluate(command: &str) -> RuleMatch {
33    let config = config::Config::default_config();
34    let registry = eval::CommandRegistry::from_config(&config);
35    registry.evaluate(command)
36}