Use CreateFlags to parse flags for archive creation

This commit is contained in:
Nathan Fisher 2024-12-15 02:01:32 -05:00
parent 5420684061
commit dd8daa26ed

View File

@ -33,16 +33,17 @@ struct ListingFlags {
} }
impl From<&ArgMatches> for ListingFlags { impl From<&ArgMatches> for ListingFlags {
fn from(value: &ArgMatches) -> Self { fn from(matches: &ArgMatches) -> Self {
Self { Self {
files: value.get_flag("files"), files: matches.get_flag("files"),
color: value.get_flag("color"), color: matches.get_flag("color"),
long: value.get_flag("long"), long: matches.get_flag("long"),
human: value.get_flag("human"), human: matches.get_flag("human"),
} }
} }
} }
#[derive(PartialEq, Eq)]
enum Verbosity { enum Verbosity {
Quiet, Quiet,
Normal, Normal,
@ -50,7 +51,7 @@ enum Verbosity {
} }
impl From<&ArgMatches> for Verbosity { impl From<&ArgMatches> for Verbosity {
fn from(value: &ArgMatches) -> Self { fn from(matches: &ArgMatches) -> Self {
if matches.get_flag("quiet") { if matches.get_flag("quiet") {
Self::Quiet Self::Quiet
} else if matches.get_flag("verbose") && !matches.get_flag("stdout") { } else if matches.get_flag("verbose") && !matches.get_flag("stdout") {
@ -70,9 +71,22 @@ struct CreateFlags {
gid: Option<u32>, gid: Option<u32>,
} }
impl From<&ArgMatches> for CreateFlags { impl TryFrom<&ArgMatches> for CreateFlags {
fn from(value: &ArgMatches) -> Self { type Error = haggis::Error;
todo!()
fn try_from(matches: &ArgMatches) -> Result<Self, Self::Error> {
Ok(Self {
algorithm: matches.get_one::<String>("algorithm").unwrap().parse()?,
zstd_compression: if matches.get_flag("zstd") {
Some(matches.get_one::<i32>("level").copied().unwrap_or(3))
} else {
None
},
verbosity: Verbosity::from(matches),
stdout: matches.get_flag("stdout"),
uid: matches.get_one::<u32>("uid").copied(),
gid: matches.get_one::<u32>("gid").copied(),
})
} }
} }
@ -108,10 +122,7 @@ fn main() {
#[allow(clippy::similar_names)] #[allow(clippy::similar_names)]
fn create(matches: &ArgMatches) -> Result<(), haggis::Error> { fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
let verbose = !matches.get_flag("stdout") || matches.get_flag("quiet"); let flags = CreateFlags::try_from(matches)?;
let algorithm: Algorithm = matches.get_one::<String>("algorithm").unwrap().parse()?;
let uid = matches.get_one::<u32>("uid").copied();
let gid = matches.get_one::<u32>("gid").copied();
let mut files = vec![]; let mut files = vec![];
if let Some(f) = matches.get_many::<String>("files") { if let Some(f) = matches.get_many::<String>("files") {
for f in f { for f in f {
@ -136,7 +147,7 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
let len = files.len(); let len = files.len();
let mut handle = None; let mut handle = None;
if verbose { if flags.verbosity == Verbosity::Verbose {
let pb = ProgressBar::new(len as u64); let pb = ProgressBar::new(len as u64);
pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap()); pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap());
pb.set_prefix("Adding files"); pb.set_prefix("Adding files");
@ -172,34 +183,33 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
} }
})); }));
} }
if matches.get_flag("zstd") { if let Some(level) = flags.zstd_compression {
let level = matches.get_one::<i32>("level").copied().unwrap_or(3); if flags.stdout {
if matches.get_flag("stdout") {
let stdout = io::stdout(); let stdout = io::stdout();
let mut writer = Encoder::new(stdout, level)?; let mut writer = Encoder::new(stdout, level)?;
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?; haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
let _fd = writer.finish(); let _fd = writer.finish();
} else if let Some(o) = output { } else if let Some(o) = output {
let fd = File::create(o)?; let fd = File::create(o)?;
let mut writer = Encoder::new(fd, level)?; let mut writer = Encoder::new(fd, level)?;
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?; haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
let _fd = writer.finish()?; let _fd = writer.finish()?;
} else { } else {
unreachable!(); unreachable!();
} }
} else if matches.get_flag("stdout") { } else if flags.stdout {
let stdout = io::stdout(); let stdout = io::stdout();
let mut writer = BufWriter::new(stdout); let mut writer = BufWriter::new(stdout);
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?; haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
} else if let Some(o) = output { } else if let Some(o) = output {
haggis::par_create_archive(o, &files, algorithm, &sender, uid, gid)?; haggis::par_create_archive(o, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
} else { } else {
unreachable!(); unreachable!();
} }
if let Some(handle) = handle { if let Some(handle) = handle {
match handle.join() { match handle.join() {
Ok(()) => { Ok(()) => {
if verbose { if flags.verbosity == Verbosity::Verbose {
println!("Archive created successfully"); println!("Archive created successfully");
} }
Ok(()) Ok(())