use std::{collections::HashMap, error::Error, io::Write, path::PathBuf}; use clap::{Parser, Subcommand}; use coster_rs::{CsvCost, Unit}; use serde::Deserialize; #[derive(Parser)] #[clap(name = "coster-rs")] #[clap(author = "Pivato M. ")] #[clap(version = "0.0.1")] #[clap(about = "Simple, fast, efficient costing tool", long_about = None)] struct Cli { #[clap(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// Moves money between accounts and departments, using the given rules and lines move_money { #[clap(short = 'r', long, parse(from_os_str), value_name = "FILE")] rules: PathBuf, #[clap(short = 'l', long, parse(from_os_str), value_name = "FILE")] lines: PathBuf, #[clap(short, long, parse(from_os_str), value_name = "FILE")] output: Option, #[clap(short, long, default_value_t = false)] use_numeric_accounts: bool, }, /// Combines rules to the minimum set required smush_rules { #[clap(short = 'r', long, parse(from_os_str), value_name = "FILE")] rules: PathBuf, #[clap(short, long, parse(from_os_str), value_name = "FILE")] output: Option, }, /// Allocates servicing department amounts to operating departments allocate_overheads { #[clap(short, long, parse(from_os_str), value_name = "FILE")] rules: PathBuf, #[clap(short, long, parse(from_os_str), value_name = "FILE")] lines: PathBuf, #[clap(short, long, parse(from_os_str), value_name = "FILE")] output: Option, }, } fn main() -> anyhow::Result<()> { let cli = Cli::parse(); match cli.command { Commands::move_money { rules, lines, output, use_numeric_accounts, } => move_money(rules, lines, output, use_numeric_accounts), Commands::smush_rules { rules, output } => smush_rules(rules, output), Commands::allocate_overheads { rules, lines, output, } => allocate_overheads(rules, lines, output), } } fn move_money( rules: PathBuf, lines: PathBuf, output: Option, use_numeric_accounts: bool, ) -> anyhow::Result<()> { coster_rs::move_money( csv::Reader::from_path(rules)?, csv::Reader::from_path(lines)?, csv::Writer::from_path(output.unwrap_or(PathBuf::from("output.csv")))?, use_numeric_accounts, ) } fn smush_rules(rules_path: PathBuf, output: Option) -> anyhow::Result<()> { Ok(()) } fn allocate_overheads( rules_path: PathBuf, lines: PathBuf, output: Option, ) -> anyhow::Result<()> { let mut rdr = csv::Reader::from_path(rules_path)?; for result in rdr.deserialize() { let record: CsvOverheadAllocationRule = result?; } let mut account_reader = csv::Reader::from_path(lines)?; for result in account_reader.deserialize() { let record: CsvCost = result?; } Ok(()) } #[derive(Debug, Deserialize)] struct CsvOverheadAllocationRule { from_overhead_department: String, to_department: String, percent: f64, to_department_type: String, }