1
+ use std:: ffi:: OsString ;
1
2
/// Structure to deal with environment variables
2
3
#[ derive( Clone , Debug , PartialEq , Eq ) ]
3
4
pub struct Environment {
4
5
/// Customized environment variables
5
- vars : Vec < ( String , String ) > ,
6
+ vars : Vec < ( OsString , OsString ) > ,
6
7
/// Define if the structure must inherit
7
8
inherit : bool ,
8
9
}
@@ -17,15 +18,17 @@ impl Default for Environment {
17
18
}
18
19
19
20
impl Environment {
20
- /// Create a new Environment object with inherit
21
+ /// Create a new Environment that inherits this process' environment.
21
22
///
22
23
/// # Examples
23
24
///
24
25
/// ```rust
25
26
/// extern crate assert_cli;
26
27
///
28
+ /// use std::ffi::OsString;
29
+ ///
27
30
/// let e = assert_cli::Environment::inherit().compile();
28
- /// let e_: Vec<(String, String )> = ::std::env::vars ().collect();
31
+ /// let e_: Vec<(OsString, OsString )> = ::std::env::vars_os ().collect();
29
32
///
30
33
/// assert_eq!(e, e_);
31
34
/// ```
@@ -36,7 +39,7 @@ impl Environment {
36
39
}
37
40
}
38
41
39
- /// Create a new Environment object without inheriting
42
+ /// Create a new Environment independent of the current process's Environment
40
43
///
41
44
/// # Examples
42
45
///
@@ -57,18 +60,20 @@ impl Environment {
57
60
/// ```rust
58
61
/// extern crate assert_cli;
59
62
///
63
+ /// use std::ffi::OsString;
64
+ ///
60
65
/// let e = assert_cli::Environment::empty().insert("foo", "bar").compile();
61
- /// assert_eq!(e, vec![("foo".to_string( ), "bar".to_string( ))]);
66
+ /// assert_eq!(e, vec![(OsString::from( "foo"), OsString::from( "bar"))]);
62
67
/// ```
63
- pub fn insert < S1 : Into < String > , S2 : Into < String > > ( mut self , key : S1 , val : S2 ) -> Self {
68
+ pub fn insert < S1 : Into < OsString > , S2 : Into < OsString > > ( mut self , key : S1 , val : S2 ) -> Self {
64
69
self . vars . push ( ( key. into ( ) , val. into ( ) ) ) ;
65
70
self
66
71
}
67
72
68
73
/// Compile Environment object
69
- pub fn compile ( self ) -> Vec < ( String , String ) > {
74
+ pub fn compile ( self ) -> Vec < ( OsString , OsString ) > {
70
75
if self . inherit {
71
- :: std:: env:: vars ( ) . chain ( self . vars ) . collect ( )
76
+ :: std:: env:: vars_os ( ) . chain ( self . vars ) . collect ( )
72
77
} else {
73
78
self . vars
74
79
}
@@ -83,12 +88,15 @@ impl<'a> From<&'a Environment> for Environment {
83
88
}
84
89
85
90
pub trait EnvironmentItem {
86
- fn to_environment_tuple ( & self ) -> ( String , String ) ;
91
+ fn to_environment_tuple ( & self ) -> ( OsString , OsString ) ;
87
92
}
88
93
89
94
impl < ' s , T : ToString , Z : ToString > EnvironmentItem for & ' s ( T , Z ) {
90
- fn to_environment_tuple ( & self ) -> ( String , String ) {
91
- ( self . 0 . to_string ( ) , self . 1 . to_string ( ) )
95
+ fn to_environment_tuple ( & self ) -> ( OsString , OsString ) {
96
+ (
97
+ OsString :: from ( self . 0 . to_string ( ) ) ,
98
+ OsString :: from ( self . 1 . to_string ( ) ) ,
99
+ )
92
100
}
93
101
}
94
102
@@ -128,7 +136,10 @@ mod test {
128
136
129
137
let y = y. insert ( "key" , "value" ) ;
130
138
131
- assert_eq ! ( y. compile( ) , vec![ ( "key" . to_string( ) , "value" . to_string( ) ) ] ) ;
139
+ assert_eq ! (
140
+ y. compile( ) ,
141
+ vec![ ( OsString :: from( "key" ) , OsString :: from( "value" ) ) ]
142
+ ) ;
132
143
}
133
144
134
145
#[ test]
@@ -154,7 +165,7 @@ mod test {
154
165
155
166
assert_eq ! (
156
167
y. clone( ) . insert( "key" , "value" ) . compile( ) ,
157
- vec![ ( "key" . to_string ( ) , "value" . to_string ( ) ) ]
168
+ vec![ ( OsString :: from ( "key" ) , OsString :: from ( "value" ) ) ]
158
169
) ;
159
170
160
171
assert ! (
0 commit comments