mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +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 crate::error;
|
||||
use crate::extensions::CompressionFormat;
|
||||
use crate::extension::{Extension, CompressionFormat};
|
||||
use crate::file::File;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
@ -15,7 +15,7 @@ pub enum CommandKind {
|
||||
),
|
||||
Decompression(
|
||||
// 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() {
|
||||
if let (file, Err(_)) = file {
|
||||
// eprintln!("{}: file '{}' is not decompressible.", "error".red(), file);
|
||||
return Err(error::Error::InputsMustHaveBeenDecompressible(file.into()));
|
||||
}
|
||||
}
|
||||
@ -91,9 +90,11 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
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_extension = CompressionFormat::try_from(output_file);
|
||||
let output_file_extension = Extension::new(output_file);
|
||||
let output_is_compressible = output_file_extension.is_ok();
|
||||
if output_is_compressible {
|
||||
// The supplied output is compressible, so we'll compress our inputs to it
|
||||
|
||||
println!(
|
||||
"{}: trying to compress input files into '{}'",
|
||||
"info".yellow(),
|
||||
@ -102,13 +103,22 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
|
||||
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 {
|
||||
kind: CommandKind::Compression(input_files),
|
||||
output: Some(File::WithExtension((
|
||||
output_file.into(),
|
||||
output_file_extension.unwrap(),
|
||||
))),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
extension: Some(output_file_extension.unwrap())
|
||||
}),
|
||||
});
|
||||
|
||||
} else {
|
||||
// Checking if input files are decompressible
|
||||
|
||||
@ -119,9 +129,15 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
"info".yellow(),
|
||||
output_file
|
||||
);
|
||||
|
||||
let input_files = input_files.into_iter().map(File::from).collect();
|
||||
|
||||
return Ok(Command {
|
||||
kind: CommandKind::Decompression(input_files),
|
||||
output: Some(File::WithoutExtension(output_file.into())),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
extension: None
|
||||
})
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@ -129,6 +145,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
// Case 1: all input files are decompressible
|
||||
// Case 2: error
|
||||
let input_files = process_decompressible_input(input_files)?;
|
||||
let input_files = input_files.into_iter().map(File::from).collect();
|
||||
return Ok(Command {
|
||||
kind: CommandKind::Decompression(input_files),
|
||||
output: None,
|
||||
|
@ -2,7 +2,7 @@ use std::{convert::TryFrom, path::{PathBuf}};
|
||||
|
||||
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 {
|
||||
command: Command,
|
||||
@ -26,27 +26,27 @@ impl Evaluator {
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_decompression(files_to_decompress: &[(PathBuf, CompressionFormat)], output_file: &Option<File>) {
|
||||
for (filename, extension) in files_to_decompress {
|
||||
// println!("file: {:?}, extension: {:?}", filename, extension);
|
||||
fn handle_decompression(files_to_decompress: &[File], output_file: &Option<File>) {
|
||||
// for (filename, extension) in files_to_decompress {
|
||||
// // println!("file: {:?}, extension: {:?}", filename, extension);
|
||||
|
||||
// TODO: actually decompress anything ;-;
|
||||
// // TODO: actually decompress anything ;-;
|
||||
|
||||
// Once decompressed, check if the file can be decompressed further
|
||||
// e.g.: "foobar.tar.gz" -> "foobar.tar"
|
||||
// // Once decompressed, check if the file can be decompressed further
|
||||
// // e.g.: "foobar.tar.gz" -> "foobar.tar"
|
||||
|
||||
|
||||
|
||||
let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into();
|
||||
match CompressionFormat::try_from(filename) {
|
||||
Ok(extension) => {
|
||||
println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension);
|
||||
},
|
||||
Err(err) => {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
// let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into();
|
||||
// match CompressionFormat::try_from(filename) {
|
||||
// Ok(extension) => {
|
||||
// println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension);
|
||||
// },
|
||||
// Err(err) => {
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn evaluate(&mut self) {
|
||||
|
@ -6,11 +6,21 @@ use CompressionFormat::*;
|
||||
/// Represents the extension of a file, but only really caring about
|
||||
/// compression formats (and .tar).
|
||||
/// 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>,
|
||||
second_ext: CompressionFormat
|
||||
}
|
||||
|
||||
impl From<CompressionFormat> for Extension {
|
||||
fn from(second_ext: CompressionFormat) -> Self {
|
||||
Self {
|
||||
first_ext: None,
|
||||
second_ext
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Extension {
|
||||
pub fn new(filename: &str) -> error::OuchResult<Self> {
|
||||
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 {
|
||||
WithExtension((PathBuf, CompressionFormat)),
|
||||
WithoutExtension(PathBuf),
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct File {
|
||||
/// 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;
|
||||
|
||||
mod cli;
|
||||
mod error;
|
||||
mod extensions;
|
||||
mod extension;
|
||||
mod file;
|
||||
mod test;
|
||||
mod evaluator;
|
||||
|
||||
fn main() {
|
||||
// let matches = cli::get_matches();
|
||||
// match cli::Command::try_from(matches) {
|
||||
// Ok(command) => {
|
||||
// let mut eval = evaluator::Evaluator::new(command);
|
||||
// eval.evaluate();
|
||||
// }
|
||||
// Err(err) => {
|
||||
// print!("{}: {}\n", "error".red(), err);
|
||||
// }
|
||||
// }
|
||||
|
||||
dbg!(extensions::get_extension_from_filename("file"));
|
||||
// dbg!(get_extension_from_filename("file.zip"));
|
||||
let matches = cli::get_matches();
|
||||
match cli::Command::try_from(matches) {
|
||||
Ok(command) => {
|
||||
let mut eval = evaluator::Evaluator::new(command);
|
||||
eval.evaluate();
|
||||
}
|
||||
Err(err) => {
|
||||
print!("{}: {}\n", "error".red(), err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
37
src/test.rs
37
src/test.rs
@ -1,11 +1,12 @@
|
||||
#[cfg(test)]
|
||||
mod cli {
|
||||
|
||||
use crate::cli::clap_app;
|
||||
use crate::{cli::clap_app, extension};
|
||||
use crate::cli::Command;
|
||||
use crate::cli::CommandKind::*;
|
||||
use crate::error::OuchResult;
|
||||
use crate::extensions::CompressionFormat::*;
|
||||
use crate::extension::CompressionFormat::*;
|
||||
use crate::extension::Extension;
|
||||
use crate::file::File;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
@ -17,8 +18,16 @@ mod cli {
|
||||
assert_eq!(
|
||||
command_from_matches,
|
||||
Command {
|
||||
kind: Decompression(vec![("file.zip".into(), Zip,),],),
|
||||
output: Some(File::WithoutExtension("folder".into())),
|
||||
kind: Decompression(vec![
|
||||
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 {
|
||||
kind: Decompression(vec![
|
||||
("file.zip".into(), Zip,),
|
||||
("file.tar".into(), Tar,),
|
||||
File {
|
||||
path: "file.zip".into(),
|
||||
extension: Some(Extension::from(Zip))
|
||||
},
|
||||
File {
|
||||
path: "file.tar".into(),
|
||||
extension: Some(Extension::from(Tar))
|
||||
}
|
||||
],),
|
||||
output: None,
|
||||
}
|
||||
@ -65,7 +80,13 @@ mod cli {
|
||||
"file2.jpeg".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)]
|
||||
mod extension_extraction {
|
||||
use crate::error::OuchResult;
|
||||
use crate::extensions::CompressionFormat;
|
||||
use crate::extension::CompressionFormat;
|
||||
use std::{convert::TryFrom, path::PathBuf, str::FromStr};
|
||||
|
||||
#[test]
|
||||
|
Loading…
x
Reference in New Issue
Block a user