aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs79
1 files changed, 12 insertions, 67 deletions
diff --git a/src/main.rs b/src/main.rs
index c3fa324..46f8042 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,76 +1,21 @@
-fn client(key: &str) -> ynab_api::apis::client::APIClient {
- let mut ynab_config = ynab_api::apis::configuration::Configuration::new();
- ynab_config.api_key = Some(ynab_api::apis::configuration::ApiKey {
- prefix: Some("Bearer".to_string()),
- key: key.to_string(),
- });
- ynab_api::apis::client::APIClient::new(ynab_config)
-}
-
-fn format_amount(amount: i64) -> String {
- let dollars = amount.abs() / 1000;
- let cents = (amount.abs() % 1000) / 10;
- let sign = if amount < 0 { "-" } else { "" };
- format!("{}{}.{:02}", sign, dollars, cents)
-}
+mod ynab;
fn main() {
let key = std::env::args().nth(1).unwrap();
- let client = client(&key);
-
- let budgets = client.budgets_api().get_budgets().unwrap().data.budgets;
- let budget = budgets.iter().next().unwrap();
- println!("budget is {} ({})", budget.name, budget.id);
-
- let reimbursables_id = client
- .categories_api()
- .get_categories(&budget.id, 0)
- .unwrap()
- .data
- .category_groups
- .iter()
- .map(|group| {
- group
- .categories
- .iter()
- .map(|c| (c.id.clone(), c.name.clone()))
- })
- .flat_map(|cs| cs)
- .find(|(_, name)| name == "Reimbursables")
- .map(|(id, _)| id)
- .unwrap();
- println!("found reimbursables category: {}", reimbursables_id);
-
- let full_budget = client
- .budgets_api()
- .get_budget_by_id(&budget.id, 0)
- .unwrap()
- .data
- .budget;
- println!("got full budget");
-
- let transactions = full_budget.transactions.unwrap();
- let payees = full_budget.payees.unwrap();
-
- for t in transactions {
- if let Some(category_id) = t.category_id {
- if category_id != reimbursables_id {
- continue;
- }
- } else {
- continue;
- }
+ let client = ynab::Client::new(&key);
+ let budget = client.default_budget();
+ println!("using budget {} ({})", budget.name(), budget.id());
- if t.flag_color.is_some() {
+ for t in budget.reimbursables() {
+ if t.reimbursed {
continue;
}
- let payee = t
- .payee_id
- .map(|id| {
- payees.iter().find(|p| p.id == id).unwrap().name.clone()
- })
- .unwrap_or_else(|| "(none)".to_string());
- println!("{} | {} | {}", t.date, payee, format_amount(t.amount))
+ println!(
+ "{} | {} | {}",
+ t.date,
+ t.payee,
+ ynab::format_amount(t.amount)
+ )
}
}