Refactor export

This commit is contained in:
Dandaedre 2026-07-16 02:08:43 +02:00
parent f86199d497
commit 08a66bf2b8
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
3 changed files with 102 additions and 90 deletions

View File

@ -13,11 +13,9 @@
//! (`MonsterConfig`) and delegates to the appropriate sub‑routine (clean, export or show).
//!
use crate::{monster_database::fetch_all_data, monster_structs::MonsterRepetableInitRow};
use std::fmt::Write;
use crate::monster_export::generate_monsters_tex;
use anyhow::Context;
use handlebars::Handlebars;
mod monster_database;
mod monster_export;
pub mod monster_structs;
@ -27,24 +25,6 @@ use cli_parser::Config;
use config::monsters::MonsterConfig;
use rusqlite::Connection;
/// Load a template from a struct field into a `templates` collection.
///
/// $templates – the variable that holds the template collection
/// $cfg_path – any path (e.g. `config`, `self.cfg`, …) that points to a
/// struct containing the field we want
/// $name – the identifier of the field / template name
macro_rules! template_load {
($templates:ident, $cfg_path:expr, $name:ident) => {{
$templates
.register_template_string(
stringify!($name), // "intro"
&$cfg_path.$name, // &config.monster.intro
)
.context(concat!("Registering ", stringify!($name), " template"))? // ? propagates errors
}};
}
// TODO: Create a method on the struct instead of this inane bs
/// Dispatches the chosen monster action.
pub fn monsters_dispatch(args: &MonsterConfig, config: &Config) -> Result<()> {
// use config::monsters::MonsterTarget;
@ -52,65 +32,7 @@ pub fn monsters_dispatch(args: &MonsterConfig, config: &Config) -> Result<()> {
let connection =
Connection::open(&args.database).context(format!("Opening database {}", args.database))?;
let all_monsters = fetch_all_data(connection)?;
let mut templates = Handlebars::new();
template_load!(templates, &config.monster, intro);
template_load!(templates, &config.monster, body_parts_row);
template_load!(templates, &config.monster, body_parts_end);
template_load!(templates, &config.monster, blood_reward);
template_load!(templates, &config.monster, rewards_row);
template_load!(templates, &config.monster, init_rows);
template_load!(templates, &config.monster, resists_row);
template_load!(templates, &config.monster, stats);
template_load!(templates, &config.monster, intent_table);
let mut out = String::new();
let creature = &all_monsters[0];
// write!(out, "{}", templates.render("intro", &creature.data)?)?;
// write!(
// out,
// "{}",
// templates.render("blood_reward", &creature.stats)?
// )?;
// for reward in creature.rewards.rewards.iter() {
// write!(out, "{}", templates.render("rewards_row", reward)?)?;
// }
// write!(out, "{}", &config.monster.rewards_end)?;
// write!(out, "{}", &config.monster.body_parts_intro)?;
// for part in creature.body_parts.body_parts.iter() {
// write!(out, "{}", templates.render("body_parts_row", part)?)?;
// }
// write!(
// out,
// "{}",
// templates.render("body_parts_end", &creature.data)?
// )?;
// // TODO: Maybe add modularity and not print empty resists
// write!(out, "{}", &config.monster.resists_intro)?;
// for part in creature.body_parts.body_parts.iter() {
// write!(out, "{}", templates.render("resists_row", part)?)?;
// }
// write!(out, "{}", &config.monster.resists_end)?;
// // write!(out, "{}", &config.monster.stats)?;
write!(out, "{}", templates.render("stats", &creature.stats)?)?;
for init in MonsterRepetableInitRow::vec_from(
&creature.stats.p1_init,
&creature.stats.p2_init,
&creature.stats.p1_actions,
&creature.stats.p2_actions,
)
.iter()
.skip(1)
{
write!(out, "{}", templates.render("init_rows", init)?)?;
}
// write!(out, "{}", &config.monster.attacks_intro)?;
// write!(out, "{}", &config.monster.attacks_end)?;
// write!(out, "{}", templates.render("intent_table", &creature.data)?)?;
println!("{}", out);
generate_monsters_tex(args, config, connection)?;
// templates
// match args.target {
// MonsterTarget::Clean => todo!("implement cleaning logic"),

View File

@ -1,6 +1,105 @@
//! Monster export module
use serde::Deserialize;
use anyhow::{Context, Result};
use cli_parser::Config;
use config::monsters::MonsterConfig;
use handlebars::Handlebars;
use rusqlite::Connection;
use std::fmt::Write;
use crate::{monster_database::fetch_all_data, monster_structs::MonsterRepetableInitRow};
/// Load a template from a struct field into a `templates` collection.
///
/// $templates – the variable that holds the template collection
/// $cfg_path – any path (e.g. `config`, `self.cfg`, …) that points to a
/// struct containing the field we want
/// $name – the identifier of the field / template name
macro_rules! template_load {
($templates:ident, $cfg_path:expr, $name:ident) => {{
$templates
.register_template_string(
stringify!($name), // "intro"
&$cfg_path.$name, // &config.monster.intro
)
.context(concat!("Registering ", stringify!($name), " template"))? // ? propagates errors
}};
}
pub trait ToTex {
fn export_to_tex(self) -> String;
}
fn load_templates(config: &Config) -> Result<Handlebars<'_>> {
let mut templates = Handlebars::new();
template_load!(templates, &config.monster, intro);
template_load!(templates, &config.monster, body_parts_row);
template_load!(templates, &config.monster, body_parts_end);
template_load!(templates, &config.monster, blood_reward);
template_load!(templates, &config.monster, rewards_row);
template_load!(templates, &config.monster, init_rows);
template_load!(templates, &config.monster, resists_row);
template_load!(templates, &config.monster, stats);
template_load!(templates, &config.monster, intent_table);
Ok(templates)
}
// TODO: Create a method on the struct instead of this inane bs
/// Export a tex with the given config and arguments
pub fn generate_monsters_tex(
args: &MonsterConfig,
config: &Config,
connection: Connection,
) -> std::result::Result<(), anyhow::Error> {
let all_monsters = fetch_all_data(connection)?;
let templates = load_templates(config)?;
let mut out = String::new();
let creature = &all_monsters[0];
// write!(out, "{}", templates.render("intro", &creature.data)?)?;
// write!(
// out,
// "{}",
// templates.render("blood_reward", &creature.stats)?
// )?;
// for reward in creature.rewards.rewards.iter() {
// write!(out, "{}", templates.render("rewards_row", reward)?)?;
// }
// write!(out, "{}", &config.monster.rewards_end)?;
// write!(out, "{}", &config.monster.body_parts_intro)?;
// for part in creature.body_parts.body_parts.iter() {
// write!(out, "{}", templates.render("body_parts_row", part)?)?;
// }
// write!(
// out,
// "{}",
// templates.render("body_parts_end", &creature.data)?
// )?;
// // TODO: Maybe add modularity and not print empty resists
// write!(out, "{}", &config.monster.resists_intro)?;
// for part in creature.body_parts.body_parts.iter() {
// write!(out, "{}", templates.render("resists_row", part)?)?;
// }
// write!(out, "{}", &config.monster.resists_end)?;
// // write!(out, "{}", &config.monster.stats)?;
write!(out, "{}", templates.render("stats", &creature.stats)?)?;
for init in MonsterRepetableInitRow::vec_from(
&creature.stats.p1_init,
&creature.stats.p2_init,
&creature.stats.p1_actions,
&creature.stats.p2_actions,
)
.iter()
.skip(1)
{
write!(out, "{}", templates.render("init_rows", init)?)?;
}
// write!(out, "{}", &config.monster.attacks_intro)?;
// write!(out, "{}", &config.monster.attacks_end)?;
// write!(out, "{}", templates.render("intent_table", &creature.data)?)?;
println!("{}", out);
Ok(())
}

View File

@ -11,7 +11,6 @@
//! ```
//!
#![allow(dead_code)]
use std::iter::Zip;
use more_fields::FieldsAsSlice;
use serde::{Deserialize, Serialize};
@ -354,11 +353,3 @@ impl Serialize for MonsterInitString {
)
}
}
// impl From<&str> for CustomString {
// fn from(value: &str) -> Self {
// Self {
// inner: value.to_string(),
// }
// }
// }