Skip to content

Commit bd69450

Browse files
committed
feat: pass since and limit to transaction list
1 parent b6fce5d commit bd69450

File tree

6 files changed

+56
-27
lines changed

6 files changed

+56
-27
lines changed

src/accounts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub async fn list(token: &str) -> Result<()> {
7373
for (account_type, account) in supported_accounts {
7474
let created = account.created.format("%Y-%m-%d").to_string();
7575
let balance = client.balance(&account.id).await?;
76-
let balance_value = Amount::try_from(balance.balance)?;
76+
let balance_value = Amount::from(balance.balance);
7777
print_balance_row(
7878
&account_type.value(),
7979
&account.account_number,

src/batch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Display for Row {
2323
}
2424
}
2525

26-
pub async fn run(token: String, file: String) -> Result<()> {
26+
pub async fn run(_token: String, file: String) -> Result<()> {
2727
let path = Path::new(&file);
2828
println!("starting batch run for file: {}", path.display());
2929

src/currency.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use serde::Deserialize;
99
#[derive(Clone, Debug, Deserialize)]
1010
#[serde(try_from = "&str")]
1111
pub struct Amount {
12-
pub pence: u32,
12+
pub pence: i64,
1313
}
1414

15-
impl From<u32> for Amount {
16-
fn from(pence: u32) -> Self {
15+
impl From<i64> for Amount {
16+
fn from(pence: i64) -> Self {
1717
Amount { pence }
1818
}
1919
}
@@ -36,12 +36,11 @@ impl TryFrom<&str> for Amount {
3636
}
3737
}
3838

39-
impl TryFrom<i64> for Amount {
39+
impl TryInto<u32> for Amount {
4040
type Error = TryFromIntError;
4141

42-
fn try_from(value: i64) -> Result<Self, Self::Error> {
43-
let pence = u32::try_from(value)?;
44-
Ok(Amount { pence })
42+
fn try_into(self) -> Result<u32, Self::Error> {
43+
self.pence.try_into()
4544
}
4645
}
4746

@@ -69,13 +68,14 @@ impl Display for Amount {
6968

7069
#[cfg(test)]
7170
mod test {
71+
7272
use super::*;
7373

7474
#[test]
7575
fn format_currency_decimals() {
7676
let pence = 890;
7777

78-
let value = Amount::from(pence).to_string();
78+
let value = Amount { pence }.to_string();
7979

8080
assert_eq!(value, "8.90")
8181
}
@@ -89,7 +89,7 @@ mod test {
8989
];
9090

9191
for (pence, expected) in cases {
92-
let value = Amount::from(pence).to_string();
92+
let value = Amount { pence }.to_string();
9393

9494
assert_eq!(value, expected);
9595
}
@@ -99,7 +99,7 @@ mod test {
9999
fn parse_currency_decimals() {
100100
let pence = "10.05";
101101

102-
let value = Amount::from_str(pence)
102+
let value = Amount::try_from(pence)
103103
.expect("must parse fixed value")
104104
.pence;
105105

@@ -110,19 +110,28 @@ mod test {
110110
fn parse_currency_separators() {
111111
let pence = "1,010.05";
112112

113-
let value = Amount::from_str(pence)
113+
let value = Amount::try_from(pence)
114114
.expect("must parse fixed value")
115115
.pence;
116116

117117
assert_eq!(value, 101005);
118118
}
119119

120120
#[test]
121-
fn try_from_i64() {
121+
fn from_i64() {
122122
let pence: i64 = 953578513525;
123123

124-
let value = Amount::try_from(pence);
124+
let value = Amount::from(pence);
125+
126+
assert_eq!(value.pence, pence);
127+
}
128+
129+
#[test]
130+
fn try_into_u32() {
131+
let pence = 1024;
132+
133+
let res: Result<u32, _> = Amount { pence }.try_into();
125134

126-
assert_eq!(value.is_err(), true);
135+
assert_eq!(res.is_ok(), true);
127136
}
128137
}

src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chrono::{DateTime, Utc};
12
use clap::{Parser, Subcommand};
23
use currency::Amount;
34

@@ -69,6 +70,12 @@ enum PotsCommands {
6970
enum TransactionCommands {
7071
List {
7172
account_type: accounts::AccountType,
73+
74+
#[arg(short, long)]
75+
since: Option<DateTime<Utc>>,
76+
77+
#[arg(short, long, default_value = "10")]
78+
limit: u16,
7279
},
7380
UpdateNote {
7481
transaction_id: String,
@@ -104,8 +111,12 @@ async fn main() -> Result<()> {
104111
}
105112
},
106113
Commands::Transactions { tx_cmd } => match tx_cmd {
107-
TransactionCommands::List { account_type } => {
108-
transactions::list(&args.monzo_access_token, account_type).await?;
114+
TransactionCommands::List {
115+
account_type,
116+
since,
117+
limit,
118+
} => {
119+
transactions::list(&args.monzo_access_token, account_type, since, limit).await?;
109120
}
110121
TransactionCommands::UpdateNote {
111122
transaction_id,

src/pots.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub async fn list(token: &str) -> Result<()> {
2222
let pots = client.pots(&account.id).await?;
2323

2424
for pot in pots.iter().filter(|pot| !pot.deleted) {
25-
let balance_value = Amount::try_from(pot.balance)?;
25+
let balance_value = Amount::from(pot.balance);
2626
print_pot_balance_row(
2727
&account_type.value(),
2828
&account.account_number,
@@ -52,16 +52,18 @@ pub async fn deposit(
5252
}
5353

5454
Some(pot) => {
55-
let balance = Amount::try_from(pot.balance)?;
55+
let balance = Amount::from(pot.balance);
5656
println!("Found pot. Name: {}, Balance: {}", pot.name, balance,);
57+
58+
let amount_i: u32 = amount.pence.try_into()?;
5759
client
58-
.deposit_into_pot(&pot.id, &pot.current_account_id, amount.pence)
60+
.deposit_into_pot(&pot.id, &pot.current_account_id, amount_i)
5961
.await?;
6062
println!("Completed deposit. Name: {}, Amount: {}", pot.name, amount);
6163
}
6264
}
6365

64-
if let Some(description) = description {
66+
if let Some(_description) = description {
6567
// find transaction
6668
// annotate transaction
6769
}

src/transactions.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use crate::accounts;
77
use crate::currency::Amount;
88
use crate::Result;
99

10-
pub async fn list(token: &str, account_type: accounts::AccountType) -> Result<()> {
10+
pub async fn list(
11+
token: &str,
12+
account_type: accounts::AccountType,
13+
since: Option<DateTime<Utc>>,
14+
limit: u16,
15+
) -> Result<()> {
1116
let client = Client::new(token);
1217

1318
let accounts = client.accounts().await?;
@@ -27,16 +32,18 @@ pub async fn list(token: &str, account_type: accounts::AccountType) -> Result<()
2732
print_transaction_row("Created", "Category", "Description", "Amount");
2833
println!("-----------------------------------------------------------------------------------------------------------");
2934

35+
let since = since.unwrap_or(Utc::now() - Duration::days(1));
36+
3037
let transactions = client
3138
.transactions(&acc.id)
32-
.since(Utc::now() - Duration::days(5))
33-
.limit(10)
39+
.since(since)
40+
.limit(limit)
3441
.send()
3542
.await?;
3643

3744
for tx in transactions.iter() {
3845
let created = &tx.created.format("%Y-%m-%d").to_string();
39-
let amount = Amount::try_from(tx.amount)?;
46+
let amount = Amount::from(tx.amount);
4047
print_transaction_row(created, &tx.category, &tx.id, &amount.to_string());
4148
}
4249
}
@@ -57,7 +64,7 @@ pub async fn annotate(token: &str, transaction_id: &str, note: String) -> Result
5764
println!("Note added.");
5865
println!("");
5966
let created = &tx.created.format("%Y-%m-%d").to_string();
60-
let amount = Amount::try_from(tx.amount)?;
67+
let amount = Amount::from(tx.amount);
6168
print_transaction_row("Created", "Category", "Note", "Amount");
6269
println!("-----------------------------------------------------------------------------------------------------------");
6370
print_transaction_row(created, &tx.category, &tx.notes, &amount.to_string());

0 commit comments

Comments
 (0)