100 lines
3.0 KiB
Rust
100 lines
3.0 KiB
Rust
//! A simple formatter that reads patterns from a TOML file,
|
|
//! applies them to stdin, and writes the formatted text to stdout.
|
|
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};
|
|
|
|
/// The base config for the project
|
|
const SAMPLE_CONFIG: &str = r#"# Base config, loaded from .fmtconfig.tml
|
|
[[patterns]]
|
|
regex = "\\p{Pd}"
|
|
replacement = "-"
|
|
|
|
[[patterns]]
|
|
regex = "[\u2212]"
|
|
replacement = "-"
|
|
|
|
[[patterns]]
|
|
regex = "[\u2018\u2019\u201A\u201B\u02BC\u02BB\u00B4\uFF40\uFF07]"
|
|
replacement = "'"
|
|
|
|
[[patterns]]
|
|
regex = "[\\u00AD\\u200B\\u200C\\u200D\\uFEFF\\u2060-\\u2064\\uFE00-\\uFE0F]"
|
|
replacement = ""
|
|
|
|
[[patterns]]
|
|
regex = "[\\u202F]"
|
|
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(());
|
|
}
|
|
|
|
// // ------------------------------------------------------------------
|
|
// // 1. Read and parse the .fmtconfig.toml file.
|
|
// // ------------------------------------------------------------------
|
|
|
|
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();
|
|
if args.stdin {
|
|
io::stdin()
|
|
.read_to_string(&mut input)
|
|
.context("Failed to read stdin")?;
|
|
} else {
|
|
unimplemented!("Use --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();
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// 4. Write the result to stdout.
|
|
// ------------------------------------------------------------------
|
|
print!("{}", output);
|
|
|
|
Ok(())
|
|
}
|