1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Command-line argument definitions.
use clap::{Parser, Subcommand};
use std::path::PathBuf;
/// guardrail-rs: a zero-Python LLM security proxy.
#[derive(Debug, Parser)]
#[command(name = "guardrail", version, about, long_about = None)]
pub struct Cli {
/// Subcommand to run.
#[command(subcommand)]
pub command: Command,
}
/// Top-level subcommands.
#[derive(Debug, Subcommand)]
pub enum Command {
/// Start the proxy server.
Run {
/// Path to the TOML configuration file.
///
/// Falls back to the `GUARDRAIL_CONFIG` environment variable if the
/// flag is not given, then to `guardrail.toml` in the current
/// directory if neither is set.
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
/// Validate a configuration file without starting the server.
Validate {
/// Path to the TOML configuration file.
///
/// Falls back to the `GUARDRAIL_CONFIG` environment variable if the
/// flag is not given, then to `guardrail.toml` in the current
/// directory if neither is set.
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
/// Run a single text payload through the pipeline and print the decision.
///
/// Useful for testing rules and policies without running a full server.
Check {
/// The text to evaluate (treated as a single user message).
text: String,
/// Path to the TOML configuration file.
///
/// Falls back to the `GUARDRAIL_CONFIG` environment variable if the
/// flag is not given, then to `guardrail.toml` in the current
/// directory if neither is set.
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
}
#[cfg(test)]
mod tests {
use super::*;
/// All `GUARDRAIL_CONFIG` behavior is tested in a single function rather
/// than separate `#[test]` functions, because `std::env::set_var` /
/// `remove_var` mutate process-global state and `cargo test`/`nextest`
/// run tests in parallel by default — separate functions touching the
/// same env var would race against each other.
#[test]
fn test_guardrail_config_env_var_behavior() {
// 1. No env var, no flag: falls back to the hardcoded default.
std::env::remove_var("GUARDRAIL_CONFIG");
let cli = Cli::parse_from(["guardrail", "run"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("guardrail.toml")),
_ => panic!("expected Run command"),
}
// 2. Env var set, no flag: env var wins over the hardcoded default.
std::env::set_var("GUARDRAIL_CONFIG", "/from/env.toml");
let cli = Cli::parse_from(["guardrail", "run"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("/from/env.toml")),
_ => panic!("expected Run command"),
}
// 3. Env var set AND flag given: explicit flag wins over the env var.
let cli = Cli::parse_from(["guardrail", "run", "--config", "/from/flag.toml"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("/from/flag.toml")),
_ => panic!("expected Run command"),
}
// 4. Same env-var fallback applies to `validate`.
let cli = Cli::parse_from(["guardrail", "validate"]);
match cli.command {
Command::Validate { config } => assert_eq!(config, PathBuf::from("/from/env.toml")),
_ => panic!("expected Validate command"),
}
// 5. Same env-var fallback applies to `check`.
let cli = Cli::parse_from(["guardrail", "check", "hello world"]);
match cli.command {
Command::Check { text, config } => {
assert_eq!(text, "hello world");
assert_eq!(config, PathBuf::from("/from/env.toml"));
}
_ => panic!("expected Check command"),
}
// Clean up so other tests in this process don't observe a stale value.
std::env::remove_var("GUARDRAIL_CONFIG");
}
}