From d883ed3b0fd0917dd476a0f3fc448c9cb0e8261b Mon Sep 17 00:00:00 2001 From: Chuck King Date: Sat, 17 Feb 2024 17:46:03 -0600 Subject: [PATCH] removed arrow print and person without group conversion (#37) --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 18 +----------------- develop.md | 4 ++-- src/gift_circle.rs | 6 ++++++ src/main.rs | 42 ++++++++---------------------------------- src/myargs.rs | 2 -- src/person.rs | 40 ---------------------------------------- 8 files changed, 19 insertions(+), 97 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d852719..5818bac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,7 +157,7 @@ dependencies = [ [[package]] name = "gift_circle" -version = "0.9.0" +version = "0.10.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 665e10d..28021a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gift_circle" -version = "0.9.0" +version = "0.10.0" edition = "2021" authors = ["Chuck King"] diff --git a/README.md b/README.md index 658f284..0d287e2 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ The input file must be in UTF-8 or UTF-8-compatible encoding (e.g., ASCII is UTF -- name -- name,email_address + - Invoke the program with the input file location, using the short or long option format. See the available options using -h/--help). #### Invoking Without Groups @@ -92,20 +93,3 @@ Billy Jones,billy.jones@example.com,3,Kenya Hill Kenya Hill,kenya.hill@example.com,1,Daisy Jones Daisy Jones,daisy.jones@example.com,3,Jack Brown ``` - -### Arrow Print - -If you want to add a line to the stderr output that shows only the names of folks and who they are assigned to give a gift to, you may use the -a/--arrow-print flag. The -a flag output will look like this: - -```shell -#Jessica Brown -> Bill Jones -> Kenya Hill -> Jack Brown -> Daisy Jones -> Joe Hill -> Billy Jones -> Jane Hill -> Beverly Jones -> Jessica Brown -``` - -The arrow output option works while using groups or without groups. - -#### Invoking With Arrow Print - -```shell -./gift_circle -a -i=path/to/participants.csv -./gift_circle --arrow-print -i=path/to/participants.csv -``` diff --git a/develop.md b/develop.md index 6777b69..90197c6 100644 --- a/develop.md +++ b/develop.md @@ -23,6 +23,6 @@ Once the gift_circle binary is moved into your path (e.g., /usr/bin/gift_circle) ```sh gift_circle -h gift_circle -i=./my-participants.csv -gift_circle -a -i=~./my-participants.csv -gift_circle -a -u -i=~./my-participants.csv +gift_circle -i=~./my-participants.csv +gift_circle -u -i=~./my-participants.csv ``` diff --git a/src/gift_circle.rs b/src/gift_circle.rs index b0f3cd7..7c01638 100644 --- a/src/gift_circle.rs +++ b/src/gift_circle.rs @@ -158,6 +158,12 @@ pub fn get_gift_circle(from_persons: People, use_groups: bool) -> Result } if use_groups { + if from_persons.iter().any(|p| p.group_number.is_none()) { + return Err(anyhow!( + "When using groups each participant must have a group assinged!" + )); + } + let possible_path = has_possible_hamiltonian_path(&from_persons); if !possible_path { return Err(anyhow!( diff --git a/src/main.rs b/src/main.rs index 90d5af2..a7b611e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,12 +7,12 @@ mod person; use std::io; use std::process; -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use gift_circle::get_gift_circle; use myargs::get_args; use people::People; -use person::{Person, PersonWithoutGroup}; +use person::Person; fn run() -> Result<()> { let args = get_args(); @@ -20,50 +20,24 @@ fn run() -> Result<()> { let mut rdr = csv::Reader::from_path(args.input.clone()) .with_context(|| format!("Failed to read input from {}", &args.input))?; - let mut participants: People = vec![]; + let mut people: People = vec![]; for result in rdr.deserialize() { let person: Person = result?; - participants.push(person); + people.push(person); } #[allow(unused_assignments)] let mut gift_circle: People = vec![]; - if args.use_groups { - if participants.iter().any(|p| p.group_number.is_none()) { - return Err(anyhow!( - "When using groups each participant must have a group assinged!" - )); - } - gift_circle = get_gift_circle(participants, true)?; - } else { - gift_circle = get_gift_circle(participants, false)?; - } - - if args.arrow_print { - let mut names = gift_circle - .iter() - .map(|p| p.name.clone()) - .collect::>(); - // Add the first person to the end to wrap the circle - let first_person = &names.first().unwrap().clone(); - names.push(first_person.to_string()); - - println!("#{}", &names.join(" -> ")); - } + gift_circle = get_gift_circle(people, args.use_groups)?; let mut wtr = csv::Writer::from_writer(io::stdout()); - if args.use_groups { - for person in gift_circle { - wtr.serialize(person)?; - } - } else { - for person in gift_circle { - wtr.serialize(PersonWithoutGroup::from(person))?; - } + for person in gift_circle { + wtr.serialize(person)?; } + Ok(wtr.flush()?) } diff --git a/src/myargs.rs b/src/myargs.rs index 9d369a7..43e52f9 100644 --- a/src/myargs.rs +++ b/src/myargs.rs @@ -8,8 +8,6 @@ pub struct Args { #[arg(long, short)] pub input: String, #[clap(long, short, action)] - pub arrow_print: bool, - #[clap(long, short, action)] pub use_groups: bool, } diff --git a/src/person.rs b/src/person.rs index 34fc5be..7c8c701 100644 --- a/src/person.rs +++ b/src/person.rs @@ -26,43 +26,3 @@ impl Person { } } } - -#[derive(Clone, PartialEq, Eq, Debug, Default, Deserialize, Serialize)] -pub struct PersonWithoutGroup { - pub name: String, - pub email_address: Option, - pub assigned_person_name: Option, -} - -impl PersonWithoutGroup { - #[allow(dead_code)] - pub fn new(name: &str) -> Self { - PersonWithoutGroup { - name: name.to_string(), - ..Default::default() - } - } -} - -impl From for PersonWithoutGroup { - fn from(person: Person) -> Self { - PersonWithoutGroup { - name: person.name, - email_address: person.email_address, - assigned_person_name: person.assigned_person_name, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_person_without_group_from_person() { - let person = Person::new("Father", 1); - let person_without_group = PersonWithoutGroup::from(person); - let expected = PersonWithoutGroup::new("Father"); - assert_eq!(person_without_group, expected); - } -}