mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Simplify cli canonicalize implementation
This commit is contained in:
parent
d2d4a929e1
commit
df3bb3b72b
22
src/cli.rs
22
src/cli.rs
@ -1,6 +1,7 @@
|
|||||||
//! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
|
//! CLI related functions, uses the clap argparsing definitions from `opts.rs`.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
io,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
@ -8,7 +9,7 @@ use std::{
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{Error, Opts, QuestionPolicy, Subcommand};
|
use crate::{Opts, QuestionPolicy, Subcommand};
|
||||||
|
|
||||||
impl Opts {
|
impl Opts {
|
||||||
/// A helper method that calls `clap::Parser::parse`.
|
/// A helper method that calls `clap::Parser::parse`.
|
||||||
@ -17,7 +18,7 @@ impl Opts {
|
|||||||
/// 1. Make paths absolute.
|
/// 1. Make paths absolute.
|
||||||
/// 2. Checks the QuestionPolicy.
|
/// 2. Checks the QuestionPolicy.
|
||||||
pub fn parse_args() -> crate::Result<(Self, 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;
|
let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. }) = &mut opts.cmd;
|
||||||
*files = canonicalize_files(files)?;
|
*files = canonicalize_files(files)?;
|
||||||
@ -34,19 +35,6 @@ impl Opts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
|
fn canonicalize_files(files: &[impl AsRef<Path>]) -> io::Result<Vec<PathBuf>> {
|
||||||
match fs::canonicalize(&path.as_ref()) {
|
files.iter().map(fs::canonicalize).collect()
|
||||||
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<Path>]) -> crate::Result<Vec<PathBuf>> {
|
|
||||||
files.iter().map(canonicalize).collect()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user