First draft of the configurator

This commit is contained in:
Dandaedre 2026-04-23 15:31:35 +02:00
parent 11e5c6c5c5
commit df688646de
1 changed files with 48 additions and 26 deletions

View File

@ -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<Pattern>,
}
#[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(())
}