Expand description
Reads key-value pairs from a file, such as an .env file, and makes them easily accessible as environment variables. This crate provides a simpler and smaller alternative to dotenv, which is no longer maintained.
§Example
use stupid_simple_dotenv::to_env;
fn main() ->Result<(), Box<dyn std::error::Error>> {
match to_env(){
Ok(_) => println!("Success reading .env"),
Err(e) if e.kind == "io" =>{
println!("IO-Error better not ignore! {}", e);
// you can return the Error: return Err(e.into());
},
Err(e) if e.kind == "LinesError" => {
println!("Errors in some lines of .env: {}", e);
},
Err(e) => {
println!("Error {}", e);
// You can return the Error return Err(e.into());
},
};
let user = std::env::var("USER")?; // if USER is not set, this will return an error
println!("USER: {}", user);
Ok(())
}
Structs§
Functions§
- file_
to_ env - file_
to_ vec - Reads key value pairs from a file and returns a vector of tuples.
- get_or
- Try to get the value of an environment variable.
If the variable is not present in the environment,
default
is returned. - to_env
- Reads .env file stores the key value pairs as environment variables.
- to_vec
- Reads .env file to a vector of key value pairs tuples.