ravenous-tools/src/main.rs

127 lines
3.4 KiB
Rust

//! The `ravenous-tool` command-line tool.
//!
//! This program is meant as a helper for the ravenous project.
//!
// #![allow(dead_code)]
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
mod makepdf;
mod makepdf_structs;
mod stat_algo;
mod stat_io;
mod stat_structs;
mod stats;
mod table_structs;
mod table_types;
mod tablegen;
use crate::table_structs::TableArgs;
use std::{
fs::{self, OpenOptions},
io::Write,
};
use anyhow::{Context, Result};
use clap::{Args, Parser, Subcommand};
use makepdf::{make_pdf, MakeConfig};
use serde::Deserialize;
use stats::{stat_solver, StatConfig};
use table_structs::Craft;
use tablegen::table_gen;
#[derive(Parser, Debug)]
#[command(version, about)]
#[command(propagate_version = true)]
/// The list of commands to run
struct ParserCLIArgs {
/// The subcommand to run.
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
/// Enum representing the available commands.
enum Commands {
/// Generate a table from input data using the provided arguments.
Tablegen(TableArgs),
/// Show/Export config
Config(ConfigArgs),
/// Handle stat repartition
Stats(StatConfig),
/// Make a pdf via pdflatex
Make(MakeConfig),
}
#[derive(Debug, Args)]
struct ConfigArgs {
/// Wether to write the config to file, or just show it
#[arg(short, long, default_value_t = false)]
write: bool,
/// Wether to overwrite the file if it exists
#[arg(short, long, default_value_t = false)]
force: bool,
/// The path to overwrite to
path: Option<String>,
}
#[derive(Deserialize)]
/// The config struct, holding all runtime information
struct Config {
/// Config specific for the classes table generation
classes: Classes,
/// Config for all the tables
craft: Craft,
}
#[derive(Deserialize)]
/// The classes config
struct Classes {
/// The header string formatter
header: String,
/// The row string formatter
row: String,
/// The table end string formatter
end: String,
}
fn main() -> Result<()> {
let args = ParserCLIArgs::parse();
let config_str = &fs::read_to_string("helper_config.toml")
.unwrap_or(include_str!("../helper_config.toml").to_string());
let config: Config = toml::from_str(config_str).expect("Coudln't read the config file");
match args.command {
Commands::Tablegen(args) => table_gen(&args, &config),
Commands::Config(args) => write_config(&args, &config, config_str),
Commands::Stats(args) => stat_solver(&args, &config),
Commands::Make(args) => make_pdf(&args, &config),
}
}
/// Write the config either to stdout, or to a file
fn write_config(args: &ConfigArgs, _configg: &Config, config_str: &str) -> Result<()> {
// if
// fs::write("./helper_config.toml", config_str).context("Writing config")
if args.write {
let p = match &args.path {
Some(p) => p,
None => "./helper_config.toml",
};
let mut file = match &args.force {
true => OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(p),
false => OpenOptions::new().write(true).create_new(true).open(p),
}
.with_context(|| format!("Trying to write to file {}", p))?;
file.write_all(config_str.as_bytes())?;
println!("Config sucessfuly written to {}", p);
} else {
println!("{}", config_str);
}
Ok(())
}