From f59c4c1f9e25f88c585ac67835b2212de0df9022 Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:05:26 +0900 Subject: [PATCH] :construction: Enhance anyhow --- cli/src/command.rs | 6 +++--- cli/src/command/commons.rs | 14 ++++++++------ cli/src/command/create.rs | 22 ++++++++++------------ cli/src/command/extract.rs | 6 ++---- cli/src/command/list.rs | 2 +- cli/src/command/stdio.rs | 16 +++++----------- cli/src/command/update.rs | 6 +++--- 7 files changed, 32 insertions(+), 40 deletions(-) diff --git a/cli/src/command.rs b/cli/src/command.rs index e04c4171..a4f3c087 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -18,7 +18,7 @@ pub mod update; mod xattr; use crate::cli::{CipherAlgorithmArgs, Cli, Commands, PasswordArgs}; -use std::{fs, io}; +use std::fs; pub fn entry(cli: Cli) -> anyhow::Result<()> { match cli.commands { @@ -34,7 +34,7 @@ pub fn entry(cli: Cli) -> anyhow::Result<()> { } } -fn ask_password(args: PasswordArgs) -> io::Result> { +fn ask_password(args: PasswordArgs) -> anyhow::Result> { if let Some(path) = args.password_file { return Ok(Some(fs::read_to_string(path)?)); }; @@ -43,7 +43,7 @@ fn ask_password(args: PasswordArgs) -> io::Result> { log::warn!("Using a password on the command line interface can be insecure."); password } - Some(None) => Some(gix_prompt::securely("Enter password: ").map_err(io::Error::other)?), + Some(None) => Some(gix_prompt::securely("Enter password: ")?), None => None, }) } diff --git a/cli/src/command/commons.rs b/cli/src/command/commons.rs index d9cfe227..4aa4cd07 100644 --- a/cli/src/command/commons.rs +++ b/cli/src/command/commons.rs @@ -2,6 +2,7 @@ use crate::{ cli::{CipherAlgorithmArgs, CompressionAlgorithmArgs, HashAlgorithmArgs}, utils::{self, PathPartExt}, }; +use anyhow::Context; use normalize_path::*; use pna::{ prelude::*, Archive, EntryBuilder, EntryName, EntryPart, EntryReference, NormalEntry, @@ -71,7 +72,7 @@ pub(crate) fn collect_items, P: Into>( gitignore: bool, follow_links: bool, exclude: Option>, -) -> io::Result> { +) -> anyhow::Result> { let mut files = files.into_iter(); let exclude = exclude.map(|it| it.into_iter().map(|path| path.normalize())); let mut target_items = vec![]; @@ -100,7 +101,7 @@ pub(crate) fn collect_items, P: Into>( } }; for path in walker.into_iter().flatten() { - let path = path.map_err(io::Error::other)?.into_path(); + let path = path?.into_path(); if keep_dir || path.is_file() { target_items.push(path); } @@ -115,14 +116,14 @@ pub(crate) fn create_entry( keep_options, owner_options, }: CreateOptions, -) -> io::Result { +) -> anyhow::Result { if path.is_symlink() { let source = fs::read_link(path)?; let entry = EntryBuilder::new_symbolic_link( EntryName::from_lossy(path), EntryReference::from_lossy(source.as_path()), )?; - return apply_metadata(entry, path, keep_options, owner_options)?.build(); + return Ok(apply_metadata(entry, path, keep_options, owner_options)?.build()?); } else if path.is_file() { let mut entry = EntryBuilder::new_file(EntryName::from_lossy(path), option)?; #[cfg(feature = "memmap")] @@ -140,15 +141,16 @@ pub(crate) fn create_entry( { entry.write_all(&fs::read(path)?)?; } - return apply_metadata(entry, path, keep_options, owner_options)?.build(); + return Ok(apply_metadata(entry, path, keep_options, owner_options)?.build()?); } else if path.is_dir() { let entry = EntryBuilder::new_dir(EntryName::from_lossy(path)); - return apply_metadata(entry, path, keep_options, owner_options)?.build(); + return Ok(apply_metadata(entry, path, keep_options, owner_options)?.build()?); } Err(io::Error::new( io::ErrorKind::Unsupported, "Currently not a regular file is not supported.", )) + .with_context(|| "") } pub(crate) fn entry_option( diff --git a/cli/src/command/create.rs b/cli/src/command/create.rs index 8320b44a..a038a979 100644 --- a/cli/src/command/create.rs +++ b/cli/src/command/create.rs @@ -12,7 +12,6 @@ use crate::{ }, utils::{self, fmt::DurationDisplay}, }; -use anyhow::Context; use bytesize::ByteSize; use clap::{ArgGroup, Parser, ValueHint}; use pna::{Archive, SolidEntryBuilder, WriteOptions}; @@ -120,8 +119,7 @@ fn create_archive(args: CreateCommand) -> anyhow::Result<()> { return Err(io::Error::new( io::ErrorKind::AlreadyExists, format!("{} is already exists", archive.display()), - )) - .with_context(|| ""); + ))?; } log::info!("Create an archive: {}", archive.display()); let mut files = args.file.files; @@ -206,14 +204,12 @@ pub(crate) fn create_archive_file( owner_options: OwnerOptions, solid: bool, target_items: Vec, -) -> io::Result<()> +) -> anyhow::Result<()> where W: Write, F: FnMut() -> io::Result, { - let pool = ThreadPoolBuilder::default() - .build() - .map_err(io::Error::other)?; + let pool = ThreadPoolBuilder::default().build()?; let (tx, rx) = std::sync::mpsc::channel(); let option = if solid { @@ -264,10 +260,8 @@ fn create_archive_with_split( solid: bool, target_items: Vec, max_file_size: usize, -) -> io::Result<()> { - let pool = ThreadPoolBuilder::default() - .build() - .map_err(io::Error::other)?; +) -> anyhow::Result<()> { + let pool = ThreadPoolBuilder::default().build()?; let (tx, rx) = std::sync::mpsc::channel(); let option = if solid { @@ -301,7 +295,11 @@ fn create_archive_with_split( let entries = entries_builder.build(); write_split_archive(archive, [entries].into_iter(), max_file_size)?; } else { - write_split_archive(archive, rx.into_iter(), max_file_size)?; + write_split_archive( + archive, + rx.into_iter().map(|it| it.map_err(io::Error::other)), + max_file_size, + )?; } Ok(()) } diff --git a/cli/src/command/extract.rs b/cli/src/command/extract.rs index 4a817bfc..cd498b9a 100644 --- a/cli/src/command/extract.rs +++ b/cli/src/command/extract.rs @@ -139,7 +139,7 @@ pub(crate) fn run_extract_archive_reader<'p, Provider>( files: Vec, mut password_provider: Provider, args: OutputOption, -) -> io::Result<()> +) -> anyhow::Result<()> where Provider: FnMut() -> Option<&'p str>, { @@ -147,9 +147,7 @@ where let globs = GlobPatterns::new(files).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; - let pool = ThreadPoolBuilder::default() - .build() - .map_err(io::Error::other)?; + let pool = ThreadPoolBuilder::default().build()?; let mut hard_link_entries = Vec::new(); diff --git a/cli/src/command/list.rs b/cli/src/command/list.rs index 24a9d1e9..3104a5af 100644 --- a/cli/src/command/list.rs +++ b/cli/src/command/list.rs @@ -249,7 +249,7 @@ pub(crate) fn run_list_archive( password: Option<&str>, files: &[String], args: ListOptions, -) -> io::Result<()> { +) -> anyhow::Result<()> { let globs = GlobPatterns::new(files).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; diff --git a/cli/src/command/stdio.rs b/cli/src/command/stdio.rs index 70181480..b57c0bec 100644 --- a/cli/src/command/stdio.rs +++ b/cli/src/command/stdio.rs @@ -13,13 +13,8 @@ use crate::{ }, utils, }; -use anyhow::Context; use clap::{ArgGroup, Args, Parser, ValueHint}; -use std::{ - fs, - io::{self, stdout}, - path::PathBuf, -}; +use std::{fs, io, path::PathBuf}; #[derive(Args, Clone, Eq, PartialEq, Hash, Debug)] #[command( @@ -131,10 +126,9 @@ fn run_stdio(args: StdioCommand) -> anyhow::Result<()> { } else { unreachable!() } - .with_context(|| "") } -fn run_create_archive(args: StdioCommand) -> io::Result<()> { +fn run_create_archive(args: StdioCommand) -> anyhow::Result<()> { let password = ask_password(args.password)?; check_password(&password, &args.cipher); let mut files = args @@ -195,7 +189,7 @@ fn run_create_archive(args: StdioCommand) -> io::Result<()> { ) } else { create_archive_file( - || Ok(stdout().lock()), + || Ok(io::stdout().lock()), cli_option, keep_options, owner_options, @@ -205,7 +199,7 @@ fn run_create_archive(args: StdioCommand) -> io::Result<()> { } } -fn run_extract_archive(args: StdioCommand) -> io::Result<()> { +fn run_extract_archive(args: StdioCommand) -> anyhow::Result<()> { let password = ask_password(args.password)?; let out_option = OutputOption { overwrite: args.overwrite, @@ -241,7 +235,7 @@ fn run_extract_archive(args: StdioCommand) -> io::Result<()> { } } -fn run_list_archive(args: StdioCommand) -> io::Result<()> { +fn run_list_archive(args: StdioCommand) -> anyhow::Result<()> { let password = ask_password(args.password)?; let list_options = ListOptions { long: false, diff --git a/cli/src/command/update.rs b/cli/src/command/update.rs index 31acba8d..8f1ad8d3 100644 --- a/cli/src/command/update.rs +++ b/cli/src/command/update.rs @@ -140,7 +140,7 @@ impl Command for UpdateCommand { } } -fn update_archive(args: UpdateCommand) -> io::Result<()> { +fn update_archive(args: UpdateCommand) -> anyhow::Result<()> { let password = ask_password(args.password)?; check_password(&password, &args.cipher); let archive_path = args.file.archive; @@ -148,7 +148,7 @@ fn update_archive(args: UpdateCommand) -> io::Resul return Err(io::Error::new( io::ErrorKind::NotFound, format!("{} is not exists", archive_path.display()), - )); + ))?; } let option = entry_option(args.compression, args.cipher, args.hash, password.clone()); let keep_options = KeepOptions { @@ -287,7 +287,7 @@ fn update_archive(args: UpdateCommand) -> io::Resul Strategy::transform( &mut out_archive, password.as_deref(), - entry.map(Into::into), + entry.map(Into::into).map_err(io::Error::other), |entry| entry.map(Some), )?; }