Complete move money between departments
This commit is contained in:
111
src/main.rs
111
src/main.rs
@@ -1,55 +1,116 @@
|
||||
use std::path::PathBuf;
|
||||
use std::{error::Error, io::Write, path::PathBuf};
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap::{builder::PathBufValueParser, Parser, Subcommand};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "coster-rs", author = "Pivato M. <mpivato4@gmail.com>", version = "0.0.1", about = "Simple, fast, efficient costing tool", long_about = None)]
|
||||
#[clap(name = "coster-rs")]
|
||||
#[clap(author = "Pivato M. <mpivato4@gmail.com>")]
|
||||
#[clap(version = "0.0.1")]
|
||||
#[clap(about = "Simple, fast, efficient costing tool", long_about = None)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
#[clap(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Moves money between accounts and departments, using the given rules and lines
|
||||
move_money {
|
||||
#[arg(short = 'r', long, value_name = "FILE")]
|
||||
#[clap(short = 'r', long, parse(from_os_str), value_name = "FILE")]
|
||||
rules: PathBuf,
|
||||
|
||||
#[arg(short, long, value_name = "FILE")]
|
||||
data: PathBuf,
|
||||
|
||||
#[arg(short, long, value_name = "FILE", default_value = "./output.csv")]
|
||||
output: PathBuf,
|
||||
},
|
||||
allocate_overheads {
|
||||
#[arg(short, long, value_name = "FILE")]
|
||||
rules: PathBuf,
|
||||
|
||||
#[arg(short, long, value_name = "FILE")]
|
||||
#[clap(short = 'l', long, parse(from_os_str), value_name = "FILE")]
|
||||
lines: PathBuf,
|
||||
|
||||
#[arg(short, long, value_name = "FILE", default_value = "./output.csv")]
|
||||
output: PathBuf,
|
||||
#[clap(short, long, parse(from_os_str), value_name = "FILE")]
|
||||
output: Option<PathBuf>,
|
||||
},
|
||||
/// 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<PathBuf>,
|
||||
},
|
||||
/// 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<PathBuf>,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO: Return error (implement the required trait to allow an error to be returned)
|
||||
fn main() {
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::move_money { rules, data, output } => move_money(rules, data, output),
|
||||
Commands::move_money {
|
||||
rules,
|
||||
lines,
|
||||
output,
|
||||
} => move_money(rules, lines, output),
|
||||
Commands::smush_rules { rules, output } => smush_rules(rules, output)?,
|
||||
Commands::allocate_overheads {
|
||||
rules,
|
||||
lines,
|
||||
output,
|
||||
} => allocate_overheads(),
|
||||
} => allocate_overheads(rules, lines, output)?,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn move_money(rules: PathBuf, lines: PathBuf, output: Option<PathBuf>) {
|
||||
// read rules into required struct (basically map each line into an array)
|
||||
|
||||
// Read gl lines data (all of it). For each line, sum the periods, and insert the summed
|
||||
// line into a hashmap (need to read the whole gl in as we can move between every line)
|
||||
|
||||
// Then run move_moeny, and output the result into a new file at the given output location
|
||||
}
|
||||
|
||||
fn smush_rules(rules_path: PathBuf, output: Option<PathBuf>) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn allocate_overheads(
|
||||
rules_path: PathBuf,
|
||||
lines: PathBuf,
|
||||
output: Option<PathBuf>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut rdr = csv::Reader::from_path(rules_path)?;
|
||||
for result in rdr.deserialize() {
|
||||
let record: CsvMovementRule = result?;
|
||||
}
|
||||
|
||||
let mut account_reader = csv::Reader::from_path(lines)?;
|
||||
|
||||
for result in account_reader.deserialize() {
|
||||
let record: CsvAccountCost = result?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn move_money(rules: PathBuf, data: PathBuf, output: PathBuf) {
|
||||
// Read all rules/data into memory (can figure out an alternative way later)
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CsvMovementRule {
|
||||
from_department: String,
|
||||
to_department: String,
|
||||
amount: f64,
|
||||
is_percent: Option<bool>,
|
||||
is_separator: Option<bool>,
|
||||
}
|
||||
|
||||
fn allocate_overheads() {}
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CsvAccountCost {
|
||||
account: String,
|
||||
department: String,
|
||||
value: f64,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user