diff --git a/helper_config.toml b/helper_config.toml index f612ad0..83f2d7c 100644 --- a/helper_config.toml +++ b/helper_config.toml @@ -1,16 +1,17 @@ [classes] header = '\subsubsection{Tier {}}' + row = '''{\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" -''' + \hline''' + end = ''' \hline \end{tabularx} -} -''' +}''' + [alchimie] [armes] diff --git a/src/table_structs.rs b/src/table_structs.rs index 9b4b10a..483f009 100644 --- a/src/table_structs.rs +++ b/src/table_structs.rs @@ -13,7 +13,7 @@ use std::{ use regex_static::once_cell::sync::Lazy; -/// Configuration options for processing CSV files and generating LaTeX tables. +/// Configuration options for processing TSV files and generating LaTeX tables. #[derive(Debug, Args)] pub(crate) struct TableArgs { /// A boolean indicating whether to generate LaTeX code and write it to an output file @@ -23,7 +23,7 @@ pub(crate) struct TableArgs { /// The kind of tables to operate on: classes, craft or a specific file pub kind: TargetType, - /// A vector of strings representing the names of CSV files to process. + /// A vector of strings representing the names of TSV files to process. pub filenames: Vec, } @@ -107,7 +107,7 @@ impl From<&str> for TargetType { #[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] -/// A single row in a martial class csv file +/// A single row in a martial class tsv file pub struct RecordClass { /// The name of a technique. pub technique: String, @@ -141,7 +141,7 @@ impl Display for RecordClass { /// Represents the whole Classes Array, with a vec for every tier pub(crate) struct ClassesArray { /// Every tier held as a vec of vec of rows - tiers: Vec>, + pub tiers: Vec>, } /// Trait needed to be writable as a LaTex table @@ -150,12 +150,8 @@ pub(crate) trait WriteRows { fn write_rows(&self, config: &Config) -> Result; } -impl ClassesArray { - /// Create a new ClassesArray from the tier list - pub fn new(tiers: Vec>) -> Self { - Self { tiers } - } -} +// impl ClassesArray { +// } impl WriteRows for ClassesArray { fn write_rows(&self, config: &Config) -> Result { let mut buffer = BufWriter::new(Vec::new()); diff --git a/src/tablegen.rs b/src/tablegen.rs index a7c3ccc..ffacc88 100644 --- a/src/tablegen.rs +++ b/src/tablegen.rs @@ -1,4 +1,4 @@ -//! The csv table generation +//! The tsv table generation use anyhow::{Context, Result}; use glob::glob; use regex::Captures; @@ -52,14 +52,13 @@ fn process_files(list_files: &mut Vec, args: &TableArgs, config: &Confi /// Process one file fn process_one_file(f: &mut PathBuf, args: &TableArgs, config: &Config) -> Result<()> { let tt = extract_type(f).context("Error extracting file type ... skipping")?; - let csv = match tt { - TableTargets::Classes => read_csv_classes(f), - _ => todo!(), - } - .context("Error reading file")?; + let tsv: Box = match tt { + TableTargets::Classes => Box::new(read_tsv_classes(f)?), + _ => Box::new(read_tsv::(f)?), + }; match tt { - TableTargets::Classes => export_csv::(f, csv, args, config), + TableTargets::Classes => export_tsv(f, tsv, args, config), TableTargets::Food => todo!(), TableTargets::Alchemy => todo!(), TableTargets::Forge => todo!(), @@ -69,8 +68,9 @@ fn process_one_file(f: &mut PathBuf, args: &TableArgs, config: &Config) -> Resul TableTargets::Gadgets => todo!(), TableTargets::Potions => todo!(), TableTargets::Traps => todo!(), + // TableTargets::Classes => todo!(), } - .context("Error exporting the csv") + .context("Error exporting the tsv") } #[inline] @@ -96,17 +96,18 @@ fn list_dir(pattern: &str) -> Vec { .collect() } -/// Converts a CSV file to LaTeX code. +/// Converts a TSV file to LaTeX code. /// -/// This function reads in a CSV file, processes its contents according to the provided configuration, +/// This function reads in a TSV 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 export_csv(f: &Path, csv: RowType, args: &TableArgs, config: &Config) -> Result<()> -where - // PathType: AsRef + Debug, - RowType: WriteRows, -{ - let converted = csv_to_string(csv, config)?; +pub fn export_tsv( + f: &Path, + tsv: Box, + args: &TableArgs, + config: &Config, +) -> Result<()> { + let converted = tsv_to_string(tsv, config)?; if args.export { let path = format!("{:?}", f).replace(".tsv", ".tex"); // let path = f.with_extension(".tex"); @@ -132,11 +133,11 @@ pub fn make_latex_string(arg: &str) -> String { res.to_string() } -/// Reads a CSV file into a table. +/// Reads a TSV file into a table. /// -/// This function reads in a comma-separated values (CSV) file and returns it as a Table object, +/// This function reads in a comma-separated values (TSV) file and returns it as a Table object, /// which can be used to generate LaTeX code or perform other operations on the data. -fn read_csv_classes(filename: &T) -> Result +fn read_tsv_classes(filename: &T) -> Result where T: AsRef, { @@ -164,44 +165,42 @@ where current_tier.borrow_mut().push(result); } } - // println!("{:#?}", levels); - Ok(ClassesArray::new(levels)) + Ok(ClassesArray { tiers: levels }) } -// /// Reads a CSV file into a table. -// /// -// /// This function reads in a comma-separated values (CSV) file and returns it as a Table object, -// /// which can be used to generate LaTeX code or perform other operations on the data. -// fn read_csv(filename: &PathType) -> Result, Error> -// where -// PathType: AsRef, -// RecordType: for<'a> serde::Deserialize<'a>, -// { -// let file = File::open(filename)?; -// let mut rdr = csv::ReaderBuilder::new() -// .has_headers(false) -// .delimiter(b'\t') -// .double_quote(false) -// .escape(Some(b'\\')) -// .flexible(true) -// .comment(Some(b'#')) -// .from_reader(file); +/// Reads a TSV file into a table. +/// +/// This function reads in a comma-separated values (TSV) file and returns it as a Table object, +/// which can be used to generate LaTeX code or perform other operations on the data. +fn read_tsv(filename: &Path) -> Result +where + RecordType: for<'a> serde::Deserialize<'a>, + RecordHolder: WriteRows, +{ + let file = File::open(filename)?; + let mut rdr = csv::ReaderBuilder::new() + .has_headers(false) + .delimiter(b'\t') + .double_quote(false) + .escape(Some(b'\\')) + .flexible(true) + .comment(Some(b'#')) + .from_reader(file); -// let mut levels = Vec::with_capacity(30); -// for result in rdr.deserialize() { -// let result: RecordType = result?; -// levels.push(result); -// } -// Ok(levels) -// } + let mut levels = Vec::with_capacity(30); + for result in rdr.deserialize() { + let result: RecordType = result?; + levels.push(result); + } + + // Ok(RecordHolder{}) + todo!() +} /// Converts a table of data into a string representation. /// /// 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. -pub(crate) fn csv_to_string(csv: RowType, config: &Config) -> Result -where - RowType: WriteRows, -{ - csv.write_rows(config) +pub(crate) fn tsv_to_string(tsv: Box, config: &Config) -> Result { + tsv.write_rows(config) }