Skip to main content

tackler_core/kernel/
report_settings.rs

1/*
2 * Tackler-NG 2025
3 * SPDX-License-Identifier: Apache-2.0
4 */
5use crate::config::{BalanceType, Scale};
6use crate::kernel::Settings;
7use crate::kernel::price_lookup::PriceLookup;
8use crate::model::Commodity;
9use crate::tackler;
10use jiff::tz::TimeZone;
11use std::sync::Arc;
12use tackler_api::txn_ts::{GroupBy, TimestampStyle};
13
14#[derive(Debug, Clone)]
15pub struct BalanceSettings {
16    pub(crate) title: String,
17    pub(crate) bal_type: BalanceType,
18    pub(crate) ras: Vec<String>,
19    pub(crate) scale: Scale,
20    pub(crate) inverted: bool,
21    pub(crate) report_commodity: Option<Arc<Commodity>>,
22    pub(crate) price_lookup: PriceLookup,
23}
24
25impl TryFrom<&Settings> for BalanceSettings {
26    type Error = tackler::Error;
27
28    fn try_from(settings: &Settings) -> Result<Self, Self::Error> {
29        Ok(BalanceSettings {
30            title: settings.report.balance.title.clone(),
31            bal_type: settings.report.balance.bal_type.clone(),
32            ras: settings.get_balance_ras(),
33            scale: settings.report.scale.clone(),
34            inverted: settings.inverted,
35            report_commodity: settings.get_report_commodity(),
36            price_lookup: settings.get_price_lookup(),
37        })
38    }
39}
40
41#[derive(Debug, Clone)]
42pub struct BalanceGroupSettings {
43    pub title: String,
44    pub bal_type: BalanceType,
45    pub ras: Vec<String>,
46    pub group_by: GroupBy,
47    pub report_tz: TimeZone,
48    pub scale: Scale,
49    pub inverted: bool,
50    pub report_commodity: Option<Arc<Commodity>>,
51    pub price_lookup: PriceLookup,
52}
53
54impl TryFrom<&Settings> for BalanceGroupSettings {
55    type Error = tackler::Error;
56
57    fn try_from(settings: &Settings) -> Result<Self, Self::Error> {
58        let bgs = BalanceGroupSettings {
59            title: settings.report.balance_group.title.clone(),
60            bal_type: settings.report.balance_group.bal_type.clone(),
61            ras: settings.get_balance_group_ras(),
62            group_by: settings.report.balance_group.group_by,
63            report_tz: settings.report.tz.clone(),
64            scale: settings.report.scale.clone(),
65            inverted: settings.inverted,
66            report_commodity: settings.get_report_commodity(),
67            price_lookup: settings.get_price_lookup(),
68        };
69        Ok(bgs)
70    }
71}
72
73impl From<BalanceGroupSettings> for BalanceSettings {
74    fn from(bgs: BalanceGroupSettings) -> BalanceSettings {
75        BalanceSettings {
76            title: String::default(),
77            bal_type: bgs.bal_type.clone(),
78            ras: bgs.ras.clone(),
79            scale: bgs.scale.clone(),
80            inverted: bgs.inverted,
81            report_commodity: bgs.report_commodity.clone(),
82            price_lookup: bgs.price_lookup.clone(),
83        }
84    }
85}
86
87#[derive(Debug, Clone)]
88pub struct RegisterSettings {
89    pub title: String,
90    pub ras: Vec<String>,
91    pub report_tz: TimeZone,
92    pub report_commodity: Option<Arc<Commodity>>,
93    pub(crate) scale: Scale,
94    pub inverted: bool,
95    pub price_lookup: PriceLookup,
96    pub timestamp_style: TimestampStyle,
97}
98
99impl TryFrom<&Settings> for RegisterSettings {
100    type Error = tackler::Error;
101
102    fn try_from(settings: &Settings) -> Result<RegisterSettings, tackler::Error> {
103        let rs = RegisterSettings {
104            title: settings.report.register.title.clone(),
105            ras: settings.get_register_ras(),
106            report_tz: settings.report.tz.clone(),
107            scale: settings.report.scale.clone(),
108            inverted: settings.inverted,
109            report_commodity: settings.get_report_commodity(),
110            price_lookup: settings.get_price_lookup(),
111            timestamp_style: settings.report.register.timestamp_style,
112        };
113        Ok(rs)
114    }
115}