From 480a9f72fba7e1acbf3c63f1d1d9595e6afb44e3 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Wed, 13 Aug 2025 00:15:39 -0600 Subject: [PATCH] further cleanup of libs and bins --- Cargo.toml | 7 +++++++ Justfile | 4 ++++ src/bin/picca.rs | 26 +++-------------------- src/bin/sha256sum.rs | 19 +---------------- src/core.rs | 5 +++-- src/core/macros.rs | 50 ++++++++++++++++++++++++++++++-------------- 6 files changed, 52 insertions(+), 59 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1f0100d..e3e9788 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,17 @@ edition = "2024" autobins = true [features] +default = [] algorithms = [] +[[bin]] +name = "sha256sum" +path = "src/bin/sha256sum.rs" +required-features = [] + [[bin]] name = "picca" +path = "src/bin/picca.rs" required-features = ["algorithms"] [dependencies] diff --git a/Justfile b/Justfile index 7f955e6..8e0192f 100644 --- a/Justfile +++ b/Justfile @@ -38,6 +38,10 @@ push-docker-latest: build-docker push-docker docker push forge.steck.dev/bryson/picca:latest docker image rm forge.steck.dev/bryson/picca:latest +# generate algorithm bin into Cargo.toml +generate-bin: + + doc: @mkdir -p target sed s/\/{{version}}/g man/picca.1.md | pandoc --standalone -t man > target/picca.1 diff --git a/src/bin/picca.rs b/src/bin/picca.rs index e755837..77f6e4b 100644 --- a/src/bin/picca.rs +++ b/src/bin/picca.rs @@ -1,26 +1,6 @@ -use clap::Parser; -use std::collections::{HashMap, VecDeque}; -use std::fs::{self, File, read_to_string}; -use std::io::{self, Read}; -use std::path::PathBuf; -use std::sync::atomic::{AtomicI32, Ordering}; -use std::sync::{Arc, Mutex}; -use std::thread::{self, JoinHandle, available_parallelism}; - -use picca::core::hashers; -use picca::{get_algorithms, message}; - #[quit::main] fn main() { - let args = Args::parse(); - let algorithm = args.algorithm; - let docker = match option_env!("PICCA_DOCKER") { - Some(v) => match v { - "true" => true, - _ => false, - }, - None => false, - }; - - picca::main!(args, algorithm, docker); + let args = picca::Args::parse(); + let algorithm = env!("CARGO_BIN_NAME").replace("sum", ""); + picca::main!(args, algorithm, false); } diff --git a/src/bin/sha256sum.rs b/src/bin/sha256sum.rs index d1505d8..c216c39 100644 --- a/src/bin/sha256sum.rs +++ b/src/bin/sha256sum.rs @@ -1,23 +1,6 @@ -use clap::Parser; -use std::collections::VecDeque; -use std::fs::{self}; -use std::path::PathBuf; -use std::sync::atomic::Ordering; -use std::thread::available_parallelism; - -use picca::message; - #[quit::main] fn main() { let args = picca::Args::parse(); let algorithm = env!("CARGO_BIN_NAME").replace("sum", ""); - let docker = match option_env!("PICCA_DOCKER") { - Some(v) => match v { - "true" => true, - _ => false, - }, - None => false, - }; - - picca::main!(args, algorithm, docker); + picca::main!(args, algorithm, true); } diff --git a/src/core.rs b/src/core.rs index 2bf2f13..952294c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,10 +1,11 @@ use std::{ collections::{HashMap, VecDeque}, - fs::{read_to_string, File}, + fs::{File, read_to_string}, io::{self, Read}, path::PathBuf, sync::{ - atomic::{AtomicI32, Ordering}, Arc, Mutex + Arc, Mutex, + atomic::{AtomicI32, Ordering}, }, thread::{self, JoinHandle}, }; diff --git a/src/core/macros.rs b/src/core/macros.rs index 8f57105..b73bf81 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -1,16 +1,34 @@ + #[macro_export] macro_rules! main { - ($a:expr,$b:expr,$c:expr) => { - let cpus = match $a.threads { - 0 => available_parallelism().unwrap().get(), - _ => $a.threads, + ($args: expr, $algorithm: expr, $independent: expr) => { + use clap::Parser; + use std::collections::VecDeque; + use std::fs::{self}; + use std::path::PathBuf; + use std::sync::atomic::Ordering; + use std::thread::available_parallelism; + + use picca::message; + + let docker = match option_env!("PICCA_DOCKER") { + Some(v) => match v { + "true" => true, + _ => false, + }, + None => false, }; - if $a.debug { + let cpus = match $args.threads { + 0 => available_parallelism().unwrap().get(), + _ => $args.threads, + }; + + if $args.debug { if env!("CARGO_BIN_NAME") != "picca" { message::debug(format!( "Starting picca using algorithm {} with a max of {} threads", - $b, cpus + $algorithm, cpus )); } else { message::debug(format!( @@ -20,26 +38,26 @@ macro_rules! main { )); } - if $c { + if docker { message::debug(format!("Docker is detected")); } } - if picca::UNSECURE_ALGORITHMS.contains(&$b.as_str()) { - message::warning(format!("{} is an unsecure hashing algorithm!", &$b)); + if picca::UNSECURE_ALGORITHMS.contains(&$algorithm.as_str()) { + message::warning(format!("{} is an unsecure hashing algorithm!", &$algorithm)); } let handles; let arc_fe; let arc_he; - let check_mode = !$a.check.is_empty(); - if &$a.check.len() >= &1 { - (handles, arc_fe, arc_he) = picca::core::verify(cpus, $b, $a.debug, $a.quiet, $a.check); + let check_mode = !$args.check.is_empty(); + if &$args.check.len() >= &1 { + (handles, arc_fe, arc_he) = picca::core::verify(cpus, $algorithm, $args.debug, $args.quiet, $args.check); } else { let mut buffer = VecDeque::new(); - if &$a.files.len() >= &1 { - for file in $a.files { - if $a.canonicalize { + if &$args.files.len() >= &1 { + for file in $args.files { + if $args.canonicalize { match fs::canonicalize(file.as_path()) { Ok(p) => buffer.push_back(p), Err(e) => panic!("unable to canonicalize {}: {}", file.as_path().display(), e), @@ -53,7 +71,7 @@ macro_rules! main { buffer.push_back(PathBuf::from("-")); } - (handles, arc_fe, arc_he) = picca::core::generate(cpus, buffer, $b, $a.debug, $a.quiet); + (handles, arc_fe, arc_he) = picca::core::generate(cpus, buffer, $algorithm, $args.debug, $args.quiet); } for handle in handles {