1
- #! [ allow ( missing_docs ) ]
1
+ /// Structure to deal with environment variables
2
2
#[ derive( Clone , Debug , PartialEq , Eq ) ]
3
3
pub struct Environment {
4
+ /// Customized environment variables
4
5
pub vars : Vec < ( String , String ) > ,
6
+ /// Define if the structure must inherit
5
7
pub inherit : bool ,
6
8
}
7
9
@@ -15,22 +17,58 @@ impl Default for Environment {
15
17
}
16
18
17
19
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
+ /// ```
18
32
pub fn inherit ( ) -> Self {
19
33
Self {
20
34
vars : vec ! [ ] ,
21
35
inherit : true ,
22
36
}
23
37
}
24
38
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
+ /// ```
25
49
pub fn empty ( ) -> Self {
26
50
Self :: default ( )
27
51
}
28
52
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
+ /// ```
30
63
pub fn insert < S1 : Into < String > , S2 : Into < String > > ( mut self , key : S1 , val : S2 ) -> Self {
31
64
self . vars . push ( ( key. into ( ) , val. into ( ) ) ) ;
32
65
self
33
66
}
67
+
68
+ /// Compile Environment object
69
+ pub fn compile ( & self ) -> Vec < ( String , String ) > {
70
+ :: std:: env:: vars ( ) . chain ( self . vars . clone ( ) ) . collect ( )
71
+ }
34
72
}
35
73
36
74
/// Implicit clone for ergonomics
@@ -40,23 +78,23 @@ impl<'a> From<&'a Environment> for Environment {
40
78
}
41
79
}
42
80
43
- pub trait FooIterItem {
44
- fn to_tuple ( & self ) -> ( String , String ) ;
81
+ pub trait EnvironmentItem {
82
+ fn to_environment_tuple ( & self ) -> ( String , String ) ;
45
83
}
46
84
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 ) {
49
87
( self . 0 . to_string ( ) , self . 1 . to_string ( ) )
50
88
}
51
89
}
52
90
53
91
impl < ' s , T > From < T > for Environment
54
- where T : IntoIterator , T :: Item : FooIterItem
92
+ where T : IntoIterator , T :: Item : EnvironmentItem
55
93
{
56
94
fn from ( v : T ) -> Self {
57
95
Self {
58
96
vars : v. into_iter ( )
59
- . map ( |k| k. to_tuple ( ) )
97
+ . map ( |k| k. to_environment_tuple ( ) )
60
98
. collect ( ) ,
61
99
inherit : false ,
62
100
}
0 commit comments