Refactor Opt into Arguments, Options, and Subcommand

type: reform
This commit is contained in:
Casey Rodarmor 2020-03-06 22:50:04 -08:00
parent eb8efaf528
commit 4fffa777b4
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
12 changed files with 68 additions and 47 deletions

28
src/arguments.rs Normal file
View File

@ -0,0 +1,28 @@
use crate::common::*;
#[derive(StructOpt)]
#[structopt(
about(consts::ABOUT),
version(consts::VERSION),
author(consts::AUTHOR),
help_message(consts::HELP_MESSAGE),
version_message(consts::VERSION_MESSAGE),
global_setting(AppSettings::ColoredHelp),
global_setting(AppSettings::ColorAuto)
)]
pub(crate) struct Arguments {
#[structopt(flatten)]
options: Options,
#[structopt(subcommand)]
subcommand: Subcommand,
}
impl Arguments {
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
self.subcommand.run(env, &self.options)
}
pub(crate) fn options(&self) -> &Options {
&self.options
}
}

View File

@ -51,12 +51,13 @@ pub(crate) use crate::{
// structs and enums
pub(crate) use crate::{
bytes::Bytes, env::Env, error::Error, file_info::FileInfo, file_path::FilePath,
file_status::FileStatus, files::Files, hasher::Hasher, info::Info, lint::Lint, linter::Linter,
md5_digest::Md5Digest, metainfo::Metainfo, mode::Mode, node::Node, opt::Opt,
piece_length_picker::PieceLengthPicker, piece_list::PieceList, platform::Platform,
sha1_digest::Sha1Digest, status::Status, style::Style, table::Table, target::Target,
torrent_summary::TorrentSummary, use_color::UseColor, verifier::Verifier, walker::Walker,
arguments::Arguments, bytes::Bytes, env::Env, error::Error, file_info::FileInfo,
file_path::FilePath, file_status::FileStatus, files::Files, hasher::Hasher, info::Info,
lint::Lint, linter::Linter, md5_digest::Md5Digest, metainfo::Metainfo, mode::Mode, node::Node,
options::Options, piece_length_picker::PieceLengthPicker, piece_list::PieceList,
platform::Platform, sha1_digest::Sha1Digest, status::Status, style::Style,
subcommand::Subcommand, table::Table, target::Target, torrent_summary::TorrentSummary,
use_color::UseColor, verifier::Verifier, walker::Walker,
};
// type aliases

View File

@ -52,15 +52,15 @@ impl Env {
#[cfg(not(test))]
pretty_env_logger::init();
let opt = Opt::from_iter_safe(&self.args)?;
let args = Arguments::from_iter_safe(&self.args)?;
match opt.use_color {
match args.options().use_color {
UseColor::Always => self.err_style = Style::active(),
UseColor::Auto => {}
UseColor::Never => self.err_style = Style::inactive(),
}
opt.run(self)
args.run(self)
}
pub(crate) fn new<D, O, E, S, I>(

View File

@ -52,6 +52,7 @@ mod test_env_builder;
#[cfg(test)]
mod capture;
mod arguments;
mod bytes;
mod common;
mod consts;
@ -71,7 +72,7 @@ mod md5_digest;
mod metainfo;
mod mode;
mod node;
mod opt;
mod options;
mod path_ext;
mod piece_length_picker;
mod piece_list;
@ -81,6 +82,7 @@ mod reckoner;
mod sha1_digest;
mod status;
mod style;
mod subcommand;
mod table;
mod target;
mod torrent_summary;

View File

@ -1,18 +1,7 @@
use crate::common::*;
mod torrent;
#[derive(StructOpt)]
#[structopt(
about(consts::ABOUT),
version(consts::VERSION),
author(consts::AUTHOR),
help_message(consts::HELP_MESSAGE),
version_message(consts::VERSION_MESSAGE),
global_setting(AppSettings::ColoredHelp),
global_setting(AppSettings::ColorAuto)
)]
pub(crate) struct Opt {
pub(crate) struct Options {
#[structopt(
long = "unstable",
short = "u",
@ -35,25 +24,14 @@ pub(crate) struct Opt {
value of `dumb`.",
)]
pub(crate) use_color: UseColor,
#[structopt(subcommand)]
subcommand: Subcommand,
}
impl Opt {
pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
self.subcommand.run(env, self.unstable)
}
}
#[derive(StructOpt)]
pub(crate) enum Subcommand {
Torrent(torrent::Torrent),
}
impl Subcommand {
pub(crate) fn run(self, env: &mut Env, unstable: bool) -> Result<(), Error> {
match self {
Self::Torrent(torrent) => torrent.run(env, unstable),
impl Options {
pub(crate) fn require_unstable(&self, feature: &'static str) -> Result<(), Error> {
if self.unstable {
Ok(())
} else {
Err(Error::Unstable { feature })
}
}
}

16
src/subcommand.rs Normal file
View File

@ -0,0 +1,16 @@
use crate::common::*;
mod torrent;
#[derive(StructOpt)]
pub(crate) enum Subcommand {
Torrent(torrent::Torrent),
}
impl Subcommand {
pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> {
match self {
Self::Torrent(torrent) => torrent.run(env, options),
}
}
}

View File

@ -22,12 +22,12 @@ pub(crate) enum Torrent {
}
impl Torrent {
pub(crate) fn run(self, env: &mut Env, unstable: bool) -> Result<(), Error> {
pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> {
match self {
Self::Create(create) => create.run(env),
Self::PieceLength(piece_length) => piece_length.run(env),
Self::Show(show) => show.run(env),
Self::Stats(stats) => stats.run(env, unstable),
Self::Stats(stats) => stats.run(env, options),
Self::Verify(verify) => verify.run(env),
}
}

View File

@ -48,12 +48,8 @@ pub(crate) struct Stats {
}
impl Stats {
pub(crate) fn run(self, env: &mut Env, unstable: bool) -> Result<(), Error> {
if !unstable {
return Err(Error::Unstable {
feature: "torrent stats subcommand",
});
}
pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> {
options.require_unstable("torrent stats subcommand")?;
let path = env.resolve(self.input);