implemented proper error handling, added README
This commit is contained in:
+32
-2
@@ -1,3 +1,4 @@
|
||||
use anyhow::{bail, Result};
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Default, Debug)]
|
||||
@@ -22,11 +23,40 @@ pub struct Arguments {
|
||||
}
|
||||
|
||||
impl Arguments {
|
||||
pub fn validate(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.action == "up" || self.action == "down" {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err("Action values are up or down.".into())
|
||||
bail!("Action values are up or down.")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn args_with_action(action: &str) -> Arguments {
|
||||
Arguments {
|
||||
project: "test".to_string(),
|
||||
action: action.to_string(),
|
||||
legacy: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_accepts_up() {
|
||||
assert!(args_with_action("up").validate().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_accepts_down() {
|
||||
assert!(args_with_action("down").validate().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_invalid() {
|
||||
let err = args_with_action("start").validate().unwrap_err();
|
||||
assert!(err.to_string().contains("up or down"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user