Works on tables
This commit is contained in:
parent
6c60a76b88
commit
0f97616dfd
|
|
@ -10,6 +10,7 @@ edition = "2021"
|
|||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
csv = "1.3.0"
|
||||
dynfmt = { version = "0.1.5", features = ["curly"] }
|
||||
glob = "0.3.1"
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
regex = "1.10.4"
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
//! The csv table generation
|
||||
use clap::Args;
|
||||
use dynfmt::{Format, SimpleCurlyFormat};
|
||||
use glob::glob;
|
||||
use phf::phf_map;
|
||||
use serde::Deserialize;
|
||||
use std::fmt::Debug;
|
||||
use std::io::{Error, Write};
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{cell::RefCell, fmt, fs::File, io::BufWriter};
|
||||
|
||||
use crate::Config;
|
||||
|
|
@ -57,7 +59,7 @@ impl From<&str> for TargetType {
|
|||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
/// A single row in a martial class csv file
|
||||
struct Record {
|
||||
struct RecordClass {
|
||||
/// The name of a technique.
|
||||
technique: String,
|
||||
/// The level or rank of a technique
|
||||
|
|
@ -73,30 +75,53 @@ struct Record {
|
|||
}
|
||||
|
||||
pub fn table_gen(args: &TableArgs, config: &Config) {
|
||||
if args.filenames.is_empty() {
|
||||
let list_files = match args.kind {
|
||||
TargetType::Classes => &mut list_dir("Tables/*.csv"),
|
||||
TargetType::Craft => &mut list_dir("Craft/*.csv"),
|
||||
TargetType::Files => &mut args.filenames.iter().map(PathBuf::from).collect(),
|
||||
};
|
||||
|
||||
if list_files.is_empty() {
|
||||
println!("No files to process ... skipping");
|
||||
}
|
||||
for f in &args.filenames {
|
||||
if let Err(e) = convert_file(f, args, config) {
|
||||
println!("Error while processing {}: {}", f, e)
|
||||
// match args.kind {
|
||||
// TargetType::Classes => todo!(),
|
||||
// TargetType::Craft => todo!(),
|
||||
// TargetType::Files => todo!(),
|
||||
// }
|
||||
for f in list_files {
|
||||
if let Err(e) = convert_class_csv(f, args, config) {
|
||||
println!("Error while processing {:?}: {}", f, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// List all files matching a pattern in the given directory
|
||||
#[inline]
|
||||
fn list_dir(pattern: &str) -> Vec<PathBuf> {
|
||||
glob(pattern)
|
||||
.expect("Failed to read glob pattern")
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
/// Converts a CSV file to LaTeX code.
|
||||
///
|
||||
/// 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, config: &Config) -> Result<(), Error> {
|
||||
let csv = read_csv(f)?;
|
||||
let converted = records_to_string(csv, config)?;
|
||||
pub fn convert_class_csv<T>(f: &T, args: &TableArgs, config: &Config) -> Result<(), Error>
|
||||
where
|
||||
T: AsRef<Path> + Debug,
|
||||
{
|
||||
let csv = read_csv_classes(f)?;
|
||||
let converted = csv_classes_to_string(csv, config)?;
|
||||
if args.export {
|
||||
let path = f.replace(".tsv", ".tex");
|
||||
let path = format!("{:?}", f).replace(".tsv", ".tex");
|
||||
println!("[Info] Writing to {}", path);
|
||||
let mut file = File::create(path)?;
|
||||
file.write_all(converted.as_bytes())?;
|
||||
} else {
|
||||
println!("###### [FILE:{}] ######", f);
|
||||
println!("###### [FILE:{:?}] ######", f);
|
||||
println!("{}", converted);
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -140,7 +165,7 @@ fn make_latex_string(arg: &str) -> String {
|
|||
///
|
||||
/// 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<T>(filename: &T) -> Result<Vec<Vec<Record>>, Error>
|
||||
fn read_csv_classes<T>(filename: &T) -> Result<Vec<Vec<RecordClass>>, Error>
|
||||
where
|
||||
T: AsRef<Path>,
|
||||
{
|
||||
|
|
@ -157,7 +182,7 @@ where
|
|||
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?;
|
||||
let result: RecordClass = 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(8));
|
||||
|
|
@ -176,7 +201,7 @@ 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>>, config: &Config) -> Result<String, Error> {
|
||||
fn csv_classes_to_string(csv: Vec<Vec<RecordClass>>, config: &Config) -> Result<String, Error> {
|
||||
let mut buffer = BufWriter::new(Vec::new());
|
||||
for (lvl, tier) in csv.iter().skip(1).enumerate() {
|
||||
// rt_write!(buffer, config.classes.header, lvl + 1)?;
|
||||
|
|
|
|||
Loading…
Reference in New Issue