From df3bb3b72b009482d3ad5b330dd502be90f25a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Bezerra?= Date: Tue, 2 Nov 2021 07:21:03 -0300 Subject: [PATCH] Simplify cli canonicalize implementation --- src/cli.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 17c6c71..6d7b4a9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,7 @@ //! CLI related functions, uses the clap argparsing definitions from `opts.rs`. use std::{ + io, path::{Path, PathBuf}, vec::Vec, }; @@ -8,7 +9,7 @@ use std::{ use clap::Parser; use fs_err as fs; -use crate::{Error, Opts, QuestionPolicy, Subcommand}; +use crate::{Opts, QuestionPolicy, Subcommand}; impl Opts { /// A helper method that calls `clap::Parser::parse`. @@ -17,7 +18,7 @@ impl Opts { /// 1. Make paths absolute. /// 2. Checks the QuestionPolicy. pub fn parse_args() -> crate::Result<(Self, QuestionPolicy)> { - let mut opts: Self = Self::parse(); + let mut opts = Self::parse(); let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. }) = &mut opts.cmd; *files = canonicalize_files(files)?; @@ -34,19 +35,6 @@ impl Opts { } } -fn canonicalize(path: impl AsRef) -> crate::Result { - match fs::canonicalize(&path.as_ref()) { - Ok(abs_path) => Ok(abs_path), - Err(io_err) => { - if !path.as_ref().exists() { - Err(Error::FileNotFound(path.as_ref().into())) - } else { - Err(io_err.into()) - } - } - } -} - -fn canonicalize_files(files: &[impl AsRef]) -> crate::Result> { - files.iter().map(canonicalize).collect() +fn canonicalize_files(files: &[impl AsRef]) -> io::Result> { + files.iter().map(fs::canonicalize).collect() }