fix examples
This commit is contained in:
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "clappconfig"
|
name = "clappconfig"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
|
authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@@ -20,3 +20,7 @@ serde = "1.0"
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clap = "2.33"
|
clap = "2.33"
|
||||||
json5 = "0.2"
|
json5 = "0.2"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
smart-default = "0.6"
|
||||||
|
serde_derive = "1.0"
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ Supports custom `clap` arguments as well.
|
|||||||
- log level can be overridden by the `--log` CLI flag
|
- log level can be overridden by the `--log` CLI flag
|
||||||
- log level can be increased by repeated use of `-v` or `--verbose`
|
- log level can be increased by repeated use of `-v` or `--verbose`
|
||||||
|
|
||||||
|
## Re-exports
|
||||||
|
|
||||||
|
The crates re-exports crates used in public API: `clap`, `anyhow`, `log`.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
See the examples directory.
|
See the examples directory.
|
||||||
|
|||||||
+2
-3
@@ -6,7 +6,6 @@ extern crate serde_derive;
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
use clappconfig::AppConfig; // Must be in scope so the trait methods may be used
|
use clappconfig::AppConfig; // Must be in scope so the trait methods may be used
|
||||||
use failure::Fallible;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@@ -25,12 +24,12 @@ impl AppConfig for Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Finalize config (this is called inside `init()`)
|
/// Finalize config (this is called inside `init()`)
|
||||||
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> Fallible<Self::Init> {
|
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result<Self::Init> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Fallible<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let cfg = Config::init("Foo Example", "examples/foo.json", None)?;
|
let cfg = Config::init("Foo Example", "examples/foo.json", None)?;
|
||||||
|
|
||||||
trace!("Trace message");
|
trace!("Trace message");
|
||||||
|
|||||||
+6
-11
@@ -3,13 +3,10 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate failure;
|
pub extern crate log;
|
||||||
#[macro_use]
|
|
||||||
extern crate log;
|
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use clappconfig::AppConfig;
|
use clappconfig::AppConfig;
|
||||||
use failure::Fallible;
|
|
||||||
use smart_default::SmartDefault;
|
use smart_default::SmartDefault;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
@@ -105,14 +102,14 @@ impl AppConfig for Config {
|
|||||||
///
|
///
|
||||||
/// This can also be solved using `#[serde(skip)]` on the field, but sometimes that is not
|
/// This can also be solved using `#[serde(skip)]` on the field, but sometimes that is not
|
||||||
/// possible.
|
/// possible.
|
||||||
fn configure<'a>(mut self, clap: &ArgMatches<'a>) -> Fallible<Self::Init> {
|
fn configure<'a>(mut self, clap: &ArgMatches<'a>) -> anyhow::Result<Self::Init> {
|
||||||
let input = clap.value_of("input").unwrap().to_string();
|
let input = clap.value_of("input").unwrap().to_string();
|
||||||
|
|
||||||
// Logging is initialized now - feel free to use it
|
// Logging is initialized now - feel free to use it
|
||||||
debug!("Input: {}", input);
|
debug!("Input: {}", input);
|
||||||
|
|
||||||
if input.is_empty() {
|
if input.is_empty() {
|
||||||
bail!("Input is required!");
|
anyhow::bail!("Input is required!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// `?` can be used to abort
|
// `?` can be used to abort
|
||||||
@@ -121,7 +118,7 @@ impl AppConfig for Config {
|
|||||||
debug!("Input path: {}", pb.display());
|
debug!("Input path: {}", pb.display());
|
||||||
|
|
||||||
if !pb.is_file() {
|
if !pb.is_file() {
|
||||||
return Err(format_err!("Input is not a file!"));
|
anyhow::bail!("Input is not a file!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set offset by arg
|
// Set offset by arg
|
||||||
@@ -144,7 +141,7 @@ impl AppConfig for Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Fallible<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
// Using custom version (default is CARGO_PKG_VERSION)
|
// Using custom version (default is CARGO_PKG_VERSION)
|
||||||
let version = format!(
|
let version = format!(
|
||||||
"{} by {}",
|
"{} by {}",
|
||||||
@@ -156,9 +153,7 @@ fn main() -> Fallible<()> {
|
|||||||
|
|
||||||
// rot13
|
// rot13
|
||||||
|
|
||||||
let mut f = OpenOptions::new()
|
let mut f = OpenOptions::new().read(true).open(main.input_file)?;
|
||||||
.read(true)
|
|
||||||
.open(main.input_file)?;
|
|
||||||
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
f.read_to_string(&mut buf)?;
|
f.read_to_string(&mut buf)?;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
// re-export libs appearing in public API
|
// re-export libs appearing in public API
|
||||||
|
pub use anyhow;
|
||||||
pub use clap;
|
pub use clap;
|
||||||
|
|
||||||
pub const LOG_LEVELS: [&str; 6] = ["off", "error", "warn", "info", "debug", "trace"];
|
pub const LOG_LEVELS: [&str; 6] = ["off", "error", "warn", "info", "debug", "trace"];
|
||||||
|
|||||||
Reference in New Issue
Block a user