Skip to content

Commit

Permalink
fix: implement 'not found' error for datasets which were not found
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGimbel committed Apr 19, 2024
1 parent 3a98db3 commit 2916690
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn get_dataset(key: &str) -> Result<JSONDataset, Box<dyn Error>> {
"gemstone" => corpora::DATA_GEMSTONE,
"mood" => corpora::DATA_MOOD,
"tlds" => tlds::DATA_TLDS,
"tvshow" => corpora::DATA_TVSHOWS,
_ => "",
};

Expand All @@ -52,6 +53,7 @@ fn get_dataset(key: &str) -> Result<JSONDataset, Box<dyn Error>> {
/// - `mood`
/// - `fabric`
/// - `tlds`
/// - `tvshow`
///
/// Each of these will return a random word from the list.
///
Expand All @@ -65,7 +67,13 @@ fn get_dataset(key: &str) -> Result<JSONDataset, Box<dyn Error>> {
/// ```
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;
Expand All @@ -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]
}
3 changes: 1 addition & 2 deletions src/data/corpora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3907,8 +3907,7 @@ pub const DATA_MOOD: &str = r#"

pub const DATA_TVSHOWS: &str = r#"
{
"data":
[
"data": [
"20/20",
"21 Jump Street",
"24",
Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ mod tests {
assert!(rand_ip_192.starts_with("192"));
}


#[test]
fn test_gen_prime() {
// very much not validating primes here,
Expand All @@ -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");
}

}

0 comments on commit 2916690

Please sign in to comment.