use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum QualityPreset {
Draft,
#[default]
Standard,
High,
Print,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum FontEmbedding {
None,
Subset,
#[default]
Full,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub quality: QualityPreset,
pub font_embedding: FontEmbedding,
pub custom_font: Option<PathBuf>,
pub verbose: bool,
pub debug: bool,
pub output_dir: PathBuf,
pub keep_intermediate: bool,
pub compression_level: u8,
pub cache_math: bool,
pub cache_fonts: bool,
pub max_cache_size: usize,
}
impl Default for Config {
fn default() -> Self {
Self {
quality: QualityPreset::default(),
font_embedding: FontEmbedding::default(),
custom_font: None,
verbose: false,
debug: false,
output_dir: PathBuf::from("output"),
keep_intermediate: false,
compression_level: 6,
cache_math: true,
cache_fonts: true,
max_cache_size: 100, }
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn with_quality(quality: QualityPreset) -> Self {
let mut config = Self {
quality,
..Self::default()
};
config.apply_quality_preset();
config
}
fn apply_quality_preset(&mut self) {
match self.quality {
QualityPreset::Draft => {
self.compression_level = 3;
self.font_embedding = FontEmbedding::Subset;
self.cache_math = true;
self.cache_fonts = true;
}
QualityPreset::Standard => {
self.compression_level = 6;
self.font_embedding = FontEmbedding::Full;
self.cache_math = true;
self.cache_fonts = true;
}
QualityPreset::High => {
self.compression_level = 7;
self.font_embedding = FontEmbedding::Full;
self.cache_math = false; self.cache_fonts = true;
}
QualityPreset::Print => {
self.compression_level = 9;
self.font_embedding = FontEmbedding::Full;
self.cache_math = false;
self.cache_fonts = false;
}
}
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn debug(mut self, debug: bool) -> Self {
self.debug = debug;
self
}
pub fn output_dir(mut self, dir: PathBuf) -> Self {
self.output_dir = dir;
self
}
pub fn custom_font(mut self, font_path: PathBuf) -> Self {
self.custom_font = Some(font_path);
self
}
pub fn compression_level(mut self, level: u8) -> Self {
self.compression_level = level.min(9);
self
}
pub fn caching(mut self, enabled: bool) -> Self {
self.cache_math = enabled;
self.cache_fonts = enabled;
self
}
pub fn keep_intermediate(mut self, keep: bool) -> Self {
self.keep_intermediate = keep;
self
}
pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
toml::to_string_pretty(self)
}
pub fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
toml::from_str(s)
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
pub fn from_json(s: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.quality, QualityPreset::Standard);
assert_eq!(config.compression_level, 6);
assert!(config.cache_math);
}
#[test]
fn test_quality_presets() {
let draft = Config::with_quality(QualityPreset::Draft);
assert_eq!(draft.compression_level, 3);
assert_eq!(draft.font_embedding, FontEmbedding::Subset);
let print = Config::with_quality(QualityPreset::Print);
assert_eq!(print.compression_level, 9);
assert!(!print.cache_math);
}
#[test]
fn test_builder_pattern() {
let config = Config::new()
.verbose(true)
.debug(true)
.compression_level(8)
.caching(false);
assert!(config.verbose);
assert!(config.debug);
assert_eq!(config.compression_level, 8);
assert!(!config.cache_math);
}
#[test]
fn test_toml_roundtrip() {
let config = Config::new().verbose(true).compression_level(7);
let toml_str = config.to_toml().unwrap();
let restored = Config::from_toml(&toml_str).unwrap();
assert!(restored.verbose);
assert_eq!(restored.compression_level, 7);
}
#[test]
fn test_json_roundtrip() {
let config = Config::new().debug(true).caching(false);
let json_str = config.to_json().unwrap();
let restored = Config::from_json(&json_str).unwrap();
assert!(restored.debug);
assert!(!restored.cache_math);
}
}