From 7dce9b1c8acb4a57390a037d23e8cd4ce01dd914 Mon Sep 17 00:00:00 2001 From: a14m Date: Thu, 1 Apr 2021 23:37:09 +0200 Subject: Add the delay opt validations --- CLI/rust/src/game_of_life.rs | 1 + CLI/rust/src/game_of_life/opts.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/CLI/rust/src/game_of_life.rs b/CLI/rust/src/game_of_life.rs index c854e7f..4b1e979 100644 --- a/CLI/rust/src/game_of_life.rs +++ b/CLI/rust/src/game_of_life.rs @@ -66,6 +66,7 @@ pub(crate) fn options() -> Result { .short('d') .long("delay") .default_value("50") + .validator(opts::validators::is_positive) .display_order(6) ) .get_matches(); diff --git a/CLI/rust/src/game_of_life/opts.rs b/CLI/rust/src/game_of_life/opts.rs index c0ece98..5e52715 100644 --- a/CLI/rust/src/game_of_life/opts.rs +++ b/CLI/rust/src/game_of_life/opts.rs @@ -37,4 +37,13 @@ pub(crate) mod validators { Err(e) => Err(e.to_string()), } } + + /// Validator to check if delay value is positive + pub(crate) fn is_positive(val: &str) -> Result<(), String> { + match val.parse::() { + Ok(n) if n.is_positive() => Ok(()), + Ok(_) => Err(String::from("must be postive value (so far)")), + Err(e) => Err(e.to_string()), + } + } } -- cgit v1.2.3