From 09fcbc4d9870df84a0d6e5b2a584485494b866b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 10 May 2020 22:58:38 +0200 Subject: [PATCH] fix API to not report own crate version if None given --- Cargo.toml | 2 +- examples/minimal.rs | 6 +++++- examples/rotn.rs | 2 +- src/lib.rs | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 267e000..8480b5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clappconfig" -version = "0.3.1" +version = "0.4.0" authors = ["Ondřej Hruška "] edition = "2018" license = "MIT" diff --git a/examples/minimal.rs b/examples/minimal.rs index 584e9a5..1050554 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -30,7 +30,11 @@ impl AppConfig for Config { } fn main() -> anyhow::Result<()> { - let cfg = Config::init("Foo Example", "examples/foo.json", None)?; + let cfg = Config::init( + "Foo Example", + "examples/foo.json", + env!("CARGO_PKG_VERSION"), + )?; trace!("Trace message"); debug!("Debug message"); diff --git a/examples/rotn.rs b/examples/rotn.rs index 3f12167..7dd361f 100644 --- a/examples/rotn.rs +++ b/examples/rotn.rs @@ -149,7 +149,7 @@ fn main() -> anyhow::Result<()> { env!("CARGO_PKG_AUTHORS") ); - let main = Config::init("Rot-N", "rotn_config.json", Some(&version))?; + let main = Config::init("Rot-N", "rotn_config.json", &version)?; // rot13 diff --git a/src/lib.rs b/src/lib.rs index 3f1f84c..eda76f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,8 +62,9 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result; /// Initialize the app - fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> anyhow::Result { - let version = version.unwrap_or_else(|| env!("CARGO_PKG_VERSION")); + /// + /// Use `env!("CARGO_PKG_VERSION")` to get a version string from Cargo.toml + fn init(name: &str, cfg_file_name: &str, version: &str) -> anyhow::Result { let clap = clap::App::new(name) .arg( clap::Arg::with_name("config")