mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 03:55:28 +00:00
Misc cleanup
This commit is contained in:
parent
eabaac0145
commit
c4a4792816
17
src/cli.rs
17
src/cli.rs
@ -7,7 +7,7 @@ use std::{
|
|||||||
|
|
||||||
use strsim::normalized_damerau_levenshtein;
|
use strsim::normalized_damerau_levenshtein;
|
||||||
|
|
||||||
use crate::{arg_flag, flag, oof};
|
use crate::{arg_flag, flag, oof, Error};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
@ -27,8 +27,8 @@ pub enum Command {
|
|||||||
|
|
||||||
/// Calls parse_args_and_flags_from using argv (std::env::args_os)
|
/// Calls parse_args_and_flags_from using argv (std::env::args_os)
|
||||||
///
|
///
|
||||||
/// This function is also responsible for treating and checking the cli input
|
/// This function is also responsible for treating and checking the command-line input,
|
||||||
/// Like calling canonicale, checking if it exists.
|
/// such as calling [`canonicalize`](std::fs::canonicalize), checking if it the given files exists, etc.
|
||||||
pub fn parse_args() -> crate::Result<ParsedArgs> {
|
pub fn parse_args() -> crate::Result<ParsedArgs> {
|
||||||
// From argv, but ignoring empty arguments
|
// From argv, but ignoring empty arguments
|
||||||
let args = env::args_os().skip(1).filter(|arg| !arg.is_empty()).collect();
|
let args = env::args_os().skip(1).filter(|arg| !arg.is_empty()).collect();
|
||||||
@ -55,8 +55,7 @@ pub struct ParsedArgs {
|
|||||||
pub flags: oof::Flags,
|
pub flags: oof::Flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// check_for_typo checks if the first argument is
|
/// Checks if the first argument is a typo for the `compress` subcommand.
|
||||||
/// a typo for the compress subcommand.
|
|
||||||
/// Returns true if the arg is probably a typo or false otherwise.
|
/// Returns true if the arg is probably a typo or false otherwise.
|
||||||
fn is_typo(path: impl AsRef<Path>) -> bool {
|
fn is_typo(path: impl AsRef<Path>) -> bool {
|
||||||
if path.as_ref().exists() {
|
if path.as_ref().exists() {
|
||||||
@ -74,7 +73,7 @@ fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
|
|||||||
Ok(abs_path) => Ok(abs_path),
|
Ok(abs_path) => Ok(abs_path),
|
||||||
Err(io_err) => {
|
Err(io_err) => {
|
||||||
if !path.as_ref().exists() {
|
if !path.as_ref().exists() {
|
||||||
Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref())))
|
Err(Error::FileNotFound(path.as_ref().into()))
|
||||||
} else {
|
} else {
|
||||||
Err(io_err.into())
|
Err(io_err.into())
|
||||||
}
|
}
|
||||||
@ -105,7 +104,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
|
|||||||
let mut files: Vec<PathBuf> = args.into_iter().map(PathBuf::from).collect();
|
let mut files: Vec<PathBuf> = args.into_iter().map(PathBuf::from).collect();
|
||||||
|
|
||||||
if files.len() < 2 {
|
if files.len() < 2 {
|
||||||
return Err(crate::Error::MissingArgumentsForCompression);
|
return Err(Error::MissingArgumentsForCompression);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safety: we checked that args.len() >= 2
|
// Safety: we checked that args.len() >= 2
|
||||||
@ -120,7 +119,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
|
|||||||
|
|
||||||
if let Some(first_arg) = args.first() {
|
if let Some(first_arg) = args.first() {
|
||||||
if is_typo(first_arg) {
|
if is_typo(first_arg) {
|
||||||
return Err(crate::Error::CompressionTypo);
|
return Err(Error::CompressionTypo);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
todo!("Complain that no decompression arguments were given.");
|
todo!("Complain that no decompression arguments were given.");
|
||||||
@ -171,7 +170,7 @@ mod tests {
|
|||||||
test_cli("compress foo bar baz.zip").unwrap().command,
|
test_cli("compress foo bar baz.zip").unwrap().command,
|
||||||
Command::Compress { files: vec!["foo".into(), "bar".into()], output_path: "baz.zip".into() }
|
Command::Compress { files: vec!["foo".into(), "bar".into()], output_path: "baz.zip".into() }
|
||||||
);
|
);
|
||||||
assert_eq!(test_cli("compress").unwrap_err(), crate::Error::MissingArgumentsForCompression);
|
assert_eq!(test_cli("compress").unwrap_err(), Error::MissingArgumentsForCompression);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -2,13 +2,12 @@ use std::io::{self, Write};
|
|||||||
|
|
||||||
use crate::utils::colors;
|
use crate::utils::colors;
|
||||||
|
|
||||||
|
/// Represents a confirmation dialog
|
||||||
pub struct Confirmation<'a> {
|
pub struct Confirmation<'a> {
|
||||||
pub prompt: &'a str,
|
pub prompt: &'a str,
|
||||||
pub placeholder: Option<&'a str>,
|
pub placeholder: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Error;
|
|
||||||
|
|
||||||
impl<'a> Confirmation<'a> {
|
impl<'a> Confirmation<'a> {
|
||||||
pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
|
pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
|
||||||
|
@ -19,11 +19,7 @@ pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
|
|||||||
pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result<PathBuf> {
|
pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result<PathBuf> {
|
||||||
let previous_location = env::current_dir()?;
|
let previous_location = env::current_dir()?;
|
||||||
|
|
||||||
let parent = if let Some(parent) = filename.parent() {
|
let parent = filename.parent().ok_or(crate::Error::CompressingRootFolder)?;
|
||||||
parent
|
|
||||||
} else {
|
|
||||||
return Err(crate::Error::CompressingRootFolder);
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: fix this error variant, as it is not the only possible error that can
|
// TODO: fix this error variant, as it is not the only possible error that can
|
||||||
// come out of this operation
|
// come out of this operation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user