example: add fun dotenv example
This commit is contained in:
parent
c099d860bd
commit
303da5659e
2 changed files with 29 additions and 0 deletions
|
@ -35,3 +35,7 @@ maintenance = { status = "actively-developed" }
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "calc"
|
name = "calc"
|
||||||
required-features = ["vec", "number"]
|
required-features = ["vec", "number"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "dotenv"
|
||||||
|
required-features = ["vec", "tuple"]
|
||||||
|
|
25
examples/dotenv.rs
Normal file
25
examples/dotenv.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use estring::{EString, Pair, SepVec};
|
||||||
|
|
||||||
|
const DOTENV_CONTENT: &str = "\
|
||||||
|
DATABASE_URL=postgres://user:password@localhost:5432/recipes
|
||||||
|
APP_HOST=http://localhost:3000";
|
||||||
|
|
||||||
|
fn main() -> Result<(), estring::ParseError> {
|
||||||
|
EString::from(DOTENV_CONTENT)
|
||||||
|
.parse::<SepVec<Pair<&str, '=', &str>, '\n'>>()?
|
||||||
|
.iter()
|
||||||
|
.for_each(|p @ Pair(key, value)| {
|
||||||
|
println!("pair: {}", p);
|
||||||
|
|
||||||
|
std::env::set_var(key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"envs: {:#?}",
|
||||||
|
std::env::vars()
|
||||||
|
.filter(|(k, ..)| ["DATABASE_URL", "APP_HOST"].contains(&k.as_str()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Reference in a new issue