Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit 3fb8640

Browse files
committed
add docs and inherit
Signed-off-by: Freyskeyd <[email protected]>
1 parent b77b522 commit 3fb8640

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/environment.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#![allow(missing_docs)]
1+
/// Structure to deal with environment variables
22
#[derive(Clone, Debug, PartialEq, Eq)]
33
pub struct Environment {
4+
/// Customized environment variables
45
pub vars: Vec<(String, String)>,
6+
/// Define if the structure must inherit
57
pub inherit: bool,
68
}
79

@@ -15,22 +17,58 @@ impl Default for Environment {
1517
}
1618

1719
impl Environment {
20+
/// Create a new Environment object with inherit
21+
///
22+
/// # Examples
23+
///
24+
/// ```rust
25+
/// extern crate assert_cli;
26+
///
27+
/// let e = assert_cli::Environment::inherit().compile();
28+
/// let e_ = ::std::env::vars();
29+
///
30+
/// assert_eq!(e, e_);
31+
/// ```
1832
pub fn inherit() -> Self {
1933
Self {
2034
vars: vec![],
2135
inherit: true,
2236
}
2337
}
2438

39+
/// Create a new Environment object without inheriting
40+
///
41+
/// # Examples
42+
///
43+
/// ```rust
44+
/// extern crate assert_cli;
45+
///
46+
/// let e = assert_cli::Environment::empty().compile();
47+
/// assert_eq!(e, Vec::new());
48+
/// ```
2549
pub fn empty() -> Self {
2650
Self::default()
2751
}
2852

29-
// Since this is meant to act like a map, calling it `insert`
53+
/// Insert a new entry into the custom variables for this environment object
54+
///
55+
/// # Examples
56+
///
57+
/// ```rust
58+
/// extern crate assert_cli;
59+
///
60+
/// let e = assert_cli::Environment::empty().insert("boo", "bar").compile();
61+
/// assert_eq!(e, vec![("foo", "bar")];
62+
/// ```
3063
pub fn insert<S1: Into<String>, S2: Into<String>>(mut self, key: S1, val: S2) -> Self {
3164
self.vars.push((key.into(), val.into()));
3265
self
3366
}
67+
68+
/// Compile Environment object
69+
pub fn compile(&self) -> Vec<(String, String)> {
70+
::std::env::vars().chain(self.vars.clone()).collect()
71+
}
3472
}
3573

3674
/// Implicit clone for ergonomics
@@ -40,23 +78,23 @@ impl<'a> From<&'a Environment> for Environment {
4078
}
4179
}
4280

43-
pub trait FooIterItem {
44-
fn to_tuple(&self) -> (String, String);
81+
pub trait EnvironmentItem {
82+
fn to_environment_tuple(&self) -> (String, String);
4583
}
4684

47-
impl<'s, T: ToString, Z:ToString> FooIterItem for &'s (T, Z) {
48-
fn to_tuple(&self) -> (String, String) {
85+
impl<'s, T: ToString, Z:ToString> EnvironmentItem for &'s (T, Z) {
86+
fn to_environment_tuple(&self) -> (String, String) {
4987
(self.0.to_string(), self.1.to_string())
5088
}
5189
}
5290

5391
impl<'s, T> From<T> for Environment
54-
where T: IntoIterator, T::Item: FooIterItem
92+
where T: IntoIterator, T::Item: EnvironmentItem
5593
{
5694
fn from(v: T) -> Self {
5795
Self {
5896
vars: v.into_iter()
59-
.map(|k| k.to_tuple())
97+
.map(|k| k.to_environment_tuple())
6098
.collect(),
6199
inherit: false,
62100
}

0 commit comments

Comments
 (0)