summaryrefslogtreecommitdiffstats
path: root/CLI/rust
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-02 00:37:13 +0200
committera14m <[email protected]>2021-04-02 00:37:13 +0200
commit7bca115698e720378d0359a416d54a360a7bce39 (patch)
treeea34752dc6a0a67ee88f82d231613c9ead23cbdb /CLI/rust
parent3f4cc21ab80f633ae6fc63e10a6216114d235a38 (diff)
Move validators module to it's own file
Diffstat (limited to 'CLI/rust')
-rw-r--r--CLI/rust/src/game_of_life/opts.rs122
-rw-r--r--CLI/rust/src/game_of_life/opts/validators.rs118
2 files changed, 119 insertions, 121 deletions
diff --git a/CLI/rust/src/game_of_life/opts.rs b/CLI/rust/src/game_of_life/opts.rs
index 9f612c1..a5da4de 100644
--- a/CLI/rust/src/game_of_life/opts.rs
+++ b/CLI/rust/src/game_of_life/opts.rs
@@ -1,121 +1 @@
-/// Clap validators module
-///
-/// Used for validating different CLI args (like width, height and delay)
-/// Ref: https://docs.rs/clap/3.0.0-beta.2/clap/struct.Arg.html#method.validator
-pub(crate) mod validators {
- use terminal_size::{terminal_size, Height, Width};
-
- /// Validator to check if width configuration can be used
- ///
- /// The maximum width allowed is the current terminal window width
- pub(crate) fn is_fitting_term_width(val: &str) -> Result<(), String> {
- let (Width(term_width), Height(_)) = terminal_size().unwrap();
- match val.parse::<u16>() {
- Ok(n) if n > 0 && n <= term_width => Ok(()),
- Ok(_) => Err(format!(
- "must be between 1 and {} (current terminal width)",
- term_width
- )),
- Err(e) => Err(e.to_string()),
- }
- }
-
- #[cfg(test)]
- mod is_fitting_term_width {
- use super::*;
-
- #[test]
- fn positive_value() {
- assert!(is_fitting_term_width("13").is_ok());
- }
-
-
- #[test]
- fn value_too_large() {
- assert!(is_fitting_term_width("1337").is_err());
- }
-
- #[test]
- fn negative_value() {
- assert!(is_fitting_term_width("-13").is_err());
- }
-
- #[test]
- fn string_value() {
- assert!(is_fitting_term_width("not a number").is_err());
- }
- }
-
- /// Validator to check if height configuration can be used
- ///
- /// The maximum height allowed is the current terminal window height - 2
- /// The last 2 lines are used for status reporting (seed/input) and space
- /// for ease of use
- pub(crate) fn is_fitting_term_height(val: &str) -> Result<(), String> {
- let (Width(_), Height(term_height)) = terminal_size().unwrap();
- let term_height = term_height - 2;
- match val.parse::<u16>() {
- Ok(n) if n > 0 && n <= term_height => Ok(()),
- Ok(_) => Err(format!(
- "must be between 1 and {} (current terminal height)",
- term_height
- )),
- Err(e) => Err(e.to_string()),
- }
- }
-
- #[cfg(test)]
- mod is_fitting_term_height {
- use super::*;
-
- #[test]
- fn positive_value() {
- assert!(is_fitting_term_height("13").is_ok());
- }
-
-
- #[test]
- fn value_too_large() {
- assert!(is_fitting_term_height("1337").is_err());
- }
-
- #[test]
- fn negative_value() {
- assert!(is_fitting_term_height("-13").is_err());
- }
-
- #[test]
- fn string_value() {
- assert!(is_fitting_term_height("not a number").is_err());
- }
- }
-
- /// Validator to check if delay value is positive
- pub(crate) fn is_positive(val: &str) -> Result<(), String> {
- match val.parse::<i16>() {
- Ok(n) if n.is_positive() => Ok(()),
- Ok(_) => Err(String::from("must be postive value (so far)")),
- Err(e) => Err(e.to_string()),
- }
- }
-
- #[cfg(test)]
- mod is_positive {
- use super::*;
-
- #[test]
- fn positive_value() {
- assert!(is_positive("1337").is_ok());
- }
-
- #[test]
- fn negative_value() {
- assert!(is_positive("-1337").is_err());
- }
-
- #[test]
- fn string_value() {
- assert!(is_positive("not a number").is_err());
- }
- }
-}
+pub(crate) mod validators;
diff --git a/CLI/rust/src/game_of_life/opts/validators.rs b/CLI/rust/src/game_of_life/opts/validators.rs
new file mode 100644
index 0000000..0de7b3f
--- /dev/null
+++ b/CLI/rust/src/game_of_life/opts/validators.rs
@@ -0,0 +1,118 @@
+//! Clap validators module
+//!
+//! Used for validating different CLI args (like width, height and delay)
+//! Ref: https://docs.rs/clap/3.0.0-beta.2/clap/struct.Arg.html#method.validator
+
+use terminal_size::{terminal_size, Height, Width};
+
+/// Validator to check if width configuration can be used
+///
+/// The maximum width allowed is the current terminal window width
+pub(crate) fn is_fitting_term_width(val: &str) -> Result<(), String> {
+ let (Width(term_width), Height(_)) = terminal_size().unwrap();
+ match val.parse::<u16>() {
+ Ok(n) if n > 0 && n <= term_width => Ok(()),
+ Ok(_) => Err(format!(
+ "must be between 1 and {} (current terminal width)",
+ term_width
+ )),
+ Err(e) => Err(e.to_string()),
+ }
+}
+
+#[cfg(test)]
+mod is_fitting_term_width {
+ use super::*;
+
+ #[test]
+ fn positive_value() {
+ assert!(is_fitting_term_width("13").is_ok());
+ }
+
+ #[test]
+ fn value_too_large() {
+ assert!(is_fitting_term_width("1337").is_err());
+ }
+
+ #[test]
+ fn negative_value() {
+ assert!(is_fitting_term_width("-13").is_err());
+ }
+
+ #[test]
+ fn string_value() {
+ assert!(is_fitting_term_width("not a number").is_err());
+ }
+}
+
+/// Validator to check if height configuration can be used
+///
+/// The maximum height allowed is the current terminal window height - 2
+/// The last 2 lines are used for status reporting (seed/input) and space
+/// for ease of use
+pub(crate) fn is_fitting_term_height(val: &str) -> Result<(), String> {
+ let (Width(_), Height(term_height)) = terminal_size().unwrap();
+ let term_height = term_height - 2;
+ match val.parse::<u16>() {
+ Ok(n) if n > 0 && n <= term_height => Ok(()),
+ Ok(_) => Err(format!(
+ "must be between 1 and {} (current terminal height)",
+ term_height
+ )),
+ Err(e) => Err(e.to_string()),
+ }
+}
+
+#[cfg(test)]
+mod is_fitting_term_height {
+ use super::*;
+
+ #[test]
+ fn positive_value() {
+ assert!(is_fitting_term_height("13").is_ok());
+ }
+
+ #[test]
+ fn value_too_large() {
+ assert!(is_fitting_term_height("1337").is_err());
+ }
+
+ #[test]
+ fn negative_value() {
+ assert!(is_fitting_term_height("-13").is_err());
+ }
+
+ #[test]
+ fn string_value() {
+ assert!(is_fitting_term_height("not a number").is_err());
+ }
+}
+
+/// Validator to check if delay value is positive
+pub(crate) fn is_positive(val: &str) -> Result<(), String> {
+ match val.parse::<i16>() {
+ Ok(n) if n.is_positive() => Ok(()),
+ Ok(_) => Err(String::from("must be postive value (so far)")),
+ Err(e) => Err(e.to_string()),
+ }
+}
+
+#[cfg(test)]
+mod is_positive {
+ use super::*;
+
+ #[test]
+ fn positive_value() {
+ assert!(is_positive("1337").is_ok());
+ }
+
+ #[test]
+ fn negative_value() {
+ assert!(is_positive("-1337").is_err());
+ }
+
+ #[test]
+ fn string_value() {
+ assert!(is_positive("not a number").is_err());
+ }
+}