From 520469850f80f6a70ba0c5c56d0093a4510cfa46 Mon Sep 17 00:00:00 2001 From: mohammad kermani Date: Sat, 30 Apr 2022 09:01:00 +0430 Subject: [PATCH] Implement {akc friend ls} - print list of friends in the most basic way, one name per line - break long lines of code into smaller ones --- src/config/mod.rs | 5 ++++ src/config/utils.rs | 59 ++++++++++++++++++++++++++++++++++++++++++--- src/friend.rs | 4 ++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 6fe39a0..691e8ea 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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); diff --git a/src/config/utils.rs b/src/config/utils.rs index d92e25d..2e05d54 100644 --- a/src/config/utils.rs +++ b/src/config/utils.rs @@ -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::>(); - names.iter().filter(| name | !all_names.iter().any(| inner_name | inner_name == *name )).collect::>() + let all_names = config + .friends + .iter() + .map(| friend_info | friend_info.name.clone()) + .collect::>(); + names + .iter() + .filter(| name | !all_names.iter().any(| inner_name | inner_name == *name )) + .collect::>() } pub fn get_config_total_chance(config: &AkcConfig, excluded_names: &[String]) -> f64 { @@ -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::>(); + friends_vector.sort(); + + friends_vector.join("\n") +} + #[cfg(test)] mod test { use super::*; @@ -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()); + } } \ No newline at end of file diff --git a/src/friend.rs b/src/friend.rs index 2ce46fc..d8d22c5 100644 --- a/src/friend.rs +++ b/src/friend.rs @@ -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)] @@ -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(), } } \ No newline at end of file