diff --git a/src/data.rs b/src/data.rs index 61c73d8..7b97759 100755 --- a/src/data.rs +++ b/src/data.rs @@ -30,6 +30,7 @@ fn get_dataset(key: &str) -> Result> { "gemstone" => corpora::DATA_GEMSTONE, "mood" => corpora::DATA_MOOD, "tlds" => tlds::DATA_TLDS, + "tvshow" => corpora::DATA_TVSHOWS, _ => "", }; @@ -52,6 +53,7 @@ fn get_dataset(key: &str) -> Result> { /// - `mood` /// - `fabric` /// - `tlds` +/// - `tvshow` /// /// Each of these will return a random word from the list. /// @@ -65,7 +67,13 @@ fn get_dataset(key: &str) -> Result> { /// ``` pub fn gen_switch(name: String) -> String { let n: &str = name.as_str(); - let data = get_dataset(n).unwrap().data; + let data = match get_dataset(n) { + Ok(val) => val.data, + Err(err) => { + eprintln!("Failed getting dataset for {}. {}", name, err); + return "Error: dataset not found".into() + } + }; let mut rnd = rand::thread_rng(); let mut index: usize = 0; @@ -85,6 +93,6 @@ pub fn gen_corpora_switch(name: String) -> String { pub fn gen_prime() -> usize { let mut rnd = rand::thread_rng(); let index = rnd.gen_range(0..primes::DATA_PRIMES.len() - 1); - + primes::DATA_PRIMES[index] } \ No newline at end of file diff --git a/src/data/corpora.rs b/src/data/corpora.rs index 5008525..74f6ba5 100644 --- a/src/data/corpora.rs +++ b/src/data/corpora.rs @@ -3907,8 +3907,7 @@ pub const DATA_MOOD: &str = r#" pub const DATA_TVSHOWS: &str = r#" { - "data": - [ + "data": [ "20/20", "21 Jump Street", "24", diff --git a/src/lib.rs b/src/lib.rs index 12e6a40..2494526 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -347,7 +347,6 @@ mod tests { assert!(rand_ip_192.starts_with("192")); } - #[test] fn test_gen_prime() { // very much not validating primes here, @@ -356,4 +355,19 @@ mod tests { assert!(prime > 1); assert!(prime <= 8017); } + + #[test] + fn test_gen_tvshows() { + // just validating we get something back here. + let show = data::gen_switch("tvshow".into()); + assert_ne!(show, ""); + assert_ne!(show, "Error: dataset not found"); + } + + #[test] + fn test_gen_not_available() { + let show = data::gen_switch("does-not-exist".into()); + assert_eq!(show, "Error: dataset not found"); + } + }