Skip to content

Commit

Permalink
upgrade deps and switch to anyhow macros (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkingjr committed Mar 14, 2024
1 parent e8254a0 commit 0644f71
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gift_circle"
version = "0.11.1"
version = "0.11.2"
edition = "2021"
authors = ["Chuck King"]

Expand All @@ -11,7 +11,7 @@ csv = "1.3"
counter = "0.5.7"
rand = "0.8.5"
serde = { version = "1", features = ["derive"] }
clap = { version = "4.5.0", features = ["derive"] }
anyhow = "1.0.79"
clap = { version = "4.5.2", features = ["derive"] }
anyhow = "1.0.81"

[dev-dependencies]
25 changes: 10 additions & 15 deletions src/gift_circle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::u16;

use anyhow::{anyhow, Result};
use anyhow::Result;
use rand::seq::SliceRandom;

use super::people::{People, PeopleCycle};
Expand Down Expand Up @@ -76,28 +76,23 @@ fn generate_no_group_path(from_people: &mut People) -> People {
/// and stderr prints the number of attempts taken.
pub fn get_gift_circle(from_people: People, use_groups: bool) -> Result<People> {
if from_people.len() <= 2 {
return Err(anyhow!(
"You must submit at least three people in order to form a gift circle."
));
anyhow::bail!("You must submit at least three people in order to form a gift circle.");
}

let duplicates: Vec<String> = from_people.get_duplicated_names();
if !duplicates.is_empty() {
return Err(anyhow!("Found duplicate names: {:#?}", duplicates));
anyhow::bail!("Found duplicate names: {:#?}", duplicates);
}

if use_groups {
if from_people.has_empty_group() {
return Err(anyhow!(
"When using groups each participant must have a group assigned!"
));
anyhow::bail!("When using groups each participant must have a group assigned!");
}

if !from_people.has_possible_hamiltonian_path() {
return Err(anyhow!(
"Sorry, no possible hamiltonian path with this set of groups."
));
}
anyhow::ensure!(
from_people.has_possible_hamiltonian_path(),
"Sorry, no possible hamiltonian path with this set of groups."
);
}

const MAX_ATTEMPTS: u16 = 500;
Expand Down Expand Up @@ -125,10 +120,10 @@ pub fn get_gift_circle(from_people: People, use_groups: bool) -> Result<People>
}

if attempt_count == MAX_ATTEMPTS {
return Err(anyhow!(
anyhow::bail!(
"Sorry, could not find gift circle in {} attempts",
MAX_ATTEMPTS
));
);
}

gift_path.assign_gift_recipients();
Expand Down

0 comments on commit 0644f71

Please sign in to comment.