From 9a7e136497cf16db792d38a15cc4395d0f08d726 Mon Sep 17 00:00:00 2001 From: DewaldV Date: Mon, 22 Apr 2024 22:26:41 +0100 Subject: [PATCH] feat: basic CSV file read --- Cargo.lock | 9 +++++---- Cargo.toml | 1 + examples/batch/batch.csv | 2 ++ src/accounts.rs | 2 +- src/batch.rs | 19 +++++++++++++++---- src/error.rs | 3 +++ 6 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 examples/batch/batch.csv diff --git a/Cargo.lock b/Cargo.lock index 6a623a2..eef93fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,6 +289,7 @@ dependencies = [ "clap", "csv", "monzo-lib", + "serde", "thiserror", "tokio", ] @@ -958,18 +959,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.197" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.198" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 98126b2..462bc25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ clap = { version = "4.5.3", features = ["derive", "env"] } csv = "1.3.0" # monzo-lib = "0.4.4" monzo-lib = { git = "https://github.com/DewaldV/monzo-lib.git" } +serde = "1.0.198" thiserror = "1.0.58" # monzo-lib = { path = "../monzo-lib" } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } diff --git a/examples/batch/batch.csv b/examples/batch/batch.csv new file mode 100644 index 0000000..4c2749f --- /dev/null +++ b/examples/batch/batch.csv @@ -0,0 +1,2 @@ +account,category,description,amount +Personal,eating_out,test-transaction,500 diff --git a/src/accounts.rs b/src/accounts.rs index 0d92350..972eafe 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -2,7 +2,7 @@ use monzo::Client; use crate::{currency, Result}; -#[derive(PartialEq, clap::ValueEnum, Clone, Copy)] +#[derive(PartialEq, Debug, Clone, Copy, serde::Deserialize, clap::ValueEnum)] pub enum AccountType { Personal, Joint, diff --git a/src/batch.rs b/src/batch.rs index 6d4c154..860fe21 100644 --- a/src/batch.rs +++ b/src/batch.rs @@ -1,12 +1,23 @@ -use std::{fs::File, path::Path}; +use std::path::Path; -use crate::error::Result; +use crate::{accounts, error::Result}; + +#[derive(Debug, serde::Deserialize)] +struct Row { + account: accounts::AccountType, + category: String, + description: String, + amount: u32, +} pub async fn run(_token: String, file: String) -> Result<()> { let path = Path::new(&file); println!("batch file path: {}", path.display()); - let _batch_file = File::open(&path)?; - + let mut reader = csv::Reader::from_path(path)?; + for result in reader.deserialize() { + let row: Row = result?; + dbg!(row); + } Ok(()) } diff --git a/src/error.rs b/src/error.rs index 05442b9..7905565 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,4 +11,7 @@ pub enum Error { #[error("IO error: {0}")] IO(#[from] std::io::Error), + + #[error("CSV error: {0}")] + CSV(#[from] csv::Error), }