Skip to content

Commit

Permalink
feat: add error type and results for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
DewaldV committed Apr 22, 2024
1 parent ef314ca commit 1e9ee52
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
10 changes: 4 additions & 6 deletions src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use monzo::Client;

use crate::currency;
use crate::{currency, Result};

#[derive(PartialEq, clap::ValueEnum, Clone, Copy)]
pub enum AccountType {
Expand All @@ -20,7 +20,7 @@ impl AccountType {
impl TryFrom<&monzo::accounts::Type> for AccountType {
type Error = String;

fn try_from(value: &monzo::accounts::Type) -> Result<Self, Self::Error> {
fn try_from(value: &monzo::accounts::Type) -> std::result::Result<Self, Self::Error> {
match value {
monzo::accounts::Type::UkRetail => Ok(AccountType::Personal),
monzo::accounts::Type::UkRetailJoint => Ok(AccountType::Joint),
Expand All @@ -45,9 +45,7 @@ fn print_balance_row(account_type: &str, account_no: &str, created: &str, balanc
);
}

pub async fn get_supported_accounts(
token: &str,
) -> monzo::Result<Vec<(AccountType, monzo::Account)>> {
pub async fn get_supported_accounts(token: &str) -> Result<Vec<(AccountType, monzo::Account)>> {
let client = Client::new(token);

let accounts = client.accounts().await?;
Expand All @@ -64,7 +62,7 @@ pub async fn get_supported_accounts(
Ok(supported_accounts)
}

pub async fn list(token: &str) -> monzo::Result<()> {
pub async fn list(token: &str) -> Result<()> {
let client = Client::new(token);
let supported_accounts = get_supported_accounts(token).await?;

Expand Down
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Unsupported Account Type")]
UnsupportedAccountType { account_type: monzo::accounts::Type },

// External
#[error("Monzo API error: {0}")]
MonzoAPI(#[from] monzo::Error),

#[error("IO error: {0}")]
IO(#[from] std::io::Error),
}

pub type Result<T> = std::result::Result<T, Error>;
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::{Parser, Subcommand};
use error::Result;

mod accounts;
mod batch;
Expand All @@ -8,6 +7,8 @@ mod error;
mod pots;
mod transactions;

pub use self::error::{Error, Result};

#[derive(Parser)]
#[command(name = "monzo")]
#[command(about = "A CLI for Monzo Finops", long_about = None)]
Expand Down
7 changes: 4 additions & 3 deletions src/pots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use monzo::Client;

use crate::accounts;
use crate::currency;
use crate::Result;

fn print_pot_balance_row(account_type: &str, account_no: &str, pot_name: &str, balance: &str) {
println!(
Expand All @@ -10,7 +11,7 @@ fn print_pot_balance_row(account_type: &str, account_no: &str, pot_name: &str, b
);
}

pub async fn list(token: &str) -> monzo::Result<()> {
pub async fn list(token: &str) -> Result<()> {
let client = Client::new(token);

let supported_accounts = accounts::get_supported_accounts(token).await?;
Expand All @@ -34,7 +35,7 @@ pub async fn list(token: &str) -> monzo::Result<()> {
Ok(())
}

pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result<()> {
pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> Result<()> {
let pence = currency::parse_currency(amount).expect("should be an amount ex: 1,203.05");

let client = Client::new(token);
Expand All @@ -61,7 +62,7 @@ pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result
Ok(())
}

async fn find_pot(token: &str, name: &str) -> monzo::Result<Option<monzo::Pot>> {
async fn find_pot(token: &str, name: &str) -> Result<Option<monzo::Pot>> {
let client = Client::new(token);

let supported_accounts = accounts::get_supported_accounts(token).await?;
Expand Down
5 changes: 3 additions & 2 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use monzo::Client;

use crate::accounts;
use crate::currency;
use crate::Result;

pub async fn list(token: &str, account_type: accounts::AccountType) -> monzo::Result<()> {
pub async fn list(token: &str, account_type: accounts::AccountType) -> Result<()> {
let client = Client::new(token);

let accounts = client.accounts().await?;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub async fn list(token: &str, account_type: accounts::AccountType) -> monzo::Re
Ok(())
}

pub async fn annotate(token: &str, transaction_id: &str, note: String) -> monzo::Result<()> {
pub async fn annotate(token: &str, transaction_id: &str, note: String) -> Result<()> {
let client = Client::new(token);

let metadata = HashMap::from([(String::from("notes"), note)]);
Expand Down

0 comments on commit 1e9ee52

Please sign in to comment.