From df688646deef8aab5b4cffa66088baa512e1e042 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 23 Apr 2026 15:31:35 +0200 Subject: [PATCH] First draft of the configurator --- crates/formatter/src/formating.rs | 74 ++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/crates/formatter/src/formating.rs b/crates/formatter/src/formating.rs index a581a34..9c8aa95 100644 --- a/crates/formatter/src/formating.rs +++ b/crates/formatter/src/formating.rs @@ -4,10 +4,13 @@ use anyhow::{Context, Result}; use cli_parser::Config; use config::formatter::FormatterConfig; use regex::Regex; +use serde::Deserialize; use std::fs; use std::io::{self, Read}; -const SAMPLE_CONFIG: &str = r#"[[patterns]] +/// The base config for the project +const SAMPLE_CONFIG: &str = r#"# Base config, loaded from .fmtconfig.tml +[[patterns]] regex = "\\p{Pd}" replacement = "-" @@ -21,49 +24,68 @@ replacement = "" [[patterns]] regex = "[\\u202F]" -replacement = " "" +replacement = "" "#; +#[derive(Debug, Deserialize)] +/// A simple regex replacement config +struct RegexConfig { + /// The list of pattterns of replacement + patterns: Vec, +} + +#[derive(Debug, Deserialize)] +/// The inner structure for a regex replacement pattern +struct Pattern { + /// A regex matching a formatting error + regex: String, + /// The replacement string + replacement: String, +} + +/// Format a file according to the formatting guidelines pub fn format_file(args: &FormatterConfig, _config: &Config) -> Result<()> { // If --config was requested, print the sample config and exit. - // if args.config { - // println!("{}", SAMPLE_CONFIG); - // return Ok(()); - // } + if args.config { + println!("{}", SAMPLE_CONFIG); + return Ok(()); + } // // ------------------------------------------------------------------ // // 1. Read and parse the .fmtconfig.toml file. // // ------------------------------------------------------------------ - // let toml_text = fs::read_to_string(".fmtconfig.toml") - // .map_err(|e| format!("Failed to read .fmtconfig.toml: {}", e))?; - // let config: Config = toml::from_str(&toml_text) - // .map_err(|e| format!("Failed to parse .fmtconfig.toml: {}", e))?; + let toml_text = fs::read_to_string(".fmtconfig.toml") + .context("Failed to read .fmtconfig.toml") + .unwrap_or(SAMPLE_CONFIG.to_string()); + + let config: RegexConfig = + toml::from_str(&toml_text).context("Failed to parse .fmtconfig.toml")?; // ------------------------------------------------------------------ // 2. Read the entire stdin into a string. // ------------------------------------------------------------------ - // let mut input = String::new(); - // io::stdin() - // .read_to_string(&mut input) - // .map_err(|e| format!("Failed to read stdin: {}", e))?; + let mut input = String::new(); + io::stdin() + .read_to_string(&mut input) + .context("Failed to read stdin")?; - // // ------------------------------------------------------------------ - // // 3. Apply the patterns sequentially. - // // ------------------------------------------------------------------ - // let mut output = input; - // for pat in &config.patterns { - // // Compile the regex lazily; panic on bad patterns in this demo. - // let re = Regex::new(&pat.regex)?; - // output = re - // .replace_all(&output, pat.replacement.as_str()) - // .into_owned(); - // } + // ------------------------------------------------------------------ + // 3. Apply the patterns sequentially. + // ------------------------------------------------------------------ + let mut output = input; + for pat in &config.patterns { + // Compile the regex lazily; panic on bad patterns in this demo. + let re = Regex::new(&pat.regex)?; + output = re + .replace_all(&output, pat.replacement.as_str()) + .into_owned(); + } // ------------------------------------------------------------------ // 4. Write the result to stdout. // ------------------------------------------------------------------ - // print!("{}", output); + print!("{}", output); Ok(()) }