Adds config and doc
This commit is contained in:
parent
c4a6c50951
commit
6c60a76b88
|
|
@ -9,9 +9,11 @@ edition = "2021"
|
|||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
csv = "1.3.0"
|
||||
dynfmt = { version = "0.1.5", features = ["curly"] }
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
regex = "1.10.4"
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
toml = "0.8.12"
|
||||
|
||||
[profile.dev]
|
||||
debug = 0
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
[classes]
|
||||
header = '\subsubsection{Tier {}}'
|
||||
row = '''
|
||||
let open = r#"{\footnotesize
|
||||
\rowcolors{2}{gray!25}{white}
|
||||
\begin{tabularx}{\textwidth}{|p{0.17\linewidth\hyphenpenalty=9000}lp{0.13\linewidth}p{0.042\linewidth}X|}
|
||||
\hline\rowcolor{gray!50}\hline
|
||||
Technique & Niv & Type & Coût & Effet\\\hline"#;
|
||||
'''
|
||||
end = '''
|
||||
r" \hline
|
||||
\end{tabularx}
|
||||
}
|
||||
'''
|
||||
30
src/main.rs
30
src/main.rs
|
|
@ -6,14 +6,17 @@
|
|||
#![warn(missing_docs)]
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
mod tablegen;
|
||||
use std::fs;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use serde::Deserialize;
|
||||
use tablegen::{table_gen, TableArgs};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
#[command(propagate_version = true)]
|
||||
/// The list of commands to run
|
||||
struct Arguments {
|
||||
struct Args {
|
||||
/// The subcommand to run.
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
|
|
@ -26,9 +29,30 @@ enum Commands {
|
|||
Tablegen(TableArgs),
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
/// The config struct, holding all runtime information
|
||||
struct Config {
|
||||
/// Config specific for the classes table generation
|
||||
classes: Classes,
|
||||
}
|
||||
|
||||
#[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() {
|
||||
let args = Arguments::parse();
|
||||
let args = Args::parse();
|
||||
let config = &fs::read_to_string("helper_config.toml")
|
||||
.unwrap_or(include_str!("../helper_config.toml").to_string());
|
||||
let config: Config = toml::from_str(config).expect("Coudln't read the config file");
|
||||
match args.command {
|
||||
Commands::Tablegen(args) => table_gen(&args),
|
||||
Commands::Tablegen(args) => table_gen(&args, &config),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
//! The csv table generation
|
||||
use clap::Args;
|
||||
use dynfmt::{Format, SimpleCurlyFormat};
|
||||
use phf::phf_map;
|
||||
use serde::Deserialize;
|
||||
use std::io::{Error, Write};
|
||||
use std::path::Path;
|
||||
use std::{cell::RefCell, fmt, fs::File, io::BufWriter};
|
||||
|
||||
use crate::Config;
|
||||
|
||||
/// Configuration options for processing CSV files and generating LaTeX tables.
|
||||
#[derive(Debug, Args)]
|
||||
pub struct TableArgs {
|
||||
|
|
@ -69,12 +72,12 @@ struct Record {
|
|||
effet: CleanString,
|
||||
}
|
||||
|
||||
pub fn table_gen(args: &TableArgs) {
|
||||
pub fn table_gen(args: &TableArgs, config: &Config) {
|
||||
if args.filenames.is_empty() {
|
||||
println!("No files to process ... skipping");
|
||||
}
|
||||
for f in &args.filenames {
|
||||
if let Err(e) = convert_file(f, args) {
|
||||
if let Err(e) = convert_file(f, args, config) {
|
||||
println!("Error while processing {}: {}", f, e)
|
||||
}
|
||||
}
|
||||
|
|
@ -84,9 +87,9 @@ pub fn table_gen(args: &TableArgs) {
|
|||
/// This function reads in a CSV file, processes its contents according to the provided configuration,
|
||||
/// and writes out the resulting LaTeX code as a string. If an error occurs during processing or writing,
|
||||
/// it will be returned as part of the result.
|
||||
pub fn convert_file(f: &String, args: &TableArgs) -> Result<(), Error> {
|
||||
pub fn convert_file(f: &String, args: &TableArgs, config: &Config) -> Result<(), Error> {
|
||||
let csv = read_csv(f)?;
|
||||
let converted = records_to_string(csv)?;
|
||||
let converted = records_to_string(csv, config)?;
|
||||
if args.export {
|
||||
let path = f.replace(".tsv", ".tex");
|
||||
println!("[Info] Writing to {}", path);
|
||||
|
|
@ -151,13 +154,13 @@ where
|
|||
.comment(Some(b'#'))
|
||||
.from_reader(file);
|
||||
|
||||
let mut levels = Vec::with_capacity(4);
|
||||
let current_tier = RefCell::new(Vec::with_capacity(4));
|
||||
let mut levels = Vec::with_capacity(8);
|
||||
let current_tier = RefCell::new(Vec::with_capacity(6));
|
||||
for result in rdr.deserialize() {
|
||||
let result: Record = result?;
|
||||
if result.technique.starts_with("Tier ") {
|
||||
// Getto way of knowing we have a new tier
|
||||
let old_tier = current_tier.replace(Vec::with_capacity(6));
|
||||
let old_tier = current_tier.replace(Vec::with_capacity(8));
|
||||
levels.push(old_tier);
|
||||
} else if (result.technique == "Technique") | result.technique.is_empty() {
|
||||
// Skip this entry
|
||||
|
|
@ -173,16 +176,15 @@ where
|
|||
///
|
||||
/// This function takes in a Table object and returns its contents as a formatted string,
|
||||
/// which can be used to generate LaTeX code or perform other operations on the data.
|
||||
fn records_to_string(csv: Vec<Vec<Record>>) -> Result<String, Error> {
|
||||
fn records_to_string(csv: Vec<Vec<Record>>, config: &Config) -> Result<String, Error> {
|
||||
let mut buffer = BufWriter::new(Vec::new());
|
||||
for (lvl, tier) in csv.iter().skip(1).enumerate() {
|
||||
write!(buffer, "\\subsubsection{{Tier {}}}", lvl + 1)?;
|
||||
let open = r#"{\footnotesize
|
||||
\rowcolors{2}{gray!25}{white}
|
||||
\begin{tabularx}{\textwidth}{|p{0.17\linewidth\hyphenpenalty=9000}lp{0.13\linewidth}p{0.042\linewidth}X|}
|
||||
\hline\rowcolor{gray!50}\hline
|
||||
Technique & Niv & Type & Coût & Effet\\\hline"#;
|
||||
write!(buffer, "{}", open)?;
|
||||
// rt_write!(buffer, config.classes.header, lvl + 1)?;
|
||||
let formatted_header = SimpleCurlyFormat
|
||||
.format(&config.classes.header, [lvl + 1])
|
||||
.expect("Formatting failed");
|
||||
write!(buffer, "{}", formatted_header)?;
|
||||
write!(buffer, "{}", config.classes.row)?;
|
||||
for elem in tier {
|
||||
write!(
|
||||
buffer,
|
||||
|
|
@ -195,13 +197,7 @@ fn records_to_string(csv: Vec<Vec<Record>>) -> Result<String, Error> {
|
|||
elem.effet
|
||||
)?;
|
||||
}
|
||||
write!(
|
||||
buffer,
|
||||
r" \hline
|
||||
\end{{tabularx}}
|
||||
}}
|
||||
"
|
||||
)?;
|
||||
write!(buffer, "{}", config.classes.end)?;
|
||||
}
|
||||
Ok(String::from_utf8(buffer.into_inner()?).unwrap())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue