taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

config.rs (3877B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use jiff::civil::Date;
     18 use reqwest::Url;
     19 use taler_api::{
     20     Serve,
     21     config::{ApiCfg, DbCfg},
     22 };
     23 use taler_common::{
     24     config::{Config, ValueErr},
     25     map_config,
     26     types::payto::PaytoURI,
     27 };
     28 
     29 use crate::{FullHuPayto, HuIban, magnet_api::oauth::Token};
     30 
     31 pub fn parse_db_cfg(cfg: &Config) -> Result<DbCfg, ValueErr> {
     32     DbCfg::parse(cfg.section("magnet-bankdb-postgres"))
     33 }
     34 
     35 pub fn parse_account_payto(cfg: &Config) -> Result<FullHuPayto, ValueErr> {
     36     let sect = cfg.section("magnet-bank");
     37     let iban: HuIban = sect.parse("iban", "IBAN").require()?;
     38     let name = sect.str("NAME").require()?;
     39 
     40     Ok(FullHuPayto::new(iban, name))
     41 }
     42 
     43 /// taler-magnet-bank httpd config
     44 pub struct ServeCfg {
     45     pub payto: PaytoURI,
     46     pub serve: Serve,
     47     pub wire_gateway: Option<ApiCfg>,
     48     pub revenue: Option<ApiCfg>,
     49 }
     50 
     51 impl ServeCfg {
     52     pub fn parse(cfg: &Config) -> Result<Self, ValueErr> {
     53         let payto = parse_account_payto(cfg)?;
     54 
     55         let sect = cfg.section("magnet-bank-httpd");
     56 
     57         let serve = Serve::parse(sect)?;
     58 
     59         let wire_gateway = ApiCfg::parse(cfg.section("magnet-bank-httpd-wire-gateway-api"))?;
     60         let revenue = ApiCfg::parse(cfg.section("magnet-bank-httpd-revenue-api"))?;
     61 
     62         Ok(Self {
     63             payto: payto.as_payto(),
     64             serve,
     65             wire_gateway,
     66             revenue,
     67         })
     68     }
     69 }
     70 
     71 #[derive(Debug, Clone, Copy)]
     72 pub enum AccountType {
     73     Exchange,
     74     Normal,
     75 }
     76 
     77 /// taler-magnet-bank worker config
     78 pub struct WorkerCfg {
     79     pub payto: FullHuPayto,
     80     pub api_url: Url,
     81     pub consumer: Token,
     82     pub keys_path: String,
     83     pub account_type: AccountType,
     84     pub ignore_tx_before: Option<Date>,
     85     pub ignore_bounces_before: Option<Date>,
     86 }
     87 
     88 impl WorkerCfg {
     89     pub fn parse(cfg: &Config) -> Result<Self, ValueErr> {
     90         let payto = parse_account_payto(cfg)?;
     91         let sect = cfg.section("magnet-bank-worker");
     92         Ok(Self {
     93             payto,
     94             account_type: map_config!(sect, "account type", "ACCOUNT_TYPE",
     95                 "exchange" => { Ok(AccountType::Exchange) },
     96                 "normal" => { Ok(AccountType::Normal) }
     97             )
     98             .require()?,
     99             api_url: sect.parse("URL", "API_URL").require()?,
    100             consumer: Token {
    101                 key: sect.str("CONSUMER_KEY").require()?,
    102                 secret: sect.str("CONSUMER_SECRET").require()?,
    103             },
    104             keys_path: sect.path("KEYS_FILE").require()?,
    105             ignore_tx_before: sect.date("IGNORE_TRANSACTIONS_BEFORE").opt()?,
    106             ignore_bounces_before: sect.date("IGNORE_BOUNCES_BEFORE").opt()?,
    107         })
    108     }
    109 }
    110 
    111 /// magnet-bank-harness config
    112 pub struct HarnessCfg {
    113     pub worker: WorkerCfg,
    114     pub client_payto: FullHuPayto,
    115 }
    116 
    117 impl HarnessCfg {
    118     pub fn parse(cfg: &Config) -> Result<Self, ValueErr> {
    119         let worker = WorkerCfg::parse(cfg)?;
    120 
    121         let sect = cfg.section("magnet-bank-harness");
    122         let iban: HuIban = sect.parse("iban", "IBAN").require()?;
    123         let name = sect.str("NAME").require()?;
    124 
    125         Ok(Self {
    126             worker,
    127             client_payto: FullHuPayto::new(iban, name),
    128         })
    129     }
    130 }