diff --git a/src/arguments.rs b/src/arguments.rs new file mode 100644 index 0000000..689d95d --- /dev/null +++ b/src/arguments.rs @@ -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 + } +} diff --git a/src/common.rs b/src/common.rs index dbc049a..8512802 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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 diff --git a/src/env.rs b/src/env.rs index cb843aa..cbca55e 100644 --- a/src/env.rs +++ b/src/env.rs @@ -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( diff --git a/src/main.rs b/src/main.rs index 0e66b63..b4434c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/opt.rs b/src/options.rs similarity index 59% rename from src/opt.rs rename to src/options.rs index 605e401..f69d834 100644 --- a/src/opt.rs +++ b/src/options.rs @@ -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 }) } } } diff --git a/src/subcommand.rs b/src/subcommand.rs new file mode 100644 index 0000000..b945f66 --- /dev/null +++ b/src/subcommand.rs @@ -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), + } + } +} diff --git a/src/opt/torrent.rs b/src/subcommand/torrent.rs similarity index 83% rename from src/opt/torrent.rs rename to src/subcommand/torrent.rs index 848d5a3..266ab03 100644 --- a/src/opt/torrent.rs +++ b/src/subcommand/torrent.rs @@ -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), } } diff --git a/src/opt/torrent/create.rs b/src/subcommand/torrent/create.rs similarity index 100% rename from src/opt/torrent/create.rs rename to src/subcommand/torrent/create.rs diff --git a/src/opt/torrent/piece_length.rs b/src/subcommand/torrent/piece_length.rs similarity index 100% rename from src/opt/torrent/piece_length.rs rename to src/subcommand/torrent/piece_length.rs diff --git a/src/opt/torrent/show.rs b/src/subcommand/torrent/show.rs similarity index 100% rename from src/opt/torrent/show.rs rename to src/subcommand/torrent/show.rs diff --git a/src/opt/torrent/stats.rs b/src/subcommand/torrent/stats.rs similarity index 97% rename from src/opt/torrent/stats.rs rename to src/subcommand/torrent/stats.rs index 8fac559..ff0783a 100644 --- a/src/opt/torrent/stats.rs +++ b/src/subcommand/torrent/stats.rs @@ -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); diff --git a/src/opt/torrent/verify.rs b/src/subcommand/torrent/verify.rs similarity index 100% rename from src/opt/torrent/verify.rs rename to src/subcommand/torrent/verify.rs