structopt_toml/
lib.rs

1//! `structopt-toml` is an default value loader from TOML for structopt.
2//!
3//! ## Examples
4//!
5//! The following example show a quick example of `structopt-toml`.
6//!
7//! If `derive(Deserialize)`, `derive(StructOptToml)` and `serde(default)` are added to the struct
8//! with `derive(StructOpt)`, some functions like `from_args_with_toml` can be used.
9//!
10//! ```
11//! use serde_derive::Deserialize;
12//! use structopt::StructOpt;
13//! use structopt_toml::StructOptToml;
14//!
15//! #[derive(Debug, Deserialize, StructOpt, StructOptToml)]
16//! #[serde(default)]
17//! struct Opt {
18//!     #[structopt(default_value = "0", short = "a")] a: i32,
19//!     #[structopt(default_value = "0", short = "b")] b: i32,
20//! }
21//!
22//! let toml_str = r#"
23//!     a = 10
24//! "#;
25//! let opt = Opt::from_args_with_toml(toml_str).expect("toml parse failed");
26//! println!("a:{}", opt.a);
27//! println!("b:{}", opt.b);
28//! ```
29//!
30//! The execution result of the above example is below.
31//!
32//! ```ignore
33//! $ ./example
34//! a:10        // value from TOML string
35//! b:0         // value from default_value of structopt
36//!
37//! $ ./example -a 20
38//! a:20        // value from command line argument
39//! b:0
40//! ```
41
42extern crate anyhow;
43extern crate clap as _clap;
44extern crate serde as _serde;
45extern crate structopt as _structopt;
46extern crate toml as _toml;
47
48#[allow(unused_imports)]
49#[macro_use]
50extern crate structopt_toml_derive;
51
52#[doc(hidden)]
53pub use structopt_toml_derive::*;
54
55use std::ffi::OsString;
56
57/// Re-export of clap
58pub mod clap {
59    pub use _clap::*;
60}
61/// Re-export of serde
62pub mod serde {
63    pub use _serde::*;
64}
65/// Re-export of structopt
66pub mod structopt {
67    pub use _structopt::*;
68}
69
70pub trait StructOptToml {
71    /// Merge the struct from TOML and the struct from args
72    fn merge<'a>(from_toml: Self, from_args: Self, args: &_clap::ArgMatches) -> Self
73    where
74        Self: Sized,
75        Self: _structopt::StructOpt,
76        Self: _serde::de::Deserialize<'a>;
77
78    /// Creates the struct from `clap::ArgMatches` with initial values from TOML.
79    fn from_clap_with_toml<'a>(
80        toml_str: &'a str,
81        args: &_clap::ArgMatches,
82    ) -> Result<Self, anyhow::Error>
83    where
84        Self: Sized,
85        Self: _structopt::StructOpt,
86        Self: _serde::de::Deserialize<'a>,
87    {
88        let from_args: Self = _structopt::StructOpt::from_clap(args);
89        let from_toml: Self = _toml::from_str(toml_str)?;
90        Ok(Self::merge(from_toml, from_args, args))
91    }
92
93    /// Creates the struct from command line arguments with initial values from TOML.
94    fn from_args_with_toml<'a>(toml_str: &'a str) -> Result<Self, anyhow::Error>
95    where
96        Self: Sized,
97        Self: _structopt::StructOpt,
98        Self: _serde::de::Deserialize<'a>,
99    {
100        let clap = Self::clap();
101        let args = clap.get_matches();
102        Self::from_clap_with_toml(toml_str, &args)
103    }
104
105    /// Creates the struct from iterator with initial values from TOML.
106    fn from_iter_with_toml<'a, I>(toml_str: &'a str, iter: I) -> Result<Self, anyhow::Error>
107    where
108        Self: Sized,
109        Self: _structopt::StructOpt,
110        Self: _serde::de::Deserialize<'a>,
111        I: IntoIterator,
112        I::Item: Into<OsString> + Clone,
113    {
114        let clap = Self::clap();
115        let args = clap.get_matches_from(iter);
116        Self::from_clap_with_toml(toml_str, &args)
117    }
118}