#![forbid(unsafe_code)]
mod command;
mod execute;
mod lsp;
mod parse;
pub enum RunFailure {
PolicyViolation(String),
Tool(String),
}
impl std::fmt::Display for RunFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RunFailure::PolicyViolation(msg) | RunFailure::Tool(msg) => f.write_str(msg),
}
}
}
impl From<String> for RunFailure {
fn from(msg: String) -> Self {
RunFailure::Tool(msg)
}
}
pub fn run(args: impl IntoIterator<Item = String>) -> Result<(), RunFailure> {
let command = parse::parse(args).map_err(RunFailure::Tool)?;
execute::execute(command)
}
#[cfg(test)]
mod tests {
use super::RunFailure;
#[test]
fn string_converts_to_tool_variant() {
let f: RunFailure = "some tool error".to_string().into();
assert!(
matches!(f, RunFailure::Tool(_)),
"From<String> must map to RunFailure::Tool, not PolicyViolation"
);
}
#[test]
fn display_shows_inner_message() {
let tool = RunFailure::Tool("tool message".to_string());
assert_eq!(tool.to_string(), "tool message");
let policy = RunFailure::PolicyViolation("policy message".to_string());
assert_eq!(policy.to_string(), "policy message");
}
}