Skip to content

Commit

Permalink
removed arrow print and person without group conversion (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwkingjr committed Feb 17, 2024
1 parent 0e7d3e4 commit d883ed3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gift_circle"
version = "0.9.0"
version = "0.10.0"
edition = "2021"
authors = ["Chuck King"]

Expand Down
18 changes: 1 addition & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
4 changes: 2 additions & 2 deletions develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 6 additions & 0 deletions src/gift_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ pub fn get_gift_circle(from_persons: People, use_groups: bool) -> Result<People>
}

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!(
Expand Down
42 changes: 8 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,37 @@ 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();

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::<Vec<_>>();
// 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()?)
}

Expand Down
2 changes: 0 additions & 2 deletions src/myargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
40 changes: 0 additions & 40 deletions src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,3 @@ impl Person {
}
}
}

#[derive(Clone, PartialEq, Eq, Debug, Default, Deserialize, Serialize)]
pub struct PersonWithoutGroup {
pub name: String,
pub email_address: Option<String>,
pub assigned_person_name: Option<String>,
}

impl PersonWithoutGroup {
#[allow(dead_code)]
pub fn new(name: &str) -> Self {
PersonWithoutGroup {
name: name.to_string(),
..Default::default()
}
}
}

impl From<Person> 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);
}
}

0 comments on commit d883ed3

Please sign in to comment.