use serde::{Deserialize, Serialize};
use std::path::Path;
pub const DEFAULT_MAX_LINE_LENGTH: usize = 120;
pub const DEFAULT_BOX_PADDING: usize = 1;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FormattingConfig {
pub max_line_length: usize,
pub box_padding: usize,
pub preserve_unicode: bool,
pub validate_diagrams: bool,
}
impl Default for FormattingConfig {
fn default() -> Self {
Self {
max_line_length: DEFAULT_MAX_LINE_LENGTH,
box_padding: DEFAULT_BOX_PADDING,
preserve_unicode: true,
validate_diagrams: false,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
pub formatting: FormattingConfig,
pub enable_flowcharts: bool,
pub enable_sequence_diagrams: bool,
#[serde(default = "default_true")]
pub fenced_diagrams: bool,
}
const fn default_true() -> bool {
true
}
impl Default for Config {
fn default() -> Self {
Self {
formatting: FormattingConfig::default(),
enable_flowcharts: false,
enable_sequence_diagrams: false,
fenced_diagrams: true,
}
}
}
impl Config {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
let contents = std::fs::read_to_string(path)?;
let config: Self = toml::from_str(&contents)?;
Ok(config)
}
pub fn load_from_cwd() -> Result<Self, Box<dyn std::error::Error>> {
let mut current = std::env::current_dir()?;
loop {
let config_path = current.join(".ascfix.toml");
if config_path.exists() {
return Self::from_file(config_path);
}
if !current.pop() {
break;
}
}
Ok(Self::default())
}
#[allow(dead_code)] pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn std::error::Error>> {
let toml = toml::to_string_pretty(self)?;
std::fs::write(path, toml)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.formatting.max_line_length, DEFAULT_MAX_LINE_LENGTH);
assert_eq!(config.formatting.box_padding, DEFAULT_BOX_PADDING);
assert!(config.formatting.preserve_unicode);
assert!(!config.enable_flowcharts);
assert!(config.fenced_diagrams);
}
#[test]
fn test_load_config_from_file() {
let mut temp_file = NamedTempFile::new().unwrap();
let config_content = r"enable_flowcharts = true
enable_sequence_diagrams = false
[formatting]
max_line_length = 100
box_padding = 2
preserve_unicode = false
validate_diagrams = true
";
temp_file.write_all(config_content.as_bytes()).unwrap();
temp_file.flush().unwrap();
let config = Config::from_file(temp_file.path()).unwrap();
assert_eq!(config.formatting.max_line_length, 100);
assert_eq!(config.formatting.box_padding, 2);
assert!(!config.formatting.preserve_unicode);
assert!(config.formatting.validate_diagrams);
assert!(config.enable_flowcharts);
assert!(!config.enable_sequence_diagrams);
assert!(config.fenced_diagrams);
}
}