From 01e7b1d08582e49233ea2382af3a3c0866890cd6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 7 Nov 2019 10:41:18 -0500 Subject: update to newer ynab-api --- Cargo.lock | 6 +- Cargo.toml | 2 +- src/main.rs | 283 ++++++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 184 insertions(+), 107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2244201..e9ea29a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1402,7 +1402,7 @@ dependencies = [ [[package]] name = "ynab-api" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "reqwest 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1417,7 +1417,7 @@ name = "ynab-export" version = "0.0.3" dependencies = [ "directories 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "ynab-api 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ynab-api 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] @@ -1582,4 +1582,4 @@ dependencies = [ "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" -"checksum ynab-api 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2249731e972372209fd0d41e1db5da32312164d01e710159411138c753c2ba8" +"checksum ynab-api 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97b5ea12cb2bac72acfde41fad9e3d9c30e04415e87b95e6221375b6c76fc944" diff --git a/Cargo.toml b/Cargo.toml index af54107..63168c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ license = "MIT" [dependencies] directories = "2" -ynab-api = "2" +ynab-api = "3" diff --git a/src/main.rs b/src/main.rs index 5b8449e..b3ad9c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,8 @@ pub fn read_api_key() -> String { #[allow(clippy::cognitive_complexity)] fn main() { - if std::env::args().nth(1) == Some("schema".to_string()) { + if std::env::args().nth(1).as_ref().map(|s| s.as_str()) == Some("schema") + { print!("{}", SCHEMA); std::process::exit(0); } @@ -51,7 +52,7 @@ fn main() { .clone(); let budget = api .budgets_api() - .get_budget_by_id(&budget_id, 0) + .get_budget_by_id(&budget_id, None) .unwrap() .data .budget; @@ -133,8 +134,9 @@ fn main() { name.trim(), payee .transfer_account_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), ] .join("\t") .as_bytes(), @@ -149,49 +151,49 @@ fn main() { if transaction.deleted { continue; } - file.write_all( - [ - transaction.id.as_ref(), - transaction.date.as_ref(), - format!("{}", transaction.amount).as_ref(), - transaction - .memo - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - transaction.cleared.as_ref(), - if transaction.approved { "1" } else { "0" }, - transaction - .flag_color - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - transaction.account_id.as_ref(), - transaction - .payee_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - transaction - .category_id - .and_then(|id| { - // the split category doesn't appear to be in the - // categories data, so we have to exclude it or else - // the NOT NULL constraint will fail - if id == SPLIT_CATEGORY_ID { - None - } else { - Some(id) - } - }) - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - transaction - .transfer_account_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - ] - .join("\t") - .as_bytes(), - ) - .unwrap(); + let parts: &[&str] = &[ + transaction.id.as_ref(), + transaction.date.as_ref(), + &format!("{}", transaction.amount), + transaction + .memo + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + cleared_to_str(transaction.cleared), + if transaction.approved { "1" } else { "0" }, + transaction + .flag_color + .map(t_flag_color_to_str) + .unwrap_or("\\N"), + transaction.account_id.as_ref(), + transaction + .payee_id + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + transaction + .category_id + .as_ref() + .map(|s| s.as_str()) + .and_then(|id| { + // the split category doesn't appear to be in the + // categories data, so we have to exclude it or else + // the NOT NULL constraint will fail + if id == SPLIT_CATEGORY_ID { + None + } else { + Some(id) + } + }) + .unwrap_or("\\N"), + transaction + .transfer_account_id + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + ]; + file.write_all(parts.join("\t").as_bytes()).unwrap(); file.write_all(b"\n").unwrap(); } file.sync_all().unwrap(); @@ -208,20 +210,24 @@ fn main() { format!("{}", subtransaction.amount).as_ref(), subtransaction .memo - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), subtransaction .payee_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), subtransaction .category_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), subtransaction .transfer_account_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), ] .join("\t") .as_bytes(), @@ -273,47 +279,50 @@ fn main() { if scheduled_transaction.deleted { continue; } - file.write_all( - [ - scheduled_transaction.id.as_ref(), - scheduled_transaction.date_next.as_ref(), - scheduled_transaction.frequency.as_ref(), - format!("{}", scheduled_transaction.amount).as_ref(), - scheduled_transaction - .memo - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + let parts: &[&str] = &[ + scheduled_transaction.id.as_ref(), + scheduled_transaction.date_next.as_ref(), + frequency_to_str(scheduled_transaction.frequency), + &format!("{}", scheduled_transaction.amount), + scheduled_transaction + .memo + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + scheduled_transaction + .flag_color + .map(st_flag_color_to_str) + .unwrap_or("\\N"), + scheduled_transaction.account_id.as_ref(), + scheduled_transaction + .payee_id + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + // the split category doesn't appear to be in the categories + // data, so we have to exclude it or else the NOT NULL + // constraint will fail + if scheduled_transaction + .category_id + .as_ref() + .map(|s| s.as_str()) + == Some(SPLIT_CATEGORY_ID) + { + "\\N" + } else { scheduled_transaction - .flag_color - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - scheduled_transaction.account_id.as_ref(), - scheduled_transaction - .payee_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - // the split category doesn't appear to be in the categories - // data, so we have to exclude it or else the NOT NULL - // constraint will fail - if scheduled_transaction.category_id - == Some(SPLIT_CATEGORY_ID.to_string()) - { - "\\N".to_string() - } else { - scheduled_transaction - .category_id - .unwrap_or_else(|| "\\N".to_string()) - } - .as_ref(), - scheduled_transaction - .transfer_account_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), - ] - .join("\t") - .as_bytes(), - ) - .unwrap(); + .category_id + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N") + }, + scheduled_transaction + .transfer_account_id + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), + ]; + file.write_all(parts.join("\t").as_bytes()).unwrap(); file.write_all(b"\n").unwrap(); } file.sync_all().unwrap(); @@ -332,20 +341,24 @@ fn main() { format!("{}", scheduled_subtransaction.amount).as_ref(), scheduled_subtransaction .memo - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), scheduled_subtransaction .payee_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), scheduled_subtransaction .category_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), scheduled_subtransaction .transfer_account_id - .unwrap_or_else(|| "\\N".to_string()) - .as_ref(), + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("\\N"), ] .join("\t") .as_bytes(), @@ -355,3 +368,67 @@ fn main() { } file.sync_all().unwrap(); } + +fn cleared_to_str( + cleared: ynab_api::models::transaction_summary::Cleared, +) -> &'static str { + use ynab_api::models::transaction_summary::Cleared::*; + + match cleared { + Cleared => "cleared", + Uncleared => "uncleared", + Reconciled => "reconciled", + } +} + +fn t_flag_color_to_str( + flag_color: ynab_api::models::transaction_summary::FlagColor, +) -> &'static str { + use ynab_api::models::transaction_summary::FlagColor::*; + + match flag_color { + Red => "red", + Orange => "orange", + Yellow => "yellow", + Green => "green", + Blue => "blue", + Purple => "purple", + } +} + +fn st_flag_color_to_str( + flag_color: ynab_api::models::scheduled_transaction_summary::FlagColor, +) -> &'static str { + use ynab_api::models::scheduled_transaction_summary::FlagColor::*; + + match flag_color { + Red => "red", + Orange => "orange", + Yellow => "yellow", + Green => "green", + Blue => "blue", + Purple => "purple", + } +} + +fn frequency_to_str( + frequency: ynab_api::models::scheduled_transaction_summary::Frequency, +) -> &'static str { + use ynab_api::models::scheduled_transaction_summary::Frequency::*; + + match frequency { + Never => "never", + Daily => "daily", + Weekly => "weekly", + EveryOtherWeek => "everyOtherWeek", + TwiceAMonth => "twiceAMonth", + Every4Weeks => "every4Weeks", + Monthly => "monthly", + EveryOtherMonth => "everyOtherMonth", + Every3Months => "every3Months", + Every4Months => "every4Months", + TwiceAYear => "twiceAYear", + Yearly => "yearly", + EveryOtherYear => "everyOtherYear", + } +} -- cgit v1.2.3