-
Notifications
You must be signed in to change notification settings - Fork 5
Implement TarOperation and TarParam foundations #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
28a49a6
c156fe1
5223477
6a77bb8
6d0213b
9f23bf2
818e1bd
4e43d5e
e5a9b0d
0e9970b
86e5ae9
b1b486f
4bef208
c959d90
17534ac
d771c52
df20a72
79e4915
bc044a2
9ee96b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||
| use crate::errors::TarError; | ||||||||||
| use crate::operations::Create; | ||||||||||
| use crate::operations::Extract; | ||||||||||
| use crate::options::TarParams; | ||||||||||
| use uucore::error::UResult; | ||||||||||
|
|
||||||||||
| /// The [`OperationKind`] Enum representation of Acdtrux arguments which is | ||||||||||
| /// later leveraged as selector for enum dispatch by the [`TarOperation`] | ||||||||||
| /// trait | ||||||||||
| pub enum OperationKind { | ||||||||||
| Concatenate, | ||||||||||
| Create, | ||||||||||
| Diff, | ||||||||||
| List, | ||||||||||
| Append, | ||||||||||
| Update, | ||||||||||
| Extract, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl TryFrom<&str> for OperationKind { | ||||||||||
| type Error = TarError; | ||||||||||
| fn try_from(value: &str) -> Result<Self, Self::Error> { | ||||||||||
| match value { | ||||||||||
| "concate" => Ok(Self::Concatenate), | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
no ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Ya actually both gnu tar takes both -> All Tar options, BSD tar doesn't have concatenate. Also correct the spelling in this one, thank you for the catch! |
||||||||||
| "create" => Ok(Self::Create), | ||||||||||
| "diff" => Ok(Self::Diff), | ||||||||||
| "list" => Ok(Self::List), | ||||||||||
| "append" => Ok(Self::Append), | ||||||||||
| "update" => Ok(Self::Update), | ||||||||||
| "extract" => Ok(Self::Extract), | ||||||||||
| _ => Err(TarError::TarOperationError(format!( | ||||||||||
| "Invalid operation selected: {}", | ||||||||||
| value | ||||||||||
| ))), | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl TarOperation for OperationKind { | ||||||||||
| fn exec(&self, options: &TarParams) -> UResult<()> { | ||||||||||
| match self { | ||||||||||
| Self::List => unimplemented!(), | ||||||||||
| Self::Create => Create.exec(options), | ||||||||||
| Self::Diff => unimplemented!(), | ||||||||||
| Self::Append => unimplemented!(), | ||||||||||
| Self::Update => unimplemented!(), | ||||||||||
| Self::Extract => Extract.exec(options), | ||||||||||
| Self::Concatenate => unimplemented!(), | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// [`TarOperation`] allows enum dispatch by enforcing the impl of the | ||||||||||
| /// trait to create the functionality to perform the operation requested via | ||||||||||
| /// the command line arg for this execution of tar | ||||||||||
| pub trait TarOperation { | ||||||||||
| fn exec(&self, options: &TarParams) -> UResult<()>; | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // re-exported to remove the redundant level of inception in | ||
| // the module tree | ||
| #[allow(clippy::module_inception)] | ||
| pub mod options; | ||
| pub use crate::options::options::TarOption; | ||
| pub use crate::options::options::TarParams; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| use crate::errors::TarError; | ||
| use crate::operations::OperationKind; | ||
| use clap::ArgMatches; | ||
| use std::path::PathBuf; | ||
| use uucore::error::UResult; | ||
|
|
||
| /// [`TarParams`] Holds common information that is parsed from | ||
| /// command line arguments. That changes the current execution of | ||
| /// tar. | ||
| #[derive(Default)] | ||
| pub struct TarParams { | ||
| archive: PathBuf, | ||
| files: Vec<PathBuf>, | ||
| options: Vec<TarOption>, | ||
| } | ||
|
|
||
| impl From<&ArgMatches> for TarParams { | ||
| fn from(matches: &ArgMatches) -> TarParams { | ||
| let mut ops = Self::default(); | ||
|
|
||
| // -v --verbose | ||
| if matches.get_flag("verbose") { | ||
| ops.options_mut().push(TarOption::Verbose); | ||
| } | ||
|
|
||
| // [FILES]... | ||
| if let Some(files) = matches.get_many::<PathBuf>("files") { | ||
| ops.files = files.map(|x| x.to_owned()).collect(); | ||
| } | ||
|
|
||
| // -f --file | ||
| if let Some(a) = matches.get_one::<PathBuf>("file") { | ||
| ops.archive = a.to_owned(); | ||
| } | ||
|
|
||
| ops | ||
| } | ||
| } | ||
|
|
||
| impl TarParams { | ||
| /// Convence method that parses the [`ArgMatches`] | ||
| /// processed by clap into [`TarParams`] and selects | ||
| /// the appropriate [`OperationKind`] for execution given back to the caller in a | ||
| /// tuple of ([`OperationKind`], [`TarParams`]) | ||
| pub fn with_operation(matches: &ArgMatches) -> UResult<(OperationKind, Self)> { | ||
| if matches.get_flag("create") { | ||
| Ok((OperationKind::Create, Self::from(matches))) | ||
| } else if matches.get_flag("extract") { | ||
| Ok((OperationKind::Extract, Self::from(matches))) | ||
| } else { | ||
| Err(Box::new(TarError::TarOperationError(format!( | ||
| "Error processing: Unknown or Unimplmented Parameters: {:?}", | ||
| matches | ||
| .ids() | ||
| .map(|i| i.to_string()) | ||
| .collect::<Vec<String>>() | ||
| )))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TarParams { | ||
| pub fn files(&self) -> &Vec<PathBuf> { | ||
| &self.files | ||
| } | ||
| pub fn archive(&self) -> &PathBuf { | ||
| &self.archive | ||
| } | ||
| pub fn options(&self) -> &Vec<TarOption> { | ||
| &self.options | ||
| } | ||
| pub fn options_mut(&mut self) -> &mut Vec<TarOption> { | ||
| &mut self.options | ||
| } | ||
| } | ||
|
|
||
| /// [`TarOption`] Enum of avaliable tar options for later use | ||
| /// by [`TarOperation`] impls, eg. List, Create, Delete | ||
| pub enum TarOption { | ||
| Verbose, | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.