use clap::Parser;
use std::fs;
use std::io::{self, Read, Write};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
input: Option<String>,
#[arg(short, long)]
output: Option<String>,
}
fn main() -> io::Result<()> {
let args = Cli::parse();
let input = if let Some(input_file) = args.input {
fs::read_to_string(input_file)?
} else {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer)?;
buffer
};
let output = transform_code(&input);
if let Some(output_file) = args.output {
fs::write(output_file, output)?;
} else {
io::stdout().write_all(output.as_bytes())?;
}
Ok(())
}
fn transform_code(input: &str) -> String {
let lines: Vec<&str> = input.lines().collect();
let symbols = ['{', '}', ';', ','];
let max_len = lines.iter().map(|line| line.len()).max().unwrap_or(0);
lines
.into_iter()
.map(|line| {
if line.trim_end().ends_with(symbols) {
line.rfind(|c| symbols.contains(&c)).map_or_else(|| line.to_string(), |pos| {
let padding = max_len - pos;
format!("{}{}{}", &line[..pos], " ".repeat(padding), &line[pos..])
})
} else {
line.to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
}