diff --git a/maid/client/cli/mod.rs b/maid/client/cli/mod.rs index 70ce1d2e69aaa8e10323df4bd23e8cfd57e5dd0c..271456466bdbb9496b4975a5c1554e9fef1936f0 100644 --- a/maid/client/cli/mod.rs +++ b/maid/client/cli/mod.rs @@ -20,14 +20,15 @@ use macros_rs::{ fs::{file_exists, folder_exists}, }; -pub fn get_version(short: bool) -> String { - return match short { - true => format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")), - false => format!("{} ({} {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"), env!("BUILD_DATE")), - }; +pub(crate) fn get_version(short: bool) -> String { + if short { + format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) + } else { + format!("{} ({} {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"), env!("BUILD_DATE")) + } } -pub fn info(path: &String) { +pub(crate) fn info(path: &String) { let values = parse::merge(path).project; let project_root = parse::file::find_maidfile_root(path); @@ -56,7 +57,7 @@ pub fn info(path: &String) { }; } -pub fn env(path: &String) { +pub(crate) fn env(path: &String) { let values = parse::merge(path); let project_name = match values.project { @@ -78,14 +79,14 @@ pub fn env(path: &String) { } } -pub fn exec(task: &str, args: &Vec<String>, path: &String, silent: bool, is_dep: bool, is_remote: bool, log_level: Option<tracing::Level>, force: bool) { +pub(crate) fn exec(task: &str, args: &Vec<String>, path: &String, silent: bool, is_dep: bool, is_remote: bool, log_level: Option<tracing::Level>, force: bool) { debug!("Starting maid {}", env!("CARGO_PKG_VERSION")); if task.is_empty() { if is_remote { - tasks::List::remote(path, silent, log_level); + tasks::list_remote(path, silent, log_level); } else { - tasks::List::all(path, silent, log_level, force); + tasks::list_all(path, silent, log_level, force); } } else { let values = parse::merge(path); diff --git a/maid/client/cli/run.rs b/maid/client/cli/run.rs index b0ff970f9a9ede716ab5dfb5f318fad14fc602dc..dfde1a8a7a62f77d521e4ad30bd8dd5326b3eac3 100644 --- a/maid/client/cli/run.rs +++ b/maid/client/cli/run.rs @@ -120,7 +120,7 @@ fn run_script(runner: Runner) { } } -pub fn task(task: cli::Task) { +pub(crate) fn task(task: cli::Task) { let mut script: Vec<&str> = vec![]; if task.script.is_str() { diff --git a/maid/client/cli/tasks.rs b/maid/client/cli/tasks.rs index ac7a2265c949596c753520a088b36ab6ad85d37d..95e309eca92ed681279582fe71460ca6f91a6f2e 100644 --- a/maid/client/cli/tasks.rs +++ b/maid/client/cli/tasks.rs @@ -7,7 +7,7 @@ use maid::log::prelude::*; use maid::{models::client::DisplayTask, table}; use text_placeholder::Template; -pub fn json(path: &String, args: &Vec<String>, hydrate: bool) { +pub(crate) fn list_json(path: &String, args: &Vec<String>, hydrate: bool) { let values = parse::merge(path); let project_root = parse::file::find_maidfile_root(path); let json = values.clone().to_json(); @@ -17,97 +17,94 @@ pub fn json(path: &String, args: &Vec<String>, hydrate: bool) { println!("{}", ternary!(hydrate.clone(), hydrated_json, json)) } -pub struct List; -impl List { - pub fn all(path: &String, silent: bool, log_level: Option<tracing::Level>, force: bool) { - let values = parse::merge(path); - let mut options: Vec<_> = values - .tasks - .iter() - .map(|(key, task)| { - let info = match &task.info { - Some(info) => match info.trim().len() < 1 { - true => "(no description)".to_string().bright_red(), - false => format!("({info})").white(), - }, - None => "(no description)".to_string().bright_red(), - }; +pub(crate) fn list_all(path: &String, silent: bool, log_level: Option<tracing::Level>, force: bool) { + let values = parse::merge(path); + let mut options: Vec<_> = values + .tasks + .iter() + .map(|(key, task)| { + let info = match &task.info { + Some(info) => match info.trim().len() < 1 { + true => "(no description)".to_string().bright_red(), + false => format!("({info})").white(), + }, + None => "(no description)".to_string().bright_red(), + }; - let verbose = match log_level.unwrap() { - tracing::Level::INFO => string!(), - _ => string!(task.script), - }; + let verbose = match log_level.unwrap() { + tracing::Level::INFO => string!(), + _ => string!(task.script), + }; - let hidden = match key.starts_with("_") { - true => true, - false => match task.hide { - Some(val) => val, - None => match task.remote.as_ref() { - Some(val) => val.exclusive, - None => false, - }, + let hidden = match key.starts_with("_") { + true => true, + false => match task.hide { + Some(val) => val, + None => match task.remote.as_ref() { + Some(val) => val.exclusive, + None => false, }, - }; - - return DisplayTask { - name: key.clone(), - formatted: format!("{} {} {}", format!("{key}").bright_yellow(), info, verbose.bright_blue()), - hidden: hidden.clone(), - }; - }) - .collect(); + }, + }; - options.retain(|key| key.hidden == false); - match Select::new("Select a task to run:", options).prompt() { - Ok(task) => { - debug!("Starting {}", task.name); - cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, false, log_level, force); - } + return DisplayTask { + name: key.clone(), + formatted: format!("{} {} {}", format!("{key}").bright_yellow(), info, verbose.bright_blue()), + hidden: hidden.clone(), + }; + }) + .collect(); - Err(_) => println!("{}", "Aborting...".white()), + options.retain(|key| key.hidden == false); + match Select::new("Select a task to run:", options).prompt() { + Ok(task) => { + debug!("Starting {}", task.name); + cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, false, log_level, force); } - } - pub fn remote(path: &String, silent: bool, log_level: Option<tracing::Level>) { - let values = parse::merge(path); - let mut options: Vec<_> = values - .tasks - .iter() - .map(|(key, task)| { - let info = match &task.info { - Some(info) => match info.trim().len() < 1 { - true => "(no description)".to_string().bright_red(), - false => format!("({info})").white(), - }, - None => "(no description)".to_string().bright_red(), - }; + Err(_) => println!("{}", "Aborting...".white()), + } +} - let verbose = match log_level.unwrap() { - tracing::Level::INFO => string!(), - _ => string!(task.script), - }; +pub(crate) fn list_remote(path: &String, silent: bool, log_level: Option<tracing::Level>) { + let values = parse::merge(path); + let mut options: Vec<_> = values + .tasks + .iter() + .map(|(key, task)| { + let info = match &task.info { + Some(info) => match info.trim().len() < 1 { + true => "(no description)".to_string().bright_red(), + false => format!("({info})").white(), + }, + None => "(no description)".to_string().bright_red(), + }; - let hidden = match task.remote { - Some(_) => false, - None => true, - }; + let verbose = match log_level.unwrap() { + tracing::Level::INFO => string!(), + _ => string!(task.script), + }; - return DisplayTask { - name: key.clone(), - formatted: format!("{} {} {}", format!("{key}").bright_yellow(), info, verbose.bright_blue()), - hidden: hidden.clone(), - }; - }) - .collect(); + let hidden = match task.remote { + Some(_) => false, + None => true, + }; - options.retain(|key| key.hidden == false); - match Select::new("Select a remote task to run:", options).prompt() { - Ok(task) => { - debug!("Starting {}", task.name); - cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, true, log_level, false); - } + return DisplayTask { + name: key.clone(), + formatted: format!("{} {} {}", format!("{key}").bright_yellow(), info, verbose.bright_blue()), + hidden: hidden.clone(), + }; + }) + .collect(); - Err(_) => println!("{}", "Aborting...".white()), + options.retain(|key| key.hidden == false); + match Select::new("Select a remote task to run:", options).prompt() { + Ok(task) => { + debug!("Starting {}", task.name); + cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, true, log_level, false); } + + Err(_) => println!("{}", "Aborting...".white()), } } diff --git a/maid/client/globals.rs b/maid/client/globals.rs index fab42cd0cf5d7f2b67f734ec69eebb8e486f0511..5bea777cbf1bdb935b13a1b836428021d14ad318 100644 --- a/maid/client/globals.rs +++ b/maid/client/globals.rs @@ -1,6 +1,6 @@ use global_placeholders::init; -pub fn init() { +pub(crate) fn init() { init!("maid.temp_dir", ".maid/temp"); init!("maid.cache_dir", ".maid/cache/{}/target"); } diff --git a/maid/client/main.rs b/maid/client/main.rs index 19a8ae1c31f183d7b1d175f3e6532e94e32f874f..9469b74e711c90359874f50d46fb10541200f289 100644 --- a/maid/client/main.rs +++ b/maid/client/main.rs @@ -26,7 +26,7 @@ macro_rules! dispatch { #[command(version = str!(cli::get_version(false)))] #[clap(disable_help_flag = true, disable_help_subcommand = true)] struct Cli { - /// Run a task defined in maidfile + /// Run a task defined in Maidfile #[arg(default_value = "", hide_default_value = true)] task: Vec<String>, @@ -116,8 +116,8 @@ fn main() { false => cli::dispatch::clean(), }, list => match cli.remote { - true => cli::tasks::List::remote(&cli.path, cli.verbose.is_silent(), cli.verbose.log_level()), - false => cli::tasks::List::all(&cli.path, cli.verbose.is_silent(), cli.verbose.log_level(), cli.force), + true => cli::tasks::list_remote(&cli.path, cli.verbose.is_silent(), cli.verbose.log_level()), + false => cli::tasks::list_all(&cli.path, cli.verbose.is_silent(), cli.verbose.log_level(), cli.force), } }); @@ -131,8 +131,8 @@ fn main() { if let Some(system) = cli.system { return match system { System::Update => cli::dispatch::update(), // add real update checker - System::Json => cli::tasks::json(&cli.path, &cli.task, false), - System::JsonHydrated => cli::tasks::json(&cli.path, &cli.task, true), + System::Json => cli::tasks::list_json(&cli.path, &cli.task, false), + System::JsonHydrated => cli::tasks::list_json(&cli.path, &cli.task, true), }; } diff --git a/maid/client/shell.rs b/maid/client/shell.rs index 59de9547188c3209f711b53f7e1e2d7de786b54b..bb39a543966e4c7235accc9906c9b9b91c2f05e2 100644 --- a/maid/client/shell.rs +++ b/maid/client/shell.rs @@ -1,8 +1,8 @@ -use std::mem; use std::fmt::{Display, Formatter, Result as FmtResult}; +use std::mem; #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum ParseError { +pub(crate) enum ParseError { UnterminatedQuote, DanglingBackslash, InvalidEscape(char), @@ -22,7 +22,7 @@ impl std::error::Error for ParseError {} #[derive(Debug)] enum State { - Delimiter, + Delimiter, Backslash, Unquoted, UnquotedBackslash, @@ -31,7 +31,6 @@ enum State { DoubleQuotedBackslash, } - pub(crate) trait IntoArgs { fn try_into_args(&self) -> Result<Vec<String>, ParseError>; } @@ -160,16 +159,14 @@ impl ArgumentParser { self.current_word.push(c); Ok(State::DoubleQuoted) } - c => Err(ParseError::InvalidEscape(c)) + c => Err(ParseError::InvalidEscape(c)), } } fn handle_end_of_input(&mut self) -> Result<(), ParseError> { match self.state { - State::SingleQuoted | State::DoubleQuoted => - Err(ParseError::UnterminatedQuote), - State::DoubleQuotedBackslash => - Err(ParseError::DanglingBackslash), + State::SingleQuoted | State::DoubleQuoted => Err(ParseError::UnterminatedQuote), + State::DoubleQuotedBackslash => Err(ParseError::DanglingBackslash), State::Backslash | State::UnquotedBackslash => { self.current_word.push('\\'); self.push_word(); diff --git a/maid/client/task/cache.rs b/maid/client/task/cache.rs index d6b0f9e92add4fc11a4232f8879b84d213c37cb4..6fc7674cf19358e25b2acdcde2d6c4f27289bafa 100644 --- a/maid/client/task/cache.rs +++ b/maid/client/task/cache.rs @@ -7,42 +7,38 @@ const DEFAULT_HASH: &str = "0000000000000000000000000000000000000000000000000000 fn bytes_to_hex(bytes: impl AsRef<[u8]>) -> &'static str { const TABLE: &[u8; 16] = b"0123456789abcdef"; static mut HEX_BUFFER: [u8; 1024] = [0; 1024]; - + let bytes = bytes.as_ref(); let len = bytes.len(); - + // Safety: we ensure exclusive access and stay within bounds unsafe { let buf = &mut HEX_BUFFER[..(len * 2)]; - + for (i, &byte) in bytes.iter().enumerate() { let idx = i * 2; buf[idx] = TABLE[(byte >> 4) as usize]; buf[idx + 1] = TABLE[(byte & 0xf) as usize]; } - + std::str::from_utf8_unchecked(&buf[..(len * 2)]) } } -pub fn create_hash<'hash>(path: impl AsRef<Path>) -> &'hash str { +pub(crate) fn create_hash<'hash>(path: impl AsRef<Path>) -> &'hash str { let path = path.as_ref(); - + if !path.exists() { warn!("Path does not exist: {}", path.display()); return DEFAULT_HASH; } - + let path = match path.to_str() { Some(path) => path, None => return DEFAULT_HASH, }; - match MerkleTree::builder(path) - .algorithm(Algorithm::Blake3) - .hash_names(false) - .build() - { + match MerkleTree::builder(path).algorithm(Algorithm::Blake3).hash_names(false).build() { Ok(tree) => { let hash = bytes_to_hex(tree.root.item.hash); debug!(path, "Successfully created tree hash"); diff --git a/maid/client/task/mod.rs b/maid/client/task/mod.rs index 65e9fd1dfebc8b34be99a25902eb78701126ef1a..025331e9f13ed6e61409cea7f09c4439435e3700 100644 --- a/maid/client/task/mod.rs +++ b/maid/client/task/mod.rs @@ -1,2 +1,2 @@ -pub mod cache; -pub mod progress; +pub(crate) mod cache; +pub(crate) mod progress; diff --git a/maid/client/task/progress.rs b/maid/client/task/progress.rs index 94847f86f7f71951bd683fd1acb5874fd3bf1037..44ee464d31e9cf982700e6f65a78b55f33b311d1 100644 --- a/maid/client/task/progress.rs +++ b/maid/client/task/progress.rs @@ -1,7 +1,7 @@ use indicatif::{ProgressBar, ProgressStyle}; use macros_rs::fmt::fmtstr; -pub fn init(ticks: Vec<&str>, template: &str, tick: u64) -> ProgressBar { +pub(crate) fn init(ticks: Vec<&str>, template: &str, tick: u64) -> ProgressBar { let pb = ProgressBar::new_spinner(); let tick_str: Vec<&str> = ticks.into_iter().map(|item| fmtstr!("{item} ")).collect();