Skip to content

Commit

Permalink
Split importers into features
Browse files Browse the repository at this point in the history
The following features are now available:

- cardcomplete
- erste
- flatex
- revolut

Alls importers are active by default.

closes #10

Signed-off-by: Peter Nirschl <[email protected]>
  • Loading branch information
petermax2 committed Nov 3, 2024
1 parent db7a7e4 commit eefe685
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ jobs:
run: cargo clippy --verbose
- name: Run tests
run: cargo test --verbose
- name: Feature tests "Revolut"
run: cargo test --no-default-features --features "revolut" --verbose
- name: Feature tests "Erste"
run: cargo test --no-default-features --features "erste" --verbose
- name: Feature tests "Cardcomplete"
run: cargo test --no-default-features --features "cardcomplete" --verbose
- name: Feature tests "Flatex"
run: cargo test --no-default-features --features "flatex" --verbose
21 changes: 14 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ name = "hledger-import"
version = "0.2.1-prerelease"
edition = "2021"

[features]
cardcomplete = ["fast-xml"]
erste = []
flatex = ["csv", "lopdf"]
revolut = ["csv"]
default = ["cardcomplete", "erste", "flatex", "revolut"]

[dependencies]
bigdecimal = {version = "0.4.4", features = ["serde"]}
chrono = {version = "0.4.38", features = ["serde", "now"]}
bigdecimal = { version = "0.4.4", features = ["serde"] }
chrono = { version = "0.4.38", features = ["serde", "now"] }
clap = { version = "4.5.7", features = ["derive"] }
csv = "1.3.0"
fast-xml = { version = "0.23.1", features = ["serialize"] }
csv = { version = "1.3.0", optional = true }
fast-xml = { version = "0.23.1", features = ["serialize"], optional = true }
homedir = "0.2.1"
lopdf = "0.32.0"
num-format = {version = "0.4.4", features = ["with-num-bigint"]}
lopdf = { version = "0.32.0", optional = true }
num-format = { version = "0.4.4", features = ["with-num-bigint"] }
regex = "1.10.5"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
serde_json = { version = "1.0.117" }
thiserror = "1.0.61"
toml = "0.8.14"
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ or start directly:
cargo run -- --help
```

### Features

The importers are split into separate features. They can be enabled separately.

The following features are available:

- cardcomplete
- erste
- flatex
- revolut

All features are enabled per default.

If you want to have a custom build with a subset of importers, you must disable the default features.
The following examples builds `hledger-import` with only the _Revolut_ importer.

```sh
cargo build --no-default-features --features "revolut"
```

## Plans for the Future

- better documentation
Expand Down
25 changes: 21 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{
error::{ImportError, Result},
importers::{flatex_csv::FlatexCsvConfig, flatex_inv::FlatexPdfConfig, revolut::RevolutConfig},
};
#[cfg(feature = "revolut")]
use crate::importers::revolut::RevolutConfig;
#[cfg(feature = "flatex")]
use crate::importers::{flatex_csv::FlatexCsvConfig, flatex_inv::FlatexPdfConfig};

use crate::error::{ImportError, Result};
use homedir::get_my_home;
use regex::RegexBuilder;
use serde::Deserialize;
Expand All @@ -24,8 +26,11 @@ pub struct ImporterConfig {
pub filter: WordFilter,
/// a fallback account can be set to balance postings that could not be assigned to any other account
pub fallback_account: Option<String>,
#[cfg(feature = "revolut")]
pub revolut: Option<RevolutConfig>,
#[cfg(feature = "flatex")]
pub flatex_csv: Option<FlatexCsvConfig>,
#[cfg(feature = "flatex")]
pub flatex_pdf: Option<FlatexPdfConfig>,
}

Expand Down Expand Up @@ -336,9 +341,12 @@ mod tests {
},
filter: WordFilter::default(),
fallback_account: Some("Equity:Unassigned".to_owned()),
#[cfg(feature = "revolut")]
revolut: None,
categories: vec![],
#[cfg(feature = "flatex")]
flatex_csv: None,
#[cfg(feature = "flatex")]
flatex_pdf: None,
};
let result = toml::from_str::<ImporterConfig>(&config_str).expect("TOML parsing failed");
Expand Down Expand Up @@ -387,8 +395,11 @@ mod tests {
}],
},
fallback_account: None,
#[cfg(feature = "revolut")]
revolut: None,
#[cfg(feature = "flatex")]
flatex_csv: None,
#[cfg(feature = "flatex")]
flatex_pdf: None,
categories: vec![CategoryMapping {
pattern: "cat1".to_owned(),
Expand Down Expand Up @@ -472,8 +483,11 @@ mod tests {
],
filter: WordFilter::default(),
fallback_account: None,
#[cfg(feature = "revolut")]
revolut: None,
#[cfg(feature = "flatex")]
flatex_csv: None,
#[cfg(feature = "flatex")]
flatex_pdf: None,
categories: vec![
CategoryMapping {
Expand Down Expand Up @@ -542,8 +556,11 @@ mod tests {
ibans: vec![],
filter: WordFilter::default(),
fallback_account: None,
#[cfg(feature = "revolut")]
revolut: None,
#[cfg(feature = "flatex")]
flatex_csv: None,
#[cfg(feature = "flatex")]
flatex_pdf: None,
categories: Vec::new(),
};
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum ImportError {
InputFileRead(std::path::PathBuf),
#[error("Failed to parse input file: {0}")]
InputParse(String),
#[cfg(feature = "flatex")]
#[error("Failed to parse input PDF file: {0}")]
PdfInputParse(#[from] lopdf::Error),
#[error("Can not interpret input as a number: {0}")]
Expand Down
5 changes: 5 additions & 0 deletions src/importers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/// hledger importer for the Erste Bank JSON files
#[cfg(feature = "erste")]
pub mod erste;

/// hledger importer for Revolut CSV export files
#[cfg(feature = "revolut")]
pub mod revolut;

/// hledger importer for Cardcomplete XML export files
#[cfg(feature = "cardcomplete")]
pub mod cardcomplete;

/// hledger importer for Flatex CSV export files (of settlement accounts)
#[cfg(feature = "flatex")]
pub mod flatex_csv;

/// hledger importer for Flatex PDF invoices
#[cfg(feature = "flatex")]
pub mod flatex_inv;
2 changes: 2 additions & 0 deletions src/importers/revolut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ TOPUP,Current,2024-05-19 10:02:45,2024-05-22 10:02:45,Payment from John Doe Jr,1
account: "Assets:Revolut".to_owned(),
fee_account: Some("Expenses:Fee".to_owned()),
}),
#[cfg(feature = "flatex")]
flatex_csv: None,
#[cfg(feature = "flatex")]
flatex_pdf: None,
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,40 @@ pub trait HledgerImporter {
#[derive(Debug, Clone, ValueEnum)]
enum Importer {
/// Erste Bank JSON export file
#[cfg(feature = "erste")]
Erste,

/// Revolut CSV export file
#[cfg(feature = "revolut")]
Revolut,

/// Cardcomplete XML export file
#[cfg(feature = "cardcomplete")]
Cardcomplete,

/// Flatex CSV export file (of settlement accounts)
#[cfg(feature = "flatex")]
FlatexCSV,

/// Flatex PDF invoice (of stock exchange transactions)
#[cfg(feature = "flatex")]
FlatexPDF,
}

impl From<Importer> for Box<dyn HledgerImporter> {
fn from(val: Importer) -> Self {
match val {
#[cfg(feature = "erste")]
Importer::Erste => Box::new(importers::erste::HledgerErsteJsonImporter::new()),
#[cfg(feature = "revolut")]
Importer::Revolut => Box::new(importers::revolut::RevolutCsvImporter::new()),
#[cfg(feature = "cardcomplete")]
Importer::Cardcomplete => {
Box::new(importers::cardcomplete::CardcompleteXmlImporter::new())
}
#[cfg(feature = "flatex")]
Importer::FlatexCSV => Box::new(importers::flatex_csv::FlatexCsvImport::new()),
#[cfg(feature = "flatex")]
Importer::FlatexPDF => Box::new(importers::flatex_inv::FlatexPdfInvoiceImporter::new()),
}
}
Expand Down

0 comments on commit eefe685

Please sign in to comment.