Stalled at the creation of generic types

This commit is contained in:
Dandaedre 2024-05-05 03:17:10 +02:00
parent b769294c06
commit 658c6e1e7b
3 changed files with 61 additions and 65 deletions

View File

@ -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]

View File

@ -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<String>,
}
@ -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<Vec<RecordClass>>,
pub tiers: Vec<Vec<RecordClass>>,
}
/// Trait needed to be writable as a LaTex table
@ -150,12 +150,8 @@ pub(crate) trait WriteRows {
fn write_rows(&self, config: &Config) -> Result<String>;
}
impl ClassesArray {
/// Create a new ClassesArray from the tier list
pub fn new(tiers: Vec<Vec<RecordClass>>) -> Self {
Self { tiers }
}
}
// impl ClassesArray {
// }
impl WriteRows for ClassesArray {
fn write_rows(&self, config: &Config) -> Result<String> {
let mut buffer = BufWriter::new(Vec::new());

View File

@ -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<PathBuf>, 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<dyn WriteRows> = match tt {
TableTargets::Classes => Box::new(read_tsv_classes(f)?),
_ => Box::new(read_tsv::<RecordClass, ClassesArray>(f)?),
};
match tt {
TableTargets::Classes => export_csv::<ClassesArray>(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<PathBuf> {
.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<RowType>(f: &Path, csv: RowType, args: &TableArgs, config: &Config) -> Result<()>
where
// PathType: AsRef<Path> + Debug,
RowType: WriteRows,
{
let converted = csv_to_string(csv, config)?;
pub fn export_tsv(
f: &Path,
tsv: Box<dyn WriteRows>,
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<T>(filename: &T) -> Result<ClassesArray, Error>
fn read_tsv_classes<T>(filename: &T) -> Result<ClassesArray, Error>
where
T: AsRef<Path>,
{
@ -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<PathType, RecordType>(filename: &PathType) -> Result<Vec<RecordType>, Error>
// where
// PathType: AsRef<Path>,
// 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<RecordType, RecordHolder>(filename: &Path) -> Result<RecordHolder>
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<RowType>(csv: RowType, config: &Config) -> Result<String>
where
RowType: WriteRows,
{
csv.write_rows(config)
pub(crate) fn tsv_to_string(tsv: Box<dyn WriteRows>, config: &Config) -> Result<String> {
tsv.write_rows(config)
}