@@ -17,10 +17,14 @@ pub enum Error {
1717 ParseEnvVarName { env_var_name : String } ,
1818}
1919
20+ /// Validated environment variable name
2021#[ derive( Clone , Debug , Default , Eq , Hash , Ord , PartialEq , PartialOrd ) ]
2122pub struct EnvVarName ( String ) ;
2223
2324impl EnvVarName {
25+ /// Creates an `EnvVarName` from the given string and panics if the validation failed
26+ ///
27+ /// Use this only with constant names that are also tested in unit tests!
2428 pub fn from_str_unsafe ( s : & str ) -> Self {
2529 EnvVarName :: from_str ( s) . expect ( "should be a valid environment variable name" )
2630 }
@@ -36,7 +40,7 @@ impl FromStr for EnvVarName {
3640 type Err = Error ;
3741
3842 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
39- // The length of the environment variable names seems not to be restricted.
43+ // The length of environment variable names seems not to be restricted.
4044
4145 if !s. is_empty ( ) && s. chars ( ) . all ( |c| matches ! ( c, ' ' ..='<' | '>' ..='~' ) ) {
4246 Ok ( Self ( s. to_owned ( ) ) )
@@ -48,24 +52,35 @@ impl FromStr for EnvVarName {
4852 }
4953}
5054
55+ /// A set of `EnvVar`s
56+ ///
57+ /// The environment variable names in the set are unique.
5158#[ derive( Clone , Debug , Default , PartialEq ) ]
5259pub struct EnvVarSet ( BTreeMap < EnvVarName , EnvVar > ) ;
5360
5461impl EnvVarSet {
62+ /// Creates an empty `EnvVarSet`
5563 pub fn new ( ) -> Self {
5664 Self :: default ( )
5765 }
5866
67+ /// Returns a reference to the `EnvVar` with the given name
5968 pub fn get ( & self , env_var_name : impl Into < EnvVarName > ) -> Option < & EnvVar > {
6069 self . 0 . get ( & env_var_name. into ( ) )
6170 }
6271
72+ /// Moves all `EnvVar`s from the given set into this one.
73+ ///
74+ /// `EnvVar`s with the same name are overridden.
6375 pub fn merge ( mut self , mut env_var_set : EnvVarSet ) -> Self {
6476 self . 0 . append ( & mut env_var_set. 0 ) ;
6577
6678 self
6779 }
6880
81+ /// Adds the given `EnvVar`s to this set
82+ ///
83+ /// `EnvVar`s with the same name are overridden.
6984 pub fn with_values < I , K , V > ( self , env_vars : I ) -> Self
7085 where
7186 I : IntoIterator < Item = ( K , V ) > ,
@@ -79,6 +94,9 @@ impl EnvVarSet {
7994 } )
8095 }
8196
97+ /// Adds an environment variable with the given name and string value to this set
98+ ///
99+ /// An `EnvVar` with the same name is overridden.
82100 pub fn with_value ( mut self , name : impl Into < EnvVarName > , value : impl Into < String > ) -> Self {
83101 let name: EnvVarName = name. into ( ) ;
84102
@@ -94,6 +112,9 @@ impl EnvVarSet {
94112 self
95113 }
96114
115+ /// Adds an environment variable with the given name and field path to this set
116+ ///
117+ /// An `EnvVar` with the same name is overridden.
97118 pub fn with_field_path (
98119 mut self ,
99120 name : impl Into < EnvVarName > ,
0 commit comments