Skip to content

Commit

Permalink
Move traits out to the separate mod
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Feb 12, 2020
1 parent 6184733 commit 17f9eca
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
3 changes: 2 additions & 1 deletion clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub fn derive_subcommand(input: &syn::DeriveInput) -> TokenStream {
impl ::clap::Subcommand for #name {
fn from_subcommand<'b>(
name: &str,
matches: ::std::option::Option<&::clap::ArgMatches>) -> Option<Self> {
matches: ::std::option::Option<&::clap::ArgMatches>
) -> Option<Self> {
unimplemented!()
}

Expand Down
66 changes: 66 additions & 0 deletions src/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{App, ArgMatches, Error};
use std::ffi::OsString;

/// This trait is just a convenience on top of FromArgMatches + IntoApp
pub trait Clap: FromArgMatches + IntoApp + Sized {
/// Parse from `std::env::args()`, exit on error
fn parse() -> Self {
let matches = <Self as IntoApp>::into_app().get_matches();
<Self as FromArgMatches>::from_arg_matches(&matches)
}

/// Parse from `std::env::args()`, return Err on error.
fn try_parse() -> Result<Self, Error> {
let matches = <Self as IntoApp>::into_app().try_get_matches()?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches))
}

/// Parse from iterator, exit on error
fn parse_from<I, T>(itr: I) -> Self
where
I: IntoIterator<Item = T>,
// TODO (@CreepySkeleton): discover a way to avoid cloning here
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().get_matches_from(itr);
<Self as FromArgMatches>::from_arg_matches(&matches)
}

/// Parse from `std::env::args()`, return Err on error.
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where
I: IntoIterator<Item = T>,
// TODO (@CreepySkeleton): discover a way to avoid cloning here
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().try_get_matches_from(itr)?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches))
}
}

/// Build an App according to the struct
///
/// Also serves for flattening
pub trait IntoApp: Sized {
/// @TODO @release @docs
fn into_app<'b>() -> App<'b>;
/// @TODO @release @docs
fn augment_clap<'b>(app: App<'b>) -> App<'b>;
}

/// Extract values from ArgMatches into the struct.
pub trait FromArgMatches: Sized {
/// @TODO @release @docs
fn from_arg_matches<'b>(matches: &ArgMatches) -> Self;
}

/// @TODO @release @docs
pub trait Subcommand: Sized {
/// @TODO @release @docs
fn from_subcommand<'b>(name: &str, matches: Option<&ArgMatches>) -> Option<Self>;
/// @TODO @release @docs
fn augment_subcommands<'b>(app: App<'b>) -> App<'b>;
}

/// @TODO @release @docs
pub trait ArgEnum {}
74 changes: 5 additions & 69 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
compile_error!("`std` feature is currently required to build this crate");

pub use crate::build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation};
pub use crate::derive::{Clap, FromArgMatches, IntoApp, Subcommand};
pub use crate::output::fmt::Format;
pub use crate::parse::errors::{Error, ErrorKind, Result};
pub use crate::parse::{ArgMatches, OsValues, SubCommand, Values};
Expand All @@ -462,86 +463,21 @@ pub use clap_derive::{self, *};
#[cfg_attr(feature = "derive", doc(hidden))]
pub use lazy_static;

use std::ffi::OsString;
use std::result::Result as StdResult;
#[doc(hidden)]
pub use mkeymap::KeyType;

#[macro_use]
#[allow(missing_docs)]
pub mod macros;

pub mod derive;

mod build;
mod mkeymap;
mod output;
mod parse;
mod util;

#[doc(hidden)]
pub use mkeymap::KeyType;

const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a bug \
report at https://github.com/clap-rs/clap/issues";
const INVALID_UTF8: &str = "unexpected invalid UTF-8 code point";

/// This trait is just a convenience on top of FromArgMatches + IntoApp
pub trait Clap: FromArgMatches + IntoApp + Sized {
/// Parse from `std::env::args()`, exit on error
fn parse() -> Self {
let matches = <Self as IntoApp>::into_app().get_matches();
<Self as FromArgMatches>::from_arg_matches(&matches)
}

/// Parse from `std::env::args()`, return Err on error.
fn try_parse() -> StdResult<Self, Error> {
let matches = <Self as IntoApp>::into_app().try_get_matches()?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches))
}

/// Parse from iterator, exit on error
fn parse_from<I, T>(itr: I) -> Self
where
I: IntoIterator<Item = T>,
// TODO (@CreepySkeleton): discover a way to avoid cloning here
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().get_matches_from(itr);
<Self as FromArgMatches>::from_arg_matches(&matches)
}

/// Parse from `std::env::args()`, return Err on error.
fn try_parse_from<I, T>(itr: I) -> StdResult<Self, Error>
where
I: IntoIterator<Item = T>,
// TODO (@CreepySkeleton): discover a way to avoid cloning here
T: Into<OsString> + Clone,
{
let matches = <Self as IntoApp>::into_app().try_get_matches_from(itr)?;
Ok(<Self as FromArgMatches>::from_arg_matches(&matches))
}
}

/// Build an App according to the struct
///
/// Also serves for flattening
pub trait IntoApp: Sized {
/// @TODO @release @docs
fn into_app<'b>() -> App<'b>;
/// @TODO @release @docs
fn augment_clap<'b>(app: App<'b>) -> App<'b>;
}

/// Extract values from ArgMatches into the struct.
pub trait FromArgMatches: Sized {
/// @TODO @release @docs
fn from_arg_matches<'b>(matches: &ArgMatches) -> Self;
}

/// @TODO @release @docs
pub trait Subcommand: Sized {
/// @TODO @release @docs
fn from_subcommand<'b>(name: &str, matches: Option<&ArgMatches>) -> Option<Self>;
/// @TODO @release @docs
fn augment_subcommands<'b>(app: App<'b>) -> App<'b>;
}

/// @TODO @release @docs
pub trait ArgEnum {}

0 comments on commit 17f9eca

Please sign in to comment.