mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
refactor: New File struct and switch to use Extension
This commit is contained in:
parent
7fd6020d99
commit
155fca4526
35
src/cli.rs
35
src/cli.rs
@ -4,7 +4,7 @@ use clap::{Arg, Values};
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::error;
|
use crate::error;
|
||||||
use crate::extensions::CompressionFormat;
|
use crate::extension::{Extension, CompressionFormat};
|
||||||
use crate::file::File;
|
use crate::file::File;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
@ -15,7 +15,7 @@ pub enum CommandKind {
|
|||||||
),
|
),
|
||||||
Decompression(
|
Decompression(
|
||||||
// Files to be decompressed and their extensions
|
// Files to be decompressed and their extensions
|
||||||
Vec<(PathBuf, CompressionFormat)>,
|
Vec<File>,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,6 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
|
|
||||||
for file in input_files.clone() {
|
for file in input_files.clone() {
|
||||||
if let (file, Err(_)) = file {
|
if let (file, Err(_)) = file {
|
||||||
// eprintln!("{}: file '{}' is not decompressible.", "error".red(), file);
|
|
||||||
return Err(error::Error::InputsMustHaveBeenDecompressible(file.into()));
|
return Err(error::Error::InputsMustHaveBeenDecompressible(file.into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,9 +90,11 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
if output_was_supplied {
|
if output_was_supplied {
|
||||||
let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied
|
let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied
|
||||||
|
|
||||||
let output_file_extension = CompressionFormat::try_from(output_file);
|
let output_file_extension = Extension::new(output_file);
|
||||||
let output_is_compressible = output_file_extension.is_ok();
|
let output_is_compressible = output_file_extension.is_ok();
|
||||||
if output_is_compressible {
|
if output_is_compressible {
|
||||||
|
// The supplied output is compressible, so we'll compress our inputs to it
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}: trying to compress input files into '{}'",
|
"{}: trying to compress input files into '{}'",
|
||||||
"info".yellow(),
|
"info".yellow(),
|
||||||
@ -102,13 +103,22 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
|
|
||||||
let input_files = input_files.map(PathBuf::from).collect();
|
let input_files = input_files.map(PathBuf::from).collect();
|
||||||
|
|
||||||
|
// return Ok(Command {
|
||||||
|
// kind: CommandKind::Compression(input_files),
|
||||||
|
// output: Some(File::WithExtension((
|
||||||
|
// output_file.into(),
|
||||||
|
// output_file_extension.unwrap(),
|
||||||
|
// ))),
|
||||||
|
// });
|
||||||
|
|
||||||
return Ok(Command {
|
return Ok(Command {
|
||||||
kind: CommandKind::Compression(input_files),
|
kind: CommandKind::Compression(input_files),
|
||||||
output: Some(File::WithExtension((
|
output: Some(File {
|
||||||
output_file.into(),
|
path: output_file.into(),
|
||||||
output_file_extension.unwrap(),
|
extension: Some(output_file_extension.unwrap())
|
||||||
))),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Checking if input files are decompressible
|
// Checking if input files are decompressible
|
||||||
|
|
||||||
@ -119,9 +129,15 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
"info".yellow(),
|
"info".yellow(),
|
||||||
output_file
|
output_file
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let input_files = input_files.into_iter().map(File::from).collect();
|
||||||
|
|
||||||
return Ok(Command {
|
return Ok(Command {
|
||||||
kind: CommandKind::Decompression(input_files),
|
kind: CommandKind::Decompression(input_files),
|
||||||
output: Some(File::WithoutExtension(output_file.into())),
|
output: Some(File {
|
||||||
|
path: output_file.into(),
|
||||||
|
extension: None
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -129,6 +145,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
// Case 1: all input files are decompressible
|
// Case 1: all input files are decompressible
|
||||||
// Case 2: error
|
// Case 2: error
|
||||||
let input_files = process_decompressible_input(input_files)?;
|
let input_files = process_decompressible_input(input_files)?;
|
||||||
|
let input_files = input_files.into_iter().map(File::from).collect();
|
||||||
return Ok(Command {
|
return Ok(Command {
|
||||||
kind: CommandKind::Decompression(input_files),
|
kind: CommandKind::Decompression(input_files),
|
||||||
output: None,
|
output: None,
|
||||||
|
@ -2,7 +2,7 @@ use std::{convert::TryFrom, path::{PathBuf}};
|
|||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::{cli::{Command, CommandKind}, error, extensions::CompressionFormat, file::File};
|
use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File};
|
||||||
|
|
||||||
pub struct Evaluator {
|
pub struct Evaluator {
|
||||||
command: Command,
|
command: Command,
|
||||||
@ -26,27 +26,27 @@ impl Evaluator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_decompression(files_to_decompress: &[(PathBuf, CompressionFormat)], output_file: &Option<File>) {
|
fn handle_decompression(files_to_decompress: &[File], output_file: &Option<File>) {
|
||||||
for (filename, extension) in files_to_decompress {
|
// for (filename, extension) in files_to_decompress {
|
||||||
// println!("file: {:?}, extension: {:?}", filename, extension);
|
// // println!("file: {:?}, extension: {:?}", filename, extension);
|
||||||
|
|
||||||
// TODO: actually decompress anything ;-;
|
// // TODO: actually decompress anything ;-;
|
||||||
|
|
||||||
// Once decompressed, check if the file can be decompressed further
|
// // Once decompressed, check if the file can be decompressed further
|
||||||
// e.g.: "foobar.tar.gz" -> "foobar.tar"
|
// // e.g.: "foobar.tar.gz" -> "foobar.tar"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into();
|
// let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into();
|
||||||
match CompressionFormat::try_from(filename) {
|
// match CompressionFormat::try_from(filename) {
|
||||||
Ok(extension) => {
|
// Ok(extension) => {
|
||||||
println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension);
|
// println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension);
|
||||||
},
|
// },
|
||||||
Err(err) => {
|
// Err(err) => {
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn evaluate(&mut self) {
|
pub fn evaluate(&mut self) {
|
||||||
|
@ -6,11 +6,21 @@ use CompressionFormat::*;
|
|||||||
/// Represents the extension of a file, but only really caring about
|
/// Represents the extension of a file, but only really caring about
|
||||||
/// compression formats (and .tar).
|
/// compression formats (and .tar).
|
||||||
/// Ex.: Extension::new("file.tar.gz") == Extension { first_ext: Some(Tar), second_ext: Gzip }
|
/// Ex.: Extension::new("file.tar.gz") == Extension { first_ext: Some(Tar), second_ext: Gzip }
|
||||||
struct Extension {
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct Extension {
|
||||||
first_ext: Option<CompressionFormat>,
|
first_ext: Option<CompressionFormat>,
|
||||||
second_ext: CompressionFormat
|
second_ext: CompressionFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<CompressionFormat> for Extension {
|
||||||
|
fn from(second_ext: CompressionFormat) -> Self {
|
||||||
|
Self {
|
||||||
|
first_ext: None,
|
||||||
|
second_ext
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Extension {
|
impl Extension {
|
||||||
pub fn new(filename: &str) -> error::OuchResult<Self> {
|
pub fn new(filename: &str) -> error::OuchResult<Self> {
|
||||||
let ext_from_str = |ext| {
|
let ext_from_str = |ext| {
|
28
src/file.rs
28
src/file.rs
@ -1,9 +1,25 @@
|
|||||||
use std::path::PathBuf;
|
use std::{path::PathBuf, process::Command};
|
||||||
|
|
||||||
use crate::extensions::CompressionFormat;
|
use crate::extension::{CompressionFormat, Extension};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
|
||||||
pub enum File {
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
WithExtension((PathBuf, CompressionFormat)),
|
pub struct File {
|
||||||
WithoutExtension(PathBuf),
|
/// File's (relative) path
|
||||||
|
pub path: PathBuf,
|
||||||
|
/// Note: extension here might be a misleading name since
|
||||||
|
/// we don't really care about any extension other than supported compression ones.
|
||||||
|
///
|
||||||
|
/// So, for example, if a file has pathname "image.jpeg", it does have a JPEG extension but will
|
||||||
|
/// be represented as a None over here since that's not an extension we're particularly interested in
|
||||||
|
pub extension: Option<Extension>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<(PathBuf, CompressionFormat)> for File {
|
||||||
|
fn from((path, format): (PathBuf, CompressionFormat)) -> Self {
|
||||||
|
Self {
|
||||||
|
path,
|
||||||
|
extension: Some(Extension::from(format)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
27
src/main.rs
27
src/main.rs
@ -1,26 +1,23 @@
|
|||||||
use std::{convert::TryFrom, ffi::OsStr, path::Path};
|
use std::{convert::TryFrom};
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod error;
|
mod error;
|
||||||
mod extensions;
|
mod extension;
|
||||||
mod file;
|
mod file;
|
||||||
mod test;
|
mod test;
|
||||||
mod evaluator;
|
mod evaluator;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// let matches = cli::get_matches();
|
let matches = cli::get_matches();
|
||||||
// match cli::Command::try_from(matches) {
|
match cli::Command::try_from(matches) {
|
||||||
// Ok(command) => {
|
Ok(command) => {
|
||||||
// let mut eval = evaluator::Evaluator::new(command);
|
let mut eval = evaluator::Evaluator::new(command);
|
||||||
// eval.evaluate();
|
eval.evaluate();
|
||||||
// }
|
}
|
||||||
// Err(err) => {
|
Err(err) => {
|
||||||
// print!("{}: {}\n", "error".red(), err);
|
print!("{}: {}\n", "error".red(), err);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
dbg!(extensions::get_extension_from_filename("file"));
|
|
||||||
// dbg!(get_extension_from_filename("file.zip"));
|
|
||||||
}
|
}
|
||||||
|
37
src/test.rs
37
src/test.rs
@ -1,11 +1,12 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod cli {
|
mod cli {
|
||||||
|
|
||||||
use crate::cli::clap_app;
|
use crate::{cli::clap_app, extension};
|
||||||
use crate::cli::Command;
|
use crate::cli::Command;
|
||||||
use crate::cli::CommandKind::*;
|
use crate::cli::CommandKind::*;
|
||||||
use crate::error::OuchResult;
|
use crate::error::OuchResult;
|
||||||
use crate::extensions::CompressionFormat::*;
|
use crate::extension::CompressionFormat::*;
|
||||||
|
use crate::extension::Extension;
|
||||||
use crate::file::File;
|
use crate::file::File;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
@ -17,8 +18,16 @@ mod cli {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_from_matches,
|
command_from_matches,
|
||||||
Command {
|
Command {
|
||||||
kind: Decompression(vec![("file.zip".into(), Zip,),],),
|
kind: Decompression(vec![
|
||||||
output: Some(File::WithoutExtension("folder".into())),
|
File {
|
||||||
|
path: "file.zip".into(),
|
||||||
|
extension: Some(Extension::from(Zip))
|
||||||
|
}
|
||||||
|
]),
|
||||||
|
output: Some(File {
|
||||||
|
path: "folder".into(),
|
||||||
|
extension: None
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -34,8 +43,14 @@ mod cli {
|
|||||||
command_from_matches,
|
command_from_matches,
|
||||||
Command {
|
Command {
|
||||||
kind: Decompression(vec![
|
kind: Decompression(vec![
|
||||||
("file.zip".into(), Zip,),
|
File {
|
||||||
("file.tar".into(), Tar,),
|
path: "file.zip".into(),
|
||||||
|
extension: Some(Extension::from(Zip))
|
||||||
|
},
|
||||||
|
File {
|
||||||
|
path: "file.tar".into(),
|
||||||
|
extension: Some(Extension::from(Tar))
|
||||||
|
}
|
||||||
],),
|
],),
|
||||||
output: None,
|
output: None,
|
||||||
}
|
}
|
||||||
@ -65,7 +80,13 @@ mod cli {
|
|||||||
"file2.jpeg".into(),
|
"file2.jpeg".into(),
|
||||||
"file3.ok".into()
|
"file3.ok".into()
|
||||||
]),
|
]),
|
||||||
output: Some(File::WithExtension(("file.tar".into(), Tar)))
|
// output: Some(File::WithExtension(("file.tar".into(), Extension::from(Tar))))
|
||||||
|
output: Some(
|
||||||
|
File {
|
||||||
|
path: "file.tar".into(),
|
||||||
|
extension: Some(Extension::from(Tar))
|
||||||
|
}
|
||||||
|
),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -101,7 +122,7 @@ mod cli_errors {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod extension_extraction {
|
mod extension_extraction {
|
||||||
use crate::error::OuchResult;
|
use crate::error::OuchResult;
|
||||||
use crate::extensions::CompressionFormat;
|
use crate::extension::CompressionFormat;
|
||||||
use std::{convert::TryFrom, path::PathBuf, str::FromStr};
|
use std::{convert::TryFrom, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user