Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: formatter #770

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn cli() -> Command {
.about("Use HEMTT standalone utils")
.subcommand_required(false)
.arg_required_else_help(true)
.subcommand(utils::config::cli())
.subcommand(utils::inspect::cli())
.subcommand(utils::paa::cli())
.subcommand(utils::pbo::cli())
Expand All @@ -21,6 +22,7 @@ pub fn cli() -> Command {
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<Report, Error> {
match matches.subcommand() {
Some(("config", matches)) => utils::config::execute(matches),
Some(("inspect", matches)) => utils::inspect::execute(matches),
Some(("paa", matches)) => utils::paa::execute(matches),
Some(("pbo", matches)) => utils::pbo::execute(matches),
Expand Down
65 changes: 65 additions & 0 deletions bin/src/utils/config/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::{
path::{Path, PathBuf},
sync::{atomic::AtomicUsize, Arc},
};

use clap::{ArgMatches, Command};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use crate::Error;

#[must_use]
pub fn cli() -> Command {
Command::new("fmt").about("Format the config files").arg(
clap::Arg::new("path")
.help("Path to the config file or a folder to recursively fix")
.required(true),
)
}

/// Execute the convert command
///
/// # Errors
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
let path = PathBuf::from(matches.get_one::<String>("path").expect("required"));
if path.is_dir() {
let count = Arc::new(AtomicUsize::new(0));
let entries = walkdir::WalkDir::new(&path)
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
entries
.par_iter()
.map(|entry| {
if entry.file_type().is_file()
&& entry.path().extension().unwrap_or_default() == "cpp"
&& entry.path().extension().unwrap_or_default() == "hpp"
&& file(entry.path())?
{
count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
info!("Format `{}`", entry.path().display());
}
Ok(())
})
.collect::<Result<Vec<_>, Error>>()?;
info!(
"Format {} files",
count.load(std::sync::atomic::Ordering::Relaxed)
);
} else if file(&path)? {
info!("Format `{}`", path.display());
} else {
info!("No changes in `{}`", path.display());
}
Ok(())
}

fn file(path: &Path) -> Result<bool, Error> {
// TODO do not release this lmao
let content = std::fs::read_to_string(path)?;
println!(
"{}",
hemtt_config::fmt::format(&content).expect("errors later")
);
Ok(true)
}
28 changes: 28 additions & 0 deletions bin/src/utils/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod fmt;

use clap::{ArgMatches, Command};

use crate::Error;

#[must_use]
pub fn cli() -> Command {
Command::new("config")
.about("Commands for Config files")
.arg_required_else_help(true)
.subcommand(fmt::cli())
}

/// Execute the paa command
///
/// # Errors
/// [`Error`] depending on the modules
///
/// # Panics
/// If the args are not present from clap
pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
match matches.subcommand() {
Some(("fmt", matches)) => fmt::execute(matches),

_ => unreachable!(),
}
}
1 change: 1 addition & 0 deletions bin/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config;
pub mod inspect;
pub mod paa;
pub mod pbo;
Expand Down
Loading
Loading