@@ -23,6 +23,9 @@ pub struct Environment {
23
23
24
24
/// Ignore empty env values (treat as unset).
25
25
ignore_empty : bool ,
26
+
27
+ /// Parses booleans, integers and floats if they're detected (can be safely parsed).
28
+ try_parsing : bool ,
26
29
}
27
30
28
31
impl Environment {
@@ -51,6 +54,13 @@ impl Environment {
51
54
self . ignore_empty = ignore;
52
55
self
53
56
}
57
+
58
+ /// Note: enabling `try_parsing` can reduce performance it will try and parse
59
+ /// each environment variable 3 times (bool, i64, f64)
60
+ pub fn try_parsing ( mut self , try_parsing : bool ) -> Self {
61
+ self . try_parsing = try_parsing;
62
+ self
63
+ }
54
64
}
55
65
56
66
impl Default for Environment {
@@ -59,6 +69,7 @@ impl Default for Environment {
59
69
prefix : None ,
60
70
separator : None ,
61
71
ignore_empty : false ,
72
+ try_parsing : false ,
62
73
}
63
74
}
64
75
}
@@ -115,10 +126,22 @@ impl Source for Environment {
115
126
key = key. replace ( separator, "." ) ;
116
127
}
117
128
118
- m. insert (
119
- key. to_lowercase ( ) ,
120
- Value :: new ( Some ( & uri) , ValueKind :: String ( value) ) ,
121
- ) ;
129
+ let value = if self . try_parsing {
130
+ // convert to lowercase because bool parsing expects all lowercase
131
+ if let Ok ( parsed) = value. to_lowercase ( ) . parse :: < bool > ( ) {
132
+ ValueKind :: Boolean ( parsed)
133
+ } else if let Ok ( parsed) = value. parse :: < i64 > ( ) {
134
+ ValueKind :: Integer ( parsed)
135
+ } else if let Ok ( parsed) = value. parse :: < f64 > ( ) {
136
+ ValueKind :: Float ( parsed)
137
+ } else {
138
+ ValueKind :: String ( value)
139
+ }
140
+ } else {
141
+ ValueKind :: String ( value)
142
+ } ;
143
+
144
+ m. insert ( key. to_lowercase ( ) , Value :: new ( Some ( & uri) , value) ) ;
122
145
}
123
146
124
147
Ok ( m)
0 commit comments