Skip to content

Commit

Permalink
add clapme feature to create a ClapMe impl (for command-line arguments)
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Jul 9, 2018
1 parent b4c2924 commit 4dcaadc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ Never again should you need to specify units in a comment!"""
default = []
oibit = []
spec = []
test = ["quickcheck", "approx", "oibit", "spec", "serde", "serde_test"]
test = ["quickcheck", "approx", "oibit", "spec", "clapme", "serde", "serde_test"]

[dependencies]
approx = { version = "0.1.1", optional = true, features = [] }
generic-array = "0.5.1"
quickcheck = { version = "0.6.2", optional = true }
clapme = { version = "0.1.1", optional = true }
serde = { version = "1.0.0", optional = true }
serde_test = { version = "1.0.0", optional = true }
typenum = "1.6.0"
34 changes: 33 additions & 1 deletion src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ pub mod {} {{
#[cfg(feature = \"serde\")]
impl_serde!({});
#[cfg(feature = \"clapme\")]
impl_clapme!({});
pub use self::f64consts::*;
",
self.fmt, self.name
self.fmt, self.name, self.name
)?;

write!(
Expand Down Expand Up @@ -259,6 +261,36 @@ pub mod {} {{
f,
"
}}
"
)?;


write!(
f,
"
/// Test that clapme can generate a help message, and can produce a value.
#[cfg(feature = \"clapme\")]
#[test]
fn test_{}_clapme() {{
",
self.module
)?;
for base in &self.base {
write!(
f,
"
let value = 3.0 * {};
assert_eq!(value,
<{}<f64> as ClapMe>::from_iter(&[\"test\", \"3.0\"]).unwrap());
",
base.constant,
base.name
)?;
}
write!(
f,
"
}}
}}"
)?;

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ crate. Pretty much everything else is for ergonomics.
html_favicon_url = "https://raw.githubusercontent.com/paholg/dimensioned/master/favicon.png",
html_root_url = "http://paholg.com/dimensioned"
)]
#![no_std]
#![cfg_attr(not(feature="clapme"), no_std)]
#![warn(missing_docs)]
#![cfg_attr(feature = "oibit", feature(optin_builtin_traits))]
#![cfg_attr(feature = "spec", feature(specialization))]
Expand All @@ -116,6 +116,12 @@ crate. Pretty much everything else is for ergonomics.
feature = "cargo-clippy", allow(type_complexity, float_cmp, useless_attribute, doc_markdown)
)]

#[cfg(feature="clapme")]
extern crate core;
#[cfg(feature="clapme")]
extern crate clapme;
#[cfg(feature="clapme")]

// Macro debugging
// #![feature(trace_macros)]
// trace_macros!(true);
Expand Down
40 changes: 40 additions & 0 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,46 @@ macro_rules! impl_serde {
};
}


/// Implement ClapMe trait for a unit system.
///
/// The implementations generated by this macro only serialize the
/// numeric values - not the actual units. Therefore, serialization is
/// dimensionally unsafe, but it does not add any overhead over using
/// plain numeric types.
///
/// All of the unit systems defined in this crate implement
/// `Serialize` and `Deserialize` using this macro. If you define your
/// own system you may use this macro to implement those traits
/// automatically, or define them yourself:
///
/// ```rust,ignore
/// impl_clapme!(UnitSystem);
/// ```
#[cfg(feature = "clapme")]
#[macro_export]
macro_rules! impl_clapme {
($System:ident) => {
use $crate::clapme::{ClapMe};

impl<V: ClapMe,U> ClapMe for $System<V,U> {
fn with_clap<T>(info: ::clapme::ArgInfo, app: ::clapme::clap::App,
f: impl FnOnce(::clapme::clap::App) -> T) -> T {
V::with_clap(info, app, f)
}
fn from_clap(name: &str, matches: &::clapme::clap::ArgMatches) -> Option<Self> {
V::from_clap(name, matches).map(|v| $System {
value_unsafe: v,
_marker: marker::PhantomData,
})
}
fn requires_flags(name: &str) -> Vec<String> {
V::requires_flags(name)
}
}
};
}

// tests to see if we can rewrite derived! macro better
// #[test]
// fn derived2_test() {
Expand Down

0 comments on commit 4dcaadc

Please sign in to comment.