Adding rustfmt.toml and reformatting

This commit is contained in:
João M. Bezerra 2021-05-16 17:52:29 -03:00
parent 3dc9a78539
commit d01947d9e1
19 changed files with 114 additions and 178 deletions

View File

@ -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> {

View File

@ -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 {

View File

@ -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<OsStr>,
U: AsRef<str>,
{
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();

13
rustfmt.toml Normal file
View File

@ -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"

View File

@ -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<OsString>) -> crate::Result<ParsedArgs> {
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<OsString>) -> crate::Result<ParsedArgs> {
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<OsString>) -> crate::Result<ParsedArgs> {
// Parse flags
let (args, mut flags) = oof::filter_flags(args, &flags_info)?;
let files = args
.into_iter()
.map(canonicalize)
.collect::<Result<Vec<_>, _>>()?;
let files = args.into_iter().map(canonicalize).collect::<Result<Vec<_>, _>>()?;
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."),
};

View File

@ -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<PathBuf> 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<BoxedCompressor>, 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<BoxedDecompressor>, Bo
colors::reset()
);
return Err(crate::Error::InvalidInput);
}
},
};
let second_decompressor: Box<dyn Decompressor> = 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(())

View File

@ -34,7 +34,7 @@ impl BzipCompressor {
Some(bytes) => bytes,
None => {
return Err(crate::Error::InternalError);
}
},
};
Self::compress_bytes(&*bytes)

View File

@ -38,7 +38,7 @@ impl GzipCompressor {
Some(bytes) => bytes,
None => {
unreachable!();
}
},
};
Self::compress_bytes(file_contents)

View File

@ -38,7 +38,7 @@ impl LzmaCompressor {
Some(bytes) => bytes,
None => {
unreachable!();
}
},
};
Self::compress_bytes(file_contents)

View File

@ -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)?;

View File

@ -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()? {

View File

@ -3,7 +3,6 @@ use std::{
path::Path,
};
use utils::colors;
use super::decompressor::{DecompressionResult, Decompressor};

View File

@ -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)?)
}
},
}
}
}

View File

@ -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<bool> {
@ -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),
_ => {}
_ => {},
}
}
}

View File

@ -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!();
}
},
}
}
}

View File

@ -34,10 +34,7 @@ pub fn get_extension_from_filename(file_name: &OsStr) -> Option<(&OsStr, &OsStr)
impl From<CompressionFormat> 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",
})
}
}

View File

@ -21,10 +21,6 @@ impl<'a> File<'a> {
pub fn from(path: &'a Path) -> crate::Result<Self> {
let extension = Extension::from(path.as_ref()).ok();
Ok(File {
path,
contents_in_memory: None,
extension,
})
Ok(File { path, contents_in_memory: None, extension })
}
}

View File

@ -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(),);
}

View File

@ -79,7 +79,7 @@ pub fn get_destination_path<'a>(dest: &'a Option<File>) -> &'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<PathBuf> {
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<bool> {
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 }
}
}