This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
estring/examples/dotenv.rs

27 lines
667 B
Rust
Raw Permalink Normal View History

2022-07-25 22:54:57 +03:00
use estring::{EString, Pair, SepVec, Trim};
2022-07-24 11:12:22 +03:00
2022-07-25 00:16:22 +03:00
const DOTENV_CONTENT: &str = "
2022-07-24 11:12:22 +03:00
DATABASE_URL=postgres://user:password@localhost:5432/recipes
2022-07-25 00:16:22 +03:00
APP_HOST=http://localhost:3000
";
2022-07-24 11:12:22 +03:00
fn main() -> Result<(), estring::ParseError> {
EString::from(DOTENV_CONTENT)
2022-07-25 00:16:22 +03:00
.parse::<Trim<SepVec<Pair<&str, '=', &str>, '\n'>>>()?
2022-07-24 11:12:22 +03:00
.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(())
}