diff --git a/crates/cli_parser/src/lib.rs b/crates/cli_parser/src/lib.rs index 5048fde..f8d31d4 100644 --- a/crates/cli_parser/src/lib.rs +++ b/crates/cli_parser/src/lib.rs @@ -4,6 +4,7 @@ use clap::{Args, Parser, Subcommand}; use config::{ + formatter::FormatterConfig, graph::GraphConfig, make::{ HELP_BLOCK_SELECTION, HELP_BUILD_TARGET_LIST, HELP_VERBOSITY_LEVEL, MakeTargets, @@ -42,6 +43,8 @@ pub enum Commands { Make(MakeConfig), /// Make all graphs Graphs(GraphConfig), + /// Format the file given on input + Format(FormatterConfig), } #[derive(Debug, Args)] diff --git a/crates/config/src/formatter.rs b/crates/config/src/formatter.rs new file mode 100644 index 0000000..d7dcd73 --- /dev/null +++ b/crates/config/src/formatter.rs @@ -0,0 +1,13 @@ +// Workspace: config +// File: config/src/formatter.rs +//! The inner config for formatter module + +use clap::Args; + +#[derive(Args, Debug, Clone)] +/// The configuration struct for the Formatter +pub struct FormatterConfig { + /// Export the config + #[arg(short, long)] + pub config: bool, +} diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index c0be66a..cb79b28 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -8,6 +8,7 @@ // Internal Modules pub mod fast_tables; +pub mod formatter; pub mod graph; pub mod make; pub mod statsolver; diff --git a/crates/fast_class/src/lib.rs b/crates/fast_class/src/lib.rs index d33de83..ead77e2 100644 --- a/crates/fast_class/src/lib.rs +++ b/crates/fast_class/src/lib.rs @@ -19,9 +19,9 @@ //! active, crowd-control). This enum is defined for readability but the //! current `Skill::t_type` field uses raw `u8` values - see the note in //! `Skill` for guidance on converting between the two. -// +//! //! The module is free of external dependencies except for `facet`, which provides -//! the procedural macro used for deriving serialisation support. +//! the procedural macro used for deriving serialisation support. use facet::Facet; diff --git a/crates/formatter/Cargo.toml b/crates/formatter/Cargo.toml new file mode 100644 index 0000000..c4a0f90 --- /dev/null +++ b/crates/formatter/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "formatter" +version = "0.1.0" +edition = "2024" + +[dependencies] +config = {path = "../config"} +cli_parser = {path = "../cli_parser"} +regex = "1.11.1" +serde = { version = "1.0.217", features = ["derive", "alloc"] } +toml = "0.8.20" +anyhow = "1.0.95" diff --git a/crates/formatter/src/formating.rs b/crates/formatter/src/formating.rs new file mode 100644 index 0000000..a581a34 --- /dev/null +++ b/crates/formatter/src/formating.rs @@ -0,0 +1,69 @@ +//! 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 std::fs; +use std::io::{self, Read}; + +const SAMPLE_CONFIG: &str = r#"[[patterns]] +regex = "\\p{Pd}" +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 = " "" +"#; + +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") + // .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))?; + + // ------------------------------------------------------------------ + // 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))?; + + // // ------------------------------------------------------------------ + // // 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(()) +} diff --git a/crates/formatter/src/lib.rs b/crates/formatter/src/lib.rs new file mode 100644 index 0000000..5589654 --- /dev/null +++ b/crates/formatter/src/lib.rs @@ -0,0 +1,8 @@ +//! This workspace_crate is a Rust library designed to Format latex documents on save. + +#![warn(missing_docs, clippy::missing_docs_in_private_items)] +// Internal modules +mod formating; +// +// Re Export +pub use formating::format_file; diff --git a/crates/ravenous-tools/Cargo.toml b/crates/ravenous-tools/Cargo.toml index b2609e0..52d31fe 100644 --- a/crates/ravenous-tools/Cargo.toml +++ b/crates/ravenous-tools/Cargo.toml @@ -11,6 +11,7 @@ cli_parser = {path = "../cli_parser"} # config = {path = "../config"} stats = {path = "../stats"} make = {path = "../make"} +formatter = {path = "../formatter"} anyhow = "1.0.95" clap = { version = "4.5.28", features = ["derive"] } diff --git a/crates/ravenous-tools/src/main.rs b/crates/ravenous-tools/src/main.rs index 3520dc7..cabeab5 100644 --- a/crates/ravenous-tools/src/main.rs +++ b/crates/ravenous-tools/src/main.rs @@ -6,6 +6,7 @@ #![warn(clippy::missing_docs_in_private_items)] // Workspace +use formatter::format_file; use graphs::make_graphs; use make::make_pdf; use stats::stat_solver; @@ -31,6 +32,7 @@ fn main() -> Result<()> { Commands::Stats(args) => stat_solver(&args, &config), Commands::Make(args) => make_pdf(&args, &config), Commands::Graphs(args) => make_graphs(&args, &config), + Commands::Format(args) => format_file(&args, &config), } }