Skip to content

Commit

Permalink
Implement {akc friend ls}
Browse files Browse the repository at this point in the history
- print list of friends in the most basic way, one name per line
- break long lines of code into smaller ones
  • Loading branch information
mkermani144 committed Apr 30, 2022
1 parent d486020 commit 5204698
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ pub fn add_chi(name: String) {
})
}

pub fn list_friends() {
let config = read_config();
println!("{}", utils::list_friends(&config));
}

pub fn suggest() {
let config = read_config();
let filtered_config = utils::filter_config_by_enough_chance(&config);
Expand Down
59 changes: 55 additions & 4 deletions src/config/utils.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
use super::{AkcConfig, FriendInfo, default_chance, default_reduction};

pub fn is_name_duplicate(config: &AkcConfig, name: &str) -> bool {
config.friends.iter().any(| friend_info | friend_info.name == name)
config
.friends
.iter()
.any(| friend_info | friend_info.name == name)
}

pub fn filter_config_by_enough_chance(config: &AkcConfig) -> Vec<&FriendInfo> {
config.friends.iter().filter(| friend_info | friend_info.chance >= default_reduction::TEXT).collect()
config
.friends
.iter()
.filter(| friend_info | friend_info.chance >= default_reduction::TEXT)
.collect()
}

pub fn get_unknown_names<'a>(config: &AkcConfig, names: &'a[String]) -> Vec<&'a String> {
let all_names = config.friends.iter().map(| friend_info | friend_info.name.clone()).collect::<Vec<String>>();
names.iter().filter(| name | !all_names.iter().any(| inner_name | inner_name == *name )).collect::<Vec<&String>>()
let all_names = config
.friends
.iter()
.map(| friend_info | friend_info.name.clone())
.collect::<Vec<String>>();
names
.iter()
.filter(| name | !all_names.iter().any(| inner_name | inner_name == *name ))
.collect::<Vec<&String>>()
}

pub fn get_config_total_chance(config: &AkcConfig, excluded_names: &[String]) -> f64 {
Expand Down Expand Up @@ -42,6 +56,17 @@ pub fn decrease_chances_by_reduction(config: &mut AkcConfig, reduction: f64, nam
})
}

pub fn list_friends(config: &AkcConfig) -> String {
let mut friends_vector = config
.friends
.iter()
.map(| friend_info | friend_info.name.to_owned())
.collect::<Vec<String>>();
friends_vector.sort();

friends_vector.join("\n")
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -204,4 +229,30 @@ mod test {
assert_eq!(config.friends[1].chance, default_chance::KI - 1.0);
assert_eq!(config.friends[2].chance, default_chance::CHI);
}

#[test]
fn test_list_friends() {
let config = AkcConfig {
friends: vec![
FriendInfo {
name: "John".to_owned(),
chance: default_chance::AJI,
level: "aji".to_owned()
},
FriendInfo {
name: "Doe".to_owned(),
chance: default_chance::KI,
level: "ki".to_owned()
},
FriendInfo {
name: "Doe2".to_owned(),
chance: default_chance::CHI,
level: "chi".to_owned()
},
]
};

let friends = list_friends(&config);
assert_eq!(friends, "Doe\nDoe2\nJohn".to_owned());
}
}
4 changes: 3 additions & 1 deletion src/friend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ pub struct FriendCommandBase {
}

#[derive(Subcommand)]
#[clap(about = "Add a friend of specific type")]
#[clap(about = "Add or list friends")]
pub enum FriendCommand {
Aji(FriendCommandBase),
Ki(FriendCommandBase),
Chi(FriendCommandBase),
Ls,
}

#[derive(Parser)]
Expand All @@ -26,5 +27,6 @@ pub fn handle(args: Friend) {
FriendCommand::Aji(name_wrapper) => config::add_aji(name_wrapper.name),
FriendCommand::Ki(name_wrapper) => config::add_ki(name_wrapper.name),
FriendCommand::Chi(name_wrapper) => config::add_chi(name_wrapper.name),
FriendCommand::Ls => config::list_friends(),
}
}

0 comments on commit 5204698

Please sign in to comment.