1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Clap Conf
//! =========
//!
//! Use this library to unify how you get configuration options from
//!
//! * The command line arguments
//! * Config Files
//! * Environmant Variables
//!
//!
//! Basic Usage
//! ----------
//!
//! ```
//! use clap_conf::prelude::*;
//!
//! let matches = clap_app!(my_app=>
//!     (@arg filename:-f +takes_value "the input filename")
//!     //...
//! ).get_matches();
//!
//! let cfg = with_toml_env(&matches,&["toml/config/locations"]);
//!
//! //the result must be a String as std::env::var has to return a String not a pointer
//! let filename =
//! cfg.grab().arg("filename").conf("input.filename").env("MY_APP_INPUT_FILE").def("default.file");
//!
//! //if the arguments were supplied this would return something else.
//! assert_eq!(filename,"default.file".to_string());
//!
//! ```

pub mod clapget;
pub mod convert;
pub mod env;
pub mod grabber;
pub mod prelude;
pub mod replace;
pub mod tomlget;

use crate::convert::Holder;
use crate::convert::Localizer;
use crate::replace::replace_env;
use std::path::PathBuf;

pub use clap::{clap_app, crate_version, ArgMatches, Values};

pub fn clap_env<'a, G: Getter<'a, &'a str>>(a: G) -> Holder<'a, env::Enver, G, String, &'a str> {
    //a.wrap(|v|v.to_string()).hold(env::Enver{})
    env::Enver {}.hold(a)
}

pub fn with_toml_env<'a, G, S, IT>(
    a: G,
    it: IT,
) -> Holder<'a, Holder<'a, env::Enver, G, String, &'a str>, Localizer<toml::Value>, String, String>
where
    G: Getter<'a, &'a str>,
    S: AsRef<str>,
    IT: IntoIterator<Item = S>,
{
    let tml = tomlget::load_first_toml(a.value("config", Filter::Arg), it)
        .unwrap_or(Localizer::new(toml::Value::Boolean(false), ""));
    env::Enver {}.hold(a).hold(tml)
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Filter {
    Conf,
    Arg,
    Env,
    Other(char),
}

pub trait Getter<'a, R>: Sized
where
    R: PartialEq + std::fmt::Debug + std::fmt::Display,
{
    type Iter: Iterator<Item = R>;
    fn value<S: AsRef<str>>(&self, s: S, f: Filter) -> Option<R>;
    fn values<S: AsRef<str>>(&self, s: S, f: Filter) -> Option<Self::Iter>;

    fn local_value<S: AsRef<str>>(&self, s: S, f: Filter) -> Option<PathBuf> {
        let v = self.value(s, f)?;
        let s = replace_env(&v.to_string()).ok()?;
        Some(PathBuf::from(s))
    }

    fn bool_flag<S: AsRef<str>>(&self, s: S, f: Filter) -> bool {
        self.value(s, f).is_some()
    }

    fn sub<S: AsRef<str>>(&self, _: S, _: Filter) -> bool {
        return false;
    }

    fn wrap<R2, F: Fn(R) -> R2>(self, f: F) -> convert::Wrapper<Self, F, R> {
        convert::Wrapper::new(self, f)
    }

    fn hold<B, RB>(self, b: B) -> convert::Holder<'a, Self, B, R, RB>
    where
        B: Getter<'a, RB>,
        R: std::convert::From<RB>,
        RB: PartialEq + std::fmt::Debug + std::fmt::Display,
        B::Iter: Iterator<Item = RB>,
    {
        convert::Holder::new(self, b)
    }

    fn grab(&'a self) -> grabber::Grabber<'a, Self, R, Self::Iter> {
        grabber::Grabber::new(self)
    }

    fn grab_local(&'a self) -> grabber::LocalGrabber<'a, Self, R, Self::Iter> {
        grabber::LocalGrabber::new(self)
    }

    fn grab_multi(&'a self) -> grabber::MultiGrabber<'a, Self, R> {
        grabber::MultiGrabber::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn try_holder() {
        let a = ArgMatches::new();
        let tml: toml::Value = "[a]\ncar=\"red\"".parse().unwrap();
        //        let tml2 = (&tml).wrap(|r| r.as_str().unwrap());
        let ce = clap_env(&a).hold(tml);
        //let e = env::Enver {};
        //let ce = e.hold(&a).hold(tml);

        assert_eq!(ce.value("ss", Filter::Arg), None);
        assert_eq!(
            ce.value("PWD", Filter::Env),
            Some("/home/matthew/scripts/rust/mlibs/clap_conf".to_string())
        );

        assert_eq!(
            ce.grab().env("PWD").done(),
            Some("/home/matthew/scripts/rust/mlibs/clap_conf".to_string())
        );

        assert_eq!(ce.grab().conf("a.car").done(), Some("red".to_string()));

        /* assert_eq!(
            g.env("PWD").done(),
            Some("/home/matthew/scripts/rust/mlibs/clap_conf")
        );
        */
    }

    #[test]
    fn test_grab() {
        let a = ArgMatches::new();
        let r = with_toml_env(&a, &["test_data/test1.toml"]);
        assert_eq!(r.grab().conf("a.b.c").done(), Some("hello".to_string()));
    }
}