From 75003245789edd62f138e6fd10b25403ce4fd4b4 Mon Sep 17 00:00:00 2001 From: DewaldV Date: Sun, 14 Apr 2024 18:42:46 +0100 Subject: [PATCH] Remove duplicate account filters --- src/accounts.rs | 19 ++++++++++++++----- src/pots.rs | 21 +++++++-------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/accounts.rs b/src/accounts.rs index 85d96cf..629dd3f 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -45,17 +45,26 @@ fn print_balance_row(account_type: &str, account_no: &str, created: &str, balanc ); } -pub async fn list(token: &str) -> monzo::Result<()> { +pub async fn get_supported_accounts(token: &str) -> monzo::Result> { let client = Client::new(token); let accounts = client.accounts().await?; + let supported_accounts = accounts .iter() - .filter_map(|acc| match Type::try_from(&acc.account_type).ok() { - Some(a) => Some((a, acc)), - None => None, + .filter_map(|acc| match Type::try_from(&acc.account_type) { + Ok(acc_type) => Some((acc_type, acc.clone())), + Err(_) => None, }) - .filter(|a| !a.1.account_number.is_empty()); + .filter(|a| !a.1.account_number.is_empty()) + .collect(); + + Ok(supported_accounts) +} + +pub async fn list(token: &str) -> monzo::Result<()> { + let client = Client::new(token); + let supported_accounts = get_supported_accounts(token).await?; print_balance_row("Account Type", "Account No", "Created", "Balance"); println!("-------------------------------------------------------------"); diff --git a/src/pots.rs b/src/pots.rs index d7f8526..b88b665 100644 --- a/src/pots.rs +++ b/src/pots.rs @@ -13,15 +13,11 @@ fn print_pot_balance_row(account_type: &str, account_no: &str, pot_name: &str, b pub async fn list(token: &str) -> monzo::Result<()> { let client = Client::new(token); - let accounts = client.accounts().await?; - let supported_accounts = accounts.iter().filter(|acc| !acc.account_number.is_empty()); + let supported_accounts = accounts::get_supported_accounts(token).await?; print_pot_balance_row("Account Type", "Account Number", "Pot Name", "Balance"); println!("-------------------------------------------------------------------------------"); - for account in supported_accounts { - let account_type = accounts::Type::try_from(&account.account_type) - .expect("already filtered for account types that converted successfully"); - + for (account_type, account) in supported_accounts { let pots = client.pots(&account.id).await?; for pot in pots.iter().filter(|p| !p.deleted) { @@ -43,8 +39,7 @@ pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result let client = Client::new(token); - let accounts = client.accounts().await?; - let found_pot = find_pot(token, pot_name, &accounts).await?; + let found_pot = find_pot(token, pot_name).await?; match found_pot { Some(pot) => { @@ -66,14 +61,12 @@ pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result Ok(()) } -async fn find_pot( - token: &str, - name: &str, - accounts: &Vec, -) -> monzo::Result> { +async fn find_pot(token: &str, name: &str) -> monzo::Result> { let client = Client::new(token); - for account in accounts.iter().filter(|acc| !acc.account_number.is_empty()) { + let supported_accounts = accounts::get_supported_accounts(token).await?; + + for (_, account) in supported_accounts { let pots = client.pots(&account.id).await?; let found_pot = pots .iter()