diff --git a/oof/src/error.rs b/oof/src/error.rs index 86ead8c..306f176 100644 --- a/oof/src/error.rs +++ b/oof/src/error.rs @@ -16,7 +16,7 @@ pub enum OofError<'t> { UnknownLongFlag(String), MisplacedShortArgFlagError(char), MissingValueToFlag(&'t Flag), - DuplicatedFlag(&'t Flag) + DuplicatedFlag(&'t Flag), } impl<'t> error::Error for OofError<'t> { diff --git a/oof/src/flags.rs b/oof/src/flags.rs index 043896e..b70af33 100644 --- a/oof/src/flags.rs +++ b/oof/src/flags.rs @@ -13,11 +13,7 @@ pub struct ArgFlag; impl ArgFlag { pub fn long(name: &'static str) -> Flag { - Flag { - long: name, - short: None, - takes_value: true, - } + Flag { long: name, short: None, takes_value: true } } } @@ -40,11 +36,7 @@ impl std::fmt::Display for Flag { impl Flag { pub fn long(name: &'static str) -> Self { - Self { - long: name, - short: None, - takes_value: false, - } + Self { long: name, short: None, takes_value: false } } pub fn short(mut self, short_flag_char: char) -> Self { diff --git a/oof/src/lib.rs b/oof/src/lib.rs index 87445bd..6621867 100644 --- a/oof/src/lib.rs +++ b/oof/src/lib.rs @@ -7,7 +7,10 @@ mod error; mod flags; pub mod util; -use std::{collections::BTreeMap, ffi::{OsStr, OsString}}; +use std::{ + collections::BTreeMap, + ffi::{OsStr, OsString}, +}; pub use error::OofError; pub use flags::{ArgFlag, Flag, FlagType, Flags}; @@ -103,9 +106,9 @@ pub fn filter_flags( } // If it is a flag, now we try to interpret it as valid utf-8 - let flag= match arg.to_str() { + let flag = match arg.to_str() { Some(arg) => arg, - None => return Err(OofError::InvalidUnicode(arg)) + None => return Err(OofError::InvalidUnicode(arg)), }; // Only one hyphen in the flag @@ -127,12 +130,11 @@ pub fn filter_flags( // Safety: this loop only runs when len >= 1, so this subtraction is safe let is_last_letter = i == letters.len() - 1; - let flag_info = short_flags_info.get(&letter).ok_or( - OofError::UnknownShortFlag(letter) - )?; + let flag_info = + short_flags_info.get(&letter).ok_or(OofError::UnknownShortFlag(letter))?; if !is_last_letter && flag_info.takes_value { - return Err(OofError::MisplacedShortArgFlagError(letter)) + return Err(OofError::MisplacedShortArgFlagError(letter)); // Because "-AB argument" only works if B takes values, not A. // That is, the short flag that takes values needs to come at the end // of this piece of text @@ -147,9 +149,8 @@ pub fn filter_flags( } // pop the next one - let flag_argument = iter.next().ok_or( - OofError::MissingValueToFlag(flag_info) - )?; + let flag_argument = + iter.next().ok_or(OofError::MissingValueToFlag(flag_info))?; // Otherwise, insert it. result_flags.argument_flags.insert(flag_name, flag_argument); @@ -167,9 +168,9 @@ pub fn filter_flags( if let FlagType::Long = flag_type { let flag = trim_double_hyphen(flag); - let flag_info = long_flags_info.get(flag).ok_or_else(|| { - OofError::UnknownLongFlag(String::from(flag)) - })?; + let flag_info = long_flags_info + .get(flag) + .ok_or_else(|| OofError::UnknownLongFlag(String::from(flag)))?; let flag_name = flag_info.long; @@ -179,9 +180,7 @@ pub fn filter_flags( return Err(OofError::DuplicatedFlag(flag_info)); } - let flag_argument = iter.next().ok_or( - OofError::MissingValueToFlag(flag_info) - )?; + let flag_argument = iter.next().ok_or(OofError::MissingValueToFlag(flag_info))?; result_flags.argument_flags.insert(flag_name, flag_argument); } else { // If it was already inserted @@ -209,9 +208,7 @@ where T: AsRef, U: AsRef, { - texts - .iter() - .any(|text| args.iter().any(|arg| arg.as_ref() == text.as_ref())) + texts.iter().any(|text| args.iter().any(|arg| arg.as_ref() == text.as_ref())) } #[cfg(test)] @@ -237,14 +234,8 @@ mod tests { assert_eq!(args, gen_args("ouch a.zip b.tar.gz c.tar")); assert!(flags.is_present("output_file")); - assert_eq!( - Some(&OsString::from("new_folder")), - flags.arg("output_file") - ); - assert_eq!( - Some(OsString::from("new_folder")), - flags.take_arg("output_file") - ); + assert_eq!(Some(&OsString::from("new_folder")), flags.arg("output_file")); + assert_eq!(Some(OsString::from("new_folder")), flags.take_arg("output_file")); assert!(!flags.is_present("output_file")); } @@ -291,10 +282,7 @@ mod tests { // TODO: remove should_panic and use proper error handling inside of filter_args #[should_panic] fn test_flag_info_with_long_flag_conflict() { - let flags_info = [ - ArgFlag::long("verbose").short('a'), - Flag::long("verbose").short('b'), - ]; + let flags_info = [ArgFlag::long("verbose").short('a'), Flag::long("verbose").short('b')]; // Should panic here let result = filter_flags(vec![], &flags_info); @@ -305,10 +293,8 @@ mod tests { // TODO: remove should_panic and use proper error handling inside of filter_args #[should_panic] fn test_flag_info_with_short_flag_conflict() { - let flags_info = [ - ArgFlag::long("output_file").short('o'), - Flag::long("verbose").short('o'), - ]; + let flags_info = + [ArgFlag::long("output_file").short('o'), Flag::long("verbose").short('o')]; // Should panic here filter_flags(vec![], &flags_info).unwrap_err(); diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..213e7f3 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,13 @@ +# Normal features +max_width = 100 +imports_granularity = "Crate" +match_block_trailing_comma = true +overflow_delimited_expr = true +reorder_impl_items = true +use_field_init_shorthand = true +newline_style = "Unix" +edition = "2018" +reorder_imports = true +reorder_modules = true +use_try_shorthand = true +use_small_heuristics = "Max" diff --git a/src/cli.rs b/src/cli.rs index 886209a..b79d588 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -64,7 +64,7 @@ where } else { Err(crate::Error::IoError(io_err)) } - } + }, } } @@ -77,17 +77,11 @@ where pub fn parse_args_from(mut args: Vec) -> crate::Result { if oof::matches_any_arg(&args, &["--help", "-h"]) || args.is_empty() { - return Ok(ParsedArgs { - command: Command::ShowHelp, - flags: oof::Flags::default(), - }); + return Ok(ParsedArgs { command: Command::ShowHelp, flags: oof::Flags::default() }); } if oof::matches_any_arg(&args, &["--version"]) { - return Ok(ParsedArgs { - command: Command::ShowVersion, - flags: oof::Flags::default(), - }); + return Ok(ParsedArgs { command: Command::ShowVersion, flags: oof::Flags::default() }); } let subcommands = &["c", "compress"]; @@ -109,12 +103,9 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { let files = canonicalize_files(files)?; - let command = Command::Compress { - files, - compressed_output_path, - }; + let command = Command::Compress { files, compressed_output_path }; ParsedArgs { command, flags } - } + }, // Defaults to decompression when there is no subcommand None => { flags_info.push(arg_flag!('o', "output")); @@ -130,21 +121,15 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { // Parse flags let (args, mut flags) = oof::filter_flags(args, &flags_info)?; - let files = args - .into_iter() - .map(canonicalize) - .collect::, _>>()?; + let files = args.into_iter().map(canonicalize).collect::, _>>()?; let output_folder = flags.take_arg("output").map(PathBuf::from); // TODO: ensure all files are decompressible - let command = Command::Decompress { - files, - output_folder, - }; + let command = Command::Decompress { files, output_folder }; ParsedArgs { command, flags } - } + }, _ => unreachable!("You should match each subcommand passed."), }; diff --git a/src/commands.rs b/src/commands.rs index 10ef89c..ee36c60 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -24,20 +24,16 @@ use crate::{ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> { match command { - Command::Compress { - files, - compressed_output_path, - } => compress_files(files, &compressed_output_path, flags)?, - Command::Decompress { - files, - output_folder, - } => { + Command::Compress { files, compressed_output_path } => { + compress_files(files, &compressed_output_path, flags)? + }, + Command::Decompress { files, output_folder } => { // From Option to Option<&Path> let output_folder = output_folder.as_ref().map(|path| Path::new(path)); for file in files.iter() { decompress_file(file, output_folder, flags)?; } - } + }, Command::ShowHelp => crate::help_command(), Command::ShowVersion => crate::version_command(), } @@ -53,7 +49,7 @@ fn get_compressor(file: &File) -> crate::Result<(Option, BoxedC None => { // This is reached when the output file given does not have an extension or has an unsupported one return Err(crate::Error::MissingExtensionError(file.path.to_path_buf())); - } + }, }; // Supported first compressors: @@ -93,7 +89,7 @@ fn get_decompressor(file: &File) -> crate::Result<(Option, Bo colors::reset() ); return Err(crate::Error::InvalidInput); - } + }, }; let second_decompressor: Box = match extension.second_ext { @@ -126,10 +122,7 @@ fn decompress_file_in_memory( ) -> crate::Result<()> { let output_file_path = utils::get_destination_path(&output_file); - let file_name = file_path - .file_stem() - .map(Path::new) - .unwrap_or(output_file_path); + let file_name = file_path.file_stem().map(Path::new).unwrap_or(output_file_path); if "." == file_name.as_os_str() { // I believe this is only possible when the supplied input has a name @@ -156,14 +149,10 @@ fn decompress_file_in_memory( let mut f = fs::File::create(output_file_path.join(file_name))?; f.write_all(&bytes)?; return Ok(()); - } + }, }; - let file = File { - path: file_name, - contents_in_memory: Some(bytes), - extension, - }; + let file = File { path: file_name, contents_in_memory: Some(bytes), extension }; let decompression_result = decompressor.decompress(file, &output_file, flags)?; if let DecompressionResult::FileInMemory(_) = decompression_result { @@ -196,11 +185,11 @@ fn compress_files( output.contents_in_memory = Some(bytes); entry = Entry::InMemory(output); second_compressor.compress(entry)? - } + }, None => { let entry = Entry::Files(files); second_compressor.compress(entry)? - } + }, }; println!( @@ -224,9 +213,7 @@ fn decompress_file( let file = File::from(file_path)?; // The file must have a supported decompressible format if file.extension == None { - return Err(crate::Error::MissingExtensionError(PathBuf::from( - file_path, - ))); + return Err(crate::Error::MissingExtensionError(PathBuf::from(file_path))); } let output = match output { @@ -251,7 +238,7 @@ fn decompress_file( extension, flags, )?; - } + }, DecompressionResult::FilesUnpacked(_files) => { // If the file's last extension was an archival method, // such as .tar, .zip or (to-do) .rar, then we won't look for @@ -260,7 +247,7 @@ fn decompress_file( // to worry about, at least at the moment. // TODO: use the `files` variable for something - } + }, } Ok(()) diff --git a/src/compressors/bzip.rs b/src/compressors/bzip.rs index 102de37..66a2ca4 100644 --- a/src/compressors/bzip.rs +++ b/src/compressors/bzip.rs @@ -34,7 +34,7 @@ impl BzipCompressor { Some(bytes) => bytes, None => { return Err(crate::Error::InternalError); - } + }, }; Self::compress_bytes(&*bytes) diff --git a/src/compressors/gzip.rs b/src/compressors/gzip.rs index 8e16970..398ed58 100644 --- a/src/compressors/gzip.rs +++ b/src/compressors/gzip.rs @@ -38,7 +38,7 @@ impl GzipCompressor { Some(bytes) => bytes, None => { unreachable!(); - } + }, }; Self::compress_bytes(file_contents) diff --git a/src/compressors/lzma.rs b/src/compressors/lzma.rs index e8f1855..45d36b2 100644 --- a/src/compressors/lzma.rs +++ b/src/compressors/lzma.rs @@ -38,7 +38,7 @@ impl LzmaCompressor { Some(bytes) => bytes, None => { unreachable!(); - } + }, }; Self::compress_bytes(file_contents) diff --git a/src/compressors/zip.rs b/src/compressors/zip.rs index 1d814d5..cb5a717 100644 --- a/src/compressors/zip.rs +++ b/src/compressors/zip.rs @@ -37,7 +37,7 @@ impl ZipCompressor { // TODO: error description, although this block should not be // reachable return Err(crate::Error::InvalidInput); - } + }, }; writer.write_all(&*input_bytes)?; diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 4d91358..fe2cff7 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -4,7 +4,6 @@ use std::{ path::{Path, PathBuf}, }; - use tar::{self, Archive}; use utils::colors; @@ -30,7 +29,7 @@ impl TarDecompressor { None => { let file = fs::File::open(&from.path)?; tar::Archive::new(Box::new(file)) - } + }, }; for file in archive.entries()? { diff --git a/src/decompressors/to_memory.rs b/src/decompressors/to_memory.rs index d7aaf63..50105a5 100644 --- a/src/decompressors/to_memory.rs +++ b/src/decompressors/to_memory.rs @@ -3,7 +3,6 @@ use std::{ path::Path, }; - use utils::colors; use super::decompressor::{DecompressionResult, Decompressor}; diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index 501f25d..d31115c 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -66,7 +66,7 @@ impl ZipDecompressor { _is_dir @ true => { println!("File {} extracted to \"{}\"", idx, file_path.display()); fs::create_dir_all(&file_path)?; - } + }, _is_file @ false => { if let Some(path) = file_path.parent() { if !path.exists() { @@ -83,7 +83,7 @@ impl ZipDecompressor { let mut output_file = fs::File::create(&file_path)?; io::copy(&mut file, &mut output_file)?; - } + }, } #[cfg(unix)] @@ -104,14 +104,14 @@ impl ZipDecompressor { // Decompressing a .zip archive loaded up in memory let mut archive = zip::ZipArchive::new(Cursor::new(bytes))?; Ok(Self::zip_decompress(&mut archive, into, flags)?) - } + }, None => { // Decompressing a .zip archive from the file system let file = fs::File::open(&from.path)?; let mut archive = zip::ZipArchive::new(file)?; Ok(Self::zip_decompress(&mut archive, into, flags)?) - } + }, } } } diff --git a/src/dialogs.rs b/src/dialogs.rs index 645a432..5d5e44e 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -12,10 +12,7 @@ pub struct Error; impl<'a> Confirmation<'a> { pub fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { - Self { - prompt, - placeholder: pattern, - } + Self { prompt, placeholder: pattern } } pub fn ask(&self, substitute: Option<&'a str>) -> crate::Result { @@ -26,7 +23,14 @@ impl<'a> Confirmation<'a> { }; loop { - print!("{} [{}Y{}/{}n{}] ", message, colors::green(), colors::reset(), colors::red(), colors::reset()); + print!( + "{} [{}Y{}/{}n{}] ", + message, + colors::green(), + colors::reset(), + colors::red(), + colors::reset() + ); io::stdout().flush()?; let mut answer = String::new(); @@ -40,7 +44,7 @@ impl<'a> Confirmation<'a> { match trimmed_answer.to_ascii_lowercase().as_ref() { "y" | "yes" => return Ok(true), "n" | "no" => return Ok(false), - _ => {} + _ => {}, } } } diff --git a/src/error.rs b/src/error.rs index 89953c0..83657e1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ -use crate::utils::colors; use std::{fmt, path::PathBuf}; +use crate::utils::colors; + pub enum Error { UnknownExtensionError(String), MissingExtensionError(PathBuf), @@ -36,18 +37,18 @@ impl fmt::Display for Error { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; // TODO: show MIME type of the unsupported file write!(f, "cannot compress to {:?}, likely because it has an unsupported (or missing) extension.", filename) - } + }, Error::WalkdirError => { // Already printed in the From block write!(f, "") - } + }, Error::FileNotFound(file) => { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; if file == &PathBuf::from("") { return write!(f, "file not found!"); } write!(f, "file {:?} not found!", file) - } + }, Error::CompressingRootFolder => { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; let spacing = " "; @@ -64,32 +65,27 @@ impl fmt::Display for Error { colors::green(), colors::reset() ) - } + }, Error::MissingArgumentsForCompression => { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; let spacing = " "; writeln!(f,"The compress subcommands demands at least 2 arguments, an input file and an output file.")?; writeln!(f, "{}Example: `ouch compress img.jpeg img.zip`", spacing)?; write!(f, "{}For more information, run `ouch --help`", spacing) - } + }, Error::InternalError => { write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}https://github.com/vrmiguel/ouch{}", colors::green(), colors::reset()) - } + }, Error::IoError(io_err) => { write!(f, "{}[ERROR]{} {}", colors::red(), colors::reset(), io_err) - } + }, Error::CompressionTypo => { - write!( - f, - "Did you mean {}ouch compress{}?", - colors::magenta(), - colors::reset() - ) - } + write!(f, "Did you mean {}ouch compress{}?", colors::magenta(), colors::reset()) + }, _err => { todo!(); - } + }, } } } diff --git a/src/extension.rs b/src/extension.rs index 06de63d..e6a313a 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -34,10 +34,7 @@ pub fn get_extension_from_filename(file_name: &OsStr) -> Option<(&OsStr, &OsStr) impl From for Extension { fn from(second_ext: CompressionFormat) -> Self { - Self { - first_ext: None, - second_ext, - } + Self { first_ext: None, second_ext } } } @@ -57,29 +54,22 @@ impl Extension { (os_str, snd) if os_str.is_empty() => (None, snd), (fst, snd) => (Some(fst), snd), }, - None => { - return Err(crate::Error::MissingExtensionError(PathBuf::from( - file_name, - ))) - } + None => return Err(crate::Error::MissingExtensionError(PathBuf::from(file_name))), }; let (first_ext, second_ext) = match (first_ext, second_ext) { (None, snd) => { let ext = compression_format_from(snd)?; (None, ext) - } + }, (Some(fst), snd) => { let snd = compression_format_from(snd)?; let fst = compression_format_from(fst).ok(); (fst, snd) - } + }, }; - Ok(Self { - first_ext, - second_ext, - }) + Ok(Self { first_ext, second_ext }) } } @@ -119,7 +109,7 @@ impl TryFrom<&PathBuf> for CompressionFormat { Some(ext) => ext, None => { return Err(crate::Error::MissingExtensionError(PathBuf::new())); - } + }, }; extension_from_os_str(ext) } @@ -141,16 +131,12 @@ impl TryFrom<&str> for CompressionFormat { impl Display for CompressionFormat { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Gzip => ".gz", - Bzip => ".bz", - Lzma => ".lz", - Tar => ".tar", - Zip => ".zip", - } - ) + write!(f, "{}", match self { + Gzip => ".gz", + Bzip => ".bz", + Lzma => ".lz", + Tar => ".tar", + Zip => ".zip", + }) } } diff --git a/src/file.rs b/src/file.rs index a3d2623..c14d2d8 100644 --- a/src/file.rs +++ b/src/file.rs @@ -21,10 +21,6 @@ impl<'a> File<'a> { pub fn from(path: &'a Path) -> crate::Result { let extension = Extension::from(path.as_ref()).ok(); - Ok(File { - path, - contents_in_memory: None, - extension, - }) + Ok(File { path, contents_in_memory: None, extension }) } } diff --git a/src/lib.rs b/src/lib.rs index fa9bad7..c227598 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,10 +72,5 @@ Visit https://github.com/vrmiguel/ouch for more usage examples.", #[inline] fn version_command() { use utils::colors::*; - println!( - "{green}ouch{reset} {}", - crate::VERSION, - green = green(), - reset = reset(), - ); + println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset(),); } diff --git a/src/utils.rs b/src/utils.rs index 011d3ed..d989543 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -79,7 +79,7 @@ pub fn get_destination_path<'a>(dest: &'a Option) -> &'a Path { // Must be None according to the way command-line arg. parsing in Ouch works assert_eq!(output_file.extension, None); Path::new(&output_file.path) - } + }, None => Path::new("."), } } @@ -93,9 +93,7 @@ pub fn change_dir_and_return_parent(filename: &Path) -> crate::Result { return Err(crate::Error::CompressingRootFolder); }; - env::set_current_dir(parent) - .ok() - .ok_or(crate::Error::CompressingRootFolder)?; + env::set_current_dir(parent).ok().ok_or(crate::Error::CompressingRootFolder)?; Ok(previous_location) } @@ -107,11 +105,13 @@ pub fn permission_for_overwriting( ) -> crate::Result { match (flags.is_present("yes"), flags.is_present("no")) { (true, true) => { - unreachable!("This should've been cutted out in the ~/src/cli.rs filter flags function.") - } + unreachable!( + "This should've been cutted out in the ~/src/cli.rs filter flags function." + ) + }, (true, _) => return Ok(true), (_, true) => return Ok(false), - _ => {} + _ => {}, } let file_path_str = to_utf(path); @@ -181,9 +181,7 @@ impl Bytes { const UNIT_PREFIXES: [&'static str; 6] = ["", "k", "M", "G", "T", "P"]; pub fn new(bytes: u64) -> Self { - Self { - bytes: bytes as f64, - } + Self { bytes: bytes as f64 } } }