From 07a66bb0067503f93d880ab7318adda6823a0082 Mon Sep 17 00:00:00 2001 From: 35V LG84 <35vlg84-x4e6b92@e257.fi> Date: Sat, 16 Nov 2024 13:55:19 +0200 Subject: [PATCH] feat-support-none-path: for Accounts, etc. Add support that Accounts, Commodities and Tags path can be "none". Signed-off-by: 35V LG84 <35vlg84-x4e6b92@e257.fi> --- conf/tackler.toml | 7 +++ tackler-core/src/config/items.rs | 81 ++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/conf/tackler.toml b/conf/tackler.toml index 2a2d332..d541f95 100644 --- a/conf/tackler.toml +++ b/conf/tackler.toml @@ -153,18 +153,25 @@ suffix = "txn" ### Path to accounts data ### ### If the path is relative, then it's based on this file. +### +### Set the value to string "none", to disable the Chart of Accounts. path = "accounts.toml" [transaction.commodities] ### Path to commodities data ### ### If the path is relative, then it's based on this file. +### +### Set the value to string "none", to disable the Chart of Commodities, +### in that case, empty commodities are allowed by default. path = "commodities.toml" [transaction.tags] ### Path to tags data ### ### If the path is relative, then it's based on this file. +### +### Set the value to string "none", to disable the Chart of Tags path = "tags.toml" ############################################################################ diff --git a/tackler-core/src/config/items.rs b/tackler-core/src/config/items.rs index 2ec47b0..fe29837 100644 --- a/tackler-core/src/config/items.rs +++ b/tackler-core/src/config/items.rs @@ -33,6 +33,9 @@ use tackler_rs::get_abs_path; use time::{format_description, Time, UtcOffset}; use time_tz::{timezones, Tz}; +/// UI/CFG key value for none +const NONE_VALUE: &str = "none"; + #[derive(Debug, Clone, Default)] pub enum StorageType { #[default] @@ -307,14 +310,25 @@ pub struct Accounts { impl Accounts { fn from>( path: P, - acc_path_raw: &AccountsPathRaw, + accs_path_raw: &AccountsPathRaw, ) -> Result> { - let accs_path = get_abs_path(&path, acc_path_raw.path.as_str())?; - let acc_raw: AccountsRaw = toml::from_str(fs::read_to_string(accs_path)?.as_str())?; - - Ok(Accounts { - names: acc_raw.names, - }) + let accs_path_str = accs_path_raw.path.as_str(); + match accs_path_str { + NONE_VALUE => Ok(Accounts::default()), + _ => { + let accs_path = get_abs_path(&path, accs_path_str)?; + let acc_raw: AccountsRaw = match fs::read_to_string(&accs_path) { + Ok(s) => toml::from_str(s.as_str())?, + Err(err) => { + let msg = format!("Accounts configuration error while reading file '{accs_path_str}': {err}"); + return Err(msg.into()); + } + }; + Ok(Accounts { + names: acc_raw.names, + }) + } + } } } @@ -329,13 +343,27 @@ impl Commodities { path: P, comm_path_raw: &CommoditiesPathRaw, ) -> Result> { - let comms_path = get_abs_path(&path, comm_path_raw.path.as_str())?; - let comm_raw: CommoditiesRaw = toml::from_str(fs::read_to_string(comms_path)?.as_str())?; - - Ok(Commodities { - permit_empty_commodity: comm_raw.permit_empty_commodity, - names: comm_raw.names, - }) + let comm_path_str = comm_path_raw.path.as_str(); + match comm_path_str { + NONE_VALUE => Ok(Commodities { + permit_empty_commodity: Some(true), + names: Vec::new(), + }), + _ => { + let comm_path = get_abs_path(&path, comm_path_str)?; + let comm_raw: CommoditiesRaw = match fs::read_to_string(&comm_path) { + Ok(s) => toml::from_str(s.as_str())?, + Err(err) => { + let msg = format!("Commodities configuration error while reading file '{comm_path_str}': {err}"); + return Err(msg.into()); + } + }; + Ok(Commodities { + permit_empty_commodity: comm_raw.permit_empty_commodity, + names: comm_raw.names, + }) + } + } } } @@ -347,12 +375,25 @@ pub struct Tags { impl Tags { fn from>(path: P, tags_path_raw: &TagsPathRaw) -> Result> { - let tags_path = get_abs_path(&path, tags_path_raw.path.as_str())?; - let tags_raw: TagsRaw = toml::from_str(fs::read_to_string(tags_path)?.as_str())?; - - Ok(Tags { - names: tags_raw.names, - }) + let tags_path_str = tags_path_raw.path.as_str(); + match tags_path_str { + NONE_VALUE => Ok(Tags::default()), + _ => { + let tags_path = get_abs_path(&path, tags_path_str)?; + let tags_raw: TagsRaw = match fs::read_to_string(&tags_path) { + Ok(s) => toml::from_str(s.as_str())?, + Err(err) => { + let msg = format!( + "Tags configuration error while reading file '{tags_path_str}': {err}" + ); + return Err(msg.into()); + } + }; + Ok(Tags { + names: tags_raw.names, + }) + } + } } }