Refactor Opt into Arguments, Options, and Subcommand
type: reform
This commit is contained in:
		
							parent
							
								
									eb8efaf528
								
							
						
					
					
						commit
						4fffa777b4
					
				
							
								
								
									
										28
									
								
								src/arguments.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/arguments.rs
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -51,12 +51,13 @@ pub(crate) use crate::{
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// structs and enums
 | 
					// structs and enums
 | 
				
			||||||
pub(crate) use crate::{
 | 
					pub(crate) use crate::{
 | 
				
			||||||
  bytes::Bytes, env::Env, error::Error, file_info::FileInfo, file_path::FilePath,
 | 
					  arguments::Arguments, bytes::Bytes, env::Env, error::Error, file_info::FileInfo,
 | 
				
			||||||
  file_status::FileStatus, files::Files, hasher::Hasher, info::Info, lint::Lint, linter::Linter,
 | 
					  file_path::FilePath, file_status::FileStatus, files::Files, hasher::Hasher, info::Info,
 | 
				
			||||||
  md5_digest::Md5Digest, metainfo::Metainfo, mode::Mode, node::Node, opt::Opt,
 | 
					  lint::Lint, linter::Linter, md5_digest::Md5Digest, metainfo::Metainfo, mode::Mode, node::Node,
 | 
				
			||||||
  piece_length_picker::PieceLengthPicker, piece_list::PieceList, platform::Platform,
 | 
					  options::Options, piece_length_picker::PieceLengthPicker, piece_list::PieceList,
 | 
				
			||||||
  sha1_digest::Sha1Digest, status::Status, style::Style, table::Table, target::Target,
 | 
					  platform::Platform, sha1_digest::Sha1Digest, status::Status, style::Style,
 | 
				
			||||||
  torrent_summary::TorrentSummary, use_color::UseColor, verifier::Verifier, walker::Walker,
 | 
					  subcommand::Subcommand, table::Table, target::Target, torrent_summary::TorrentSummary,
 | 
				
			||||||
 | 
					  use_color::UseColor, verifier::Verifier, walker::Walker,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// type aliases
 | 
					// type aliases
 | 
				
			||||||
 | 
				
			|||||||
@ -52,15 +52,15 @@ impl Env {
 | 
				
			|||||||
    #[cfg(not(test))]
 | 
					    #[cfg(not(test))]
 | 
				
			||||||
    pretty_env_logger::init();
 | 
					    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::Always => self.err_style = Style::active(),
 | 
				
			||||||
      UseColor::Auto => {}
 | 
					      UseColor::Auto => {}
 | 
				
			||||||
      UseColor::Never => self.err_style = Style::inactive(),
 | 
					      UseColor::Never => self.err_style = Style::inactive(),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    opt.run(self)
 | 
					    args.run(self)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  pub(crate) fn new<D, O, E, S, I>(
 | 
					  pub(crate) fn new<D, O, E, S, I>(
 | 
				
			||||||
 | 
				
			|||||||
@ -52,6 +52,7 @@ mod test_env_builder;
 | 
				
			|||||||
#[cfg(test)]
 | 
					#[cfg(test)]
 | 
				
			||||||
mod capture;
 | 
					mod capture;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					mod arguments;
 | 
				
			||||||
mod bytes;
 | 
					mod bytes;
 | 
				
			||||||
mod common;
 | 
					mod common;
 | 
				
			||||||
mod consts;
 | 
					mod consts;
 | 
				
			||||||
@ -71,7 +72,7 @@ mod md5_digest;
 | 
				
			|||||||
mod metainfo;
 | 
					mod metainfo;
 | 
				
			||||||
mod mode;
 | 
					mod mode;
 | 
				
			||||||
mod node;
 | 
					mod node;
 | 
				
			||||||
mod opt;
 | 
					mod options;
 | 
				
			||||||
mod path_ext;
 | 
					mod path_ext;
 | 
				
			||||||
mod piece_length_picker;
 | 
					mod piece_length_picker;
 | 
				
			||||||
mod piece_list;
 | 
					mod piece_list;
 | 
				
			||||||
@ -81,6 +82,7 @@ mod reckoner;
 | 
				
			|||||||
mod sha1_digest;
 | 
					mod sha1_digest;
 | 
				
			||||||
mod status;
 | 
					mod status;
 | 
				
			||||||
mod style;
 | 
					mod style;
 | 
				
			||||||
 | 
					mod subcommand;
 | 
				
			||||||
mod table;
 | 
					mod table;
 | 
				
			||||||
mod target;
 | 
					mod target;
 | 
				
			||||||
mod torrent_summary;
 | 
					mod torrent_summary;
 | 
				
			||||||
 | 
				
			|||||||
@ -1,18 +1,7 @@
 | 
				
			|||||||
use crate::common::*;
 | 
					use crate::common::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
mod torrent;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(StructOpt)]
 | 
					#[derive(StructOpt)]
 | 
				
			||||||
#[structopt(
 | 
					pub(crate) struct Options {
 | 
				
			||||||
  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 {
 | 
					 | 
				
			||||||
  #[structopt(
 | 
					  #[structopt(
 | 
				
			||||||
    long = "unstable",
 | 
					    long = "unstable",
 | 
				
			||||||
    short = "u",
 | 
					    short = "u",
 | 
				
			||||||
@ -35,25 +24,14 @@ pub(crate) struct Opt {
 | 
				
			|||||||
                 value of `dumb`.",
 | 
					                 value of `dumb`.",
 | 
				
			||||||
  )]
 | 
					  )]
 | 
				
			||||||
  pub(crate) use_color: UseColor,
 | 
					  pub(crate) use_color: UseColor,
 | 
				
			||||||
  #[structopt(subcommand)]
 | 
					 | 
				
			||||||
  subcommand: Subcommand,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Opt {
 | 
					impl Options {
 | 
				
			||||||
  pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> {
 | 
					  pub(crate) fn require_unstable(&self, feature: &'static str) -> Result<(), Error> {
 | 
				
			||||||
    self.subcommand.run(env, self.unstable)
 | 
					    if self.unstable {
 | 
				
			||||||
  }
 | 
					      Ok(())
 | 
				
			||||||
}
 | 
					    } else {
 | 
				
			||||||
 | 
					      Err(Error::Unstable { feature })
 | 
				
			||||||
#[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),
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								src/subcommand.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/subcommand.rs
									
									
									
									
									
										Normal 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),
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -22,12 +22,12 @@ pub(crate) enum Torrent {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl 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 {
 | 
					    match self {
 | 
				
			||||||
      Self::Create(create) => create.run(env),
 | 
					      Self::Create(create) => create.run(env),
 | 
				
			||||||
      Self::PieceLength(piece_length) => piece_length.run(env),
 | 
					      Self::PieceLength(piece_length) => piece_length.run(env),
 | 
				
			||||||
      Self::Show(show) => show.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),
 | 
					      Self::Verify(verify) => verify.run(env),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -48,12 +48,8 @@ pub(crate) struct Stats {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Stats {
 | 
					impl Stats {
 | 
				
			||||||
  pub(crate) fn run(self, env: &mut Env, unstable: bool) -> Result<(), Error> {
 | 
					  pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> {
 | 
				
			||||||
    if !unstable {
 | 
					    options.require_unstable("torrent stats subcommand")?;
 | 
				
			||||||
      return Err(Error::Unstable {
 | 
					 | 
				
			||||||
        feature: "torrent stats subcommand",
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let path = env.resolve(self.input);
 | 
					    let path = env.resolve(self.input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user