diff --git a/examples/find/src/main.rs b/examples/find/src/main.rs index 90b0e1ea..abc31cf4 100644 --- a/examples/find/src/main.rs +++ b/examples/find/src/main.rs @@ -1,4 +1,5 @@ //! This example shows finding an env file by filename. + use dotenvy::EnvLoader; use std::{ env, error, fs, io, diff --git a/examples/modify-macro/src/main.rs b/examples/modify-macro/src/main.rs index ef1479ca..d26451c7 100644 --- a/examples/modify-macro/src/main.rs +++ b/examples/modify-macro/src/main.rs @@ -1,7 +1,10 @@ -use std::{env, error}; +//! The `load` attribute loads and modifies the environment. +//! +//! This is more ergonomic than the *modify* example. + +use std::env; #[dotenvy::load(path = "../env-example", required = true, override_ = true)] -fn main() -> Result<(), Box> { - println!("HOST={}", env::var("HOST")?); - Ok(()) +fn main() { + println!("HOST={}", env::var("HOST").unwrap()); } diff --git a/examples/modify-tokio-macro/src/main.rs b/examples/modify-tokio-macro/src/main.rs index 6ab8c181..c04b68fb 100644 --- a/examples/modify-tokio-macro/src/main.rs +++ b/examples/modify-tokio-macro/src/main.rs @@ -1,10 +1,12 @@ -//! `#[dotenvy::load]` must go before `#[tokio::main]`. +//! This is more ergonomic than the *modify-tokio* example. +//! +//! The attribute macro executes `load_and_modify` before the tokio runtime is spawned. +//! When using this method, `#[dotenvy::load]` be put above `#[tokio::main]`. -use std::{env, error}; +use std::env; #[dotenvy::load(path = "../env-example")] #[tokio::main] -async fn main() -> Result<(), Box> { - println!("HOST={}", env::var("HOST")?); - Ok(()) +async fn main() { + println!("HOST={}", env::var("HOST").unwrap()); } diff --git a/examples/modify-tokio/src/main.rs b/examples/modify-tokio/src/main.rs index d496ecb4..58e637ed 100644 --- a/examples/modify-tokio/src/main.rs +++ b/examples/modify-tokio/src/main.rs @@ -1,12 +1,16 @@ +//! `load_and_modify` uses `std::env::set_var` internally, which is not thread-safe. +//! +//! When modifying the environment, loading must be executed before the async runtime is spawned. +//! +//! The *modify-tokio-macro* example contains a more ergonomic way to do this. + use dotenvy::EnvLoader; use std::{ env::{self, VarError}, error, }; -// `load_and_modify` uses `std::env::set_var` internally, which is not thread-safe. -// As such, loading must be done before the async runtime is spawned. -// This is why we don't use `#[tokio::main]` here. + fn main() -> Result<(), Box> { let loader = EnvLoader::with_path("../env-example"); unsafe { loader.load_and_modify() }?; @@ -16,7 +20,7 @@ fn main() -> Result<(), Box> { .enable_all() .build()? .block_on(async { - println!("HOST={}", env::var("HOST")?); + println!("HOST={}", env::var("HOST").unwrap()); Ok::<_, VarError>(()) })?; diff --git a/examples/modify/src/main.rs b/examples/modify/src/main.rs index 78ff39f9..0525f36c 100644 --- a/examples/modify/src/main.rs +++ b/examples/modify/src/main.rs @@ -1,6 +1,9 @@ //! This example modifies the existing environment. //! //! This makes environment varaibles from available to subprocesses, e.g., a Python script. +//! +//! The *modify-macro* example contains a more ergonomic way to do this. + use dotenvy::{EnvLoader, EnvSequence}; use std::{env, error, fs, io, process::Command}; @@ -9,13 +12,14 @@ fn main() -> Result<(), Box> { let loader = EnvLoader::with_path("../env-example").sequence(EnvSequence::InputThenEnv); unsafe { loader.load_and_modify() }?; - println!("HOST={}", env::var("HOST")?); + println!("HOST={}", env::var("HOST").unwrap()); print_host_py()?; Ok(()) } +/// Prints the host using Python's `os.environ.get`. fn print_host_py() -> io::Result<()> { - let script = fs::read_to_string("print_host.py")?; + let script = fs::read_to_string("../modify/print_host.py")?; let output = Command::new("python3").arg("-c").arg(script).output()?; print!("{}", String::from_utf8_lossy(&output.stdout)); Ok(()) diff --git a/examples/multiple-files/src/main.rs b/examples/multiple-files/src/main.rs index b993cbb0..7e69aee1 100644 --- a/examples/multiple-files/src/main.rs +++ b/examples/multiple-files/src/main.rs @@ -2,11 +2,14 @@ use dotenvy::{EnvLoader, EnvSequence}; use std::error; fn main() -> Result<(), Box> { + // The sequence is `EnvThenInput` to load the program environment and file A. let map_a = EnvLoader::with_path("../env-example") .sequence(EnvSequence::EnvThenInput) .load()?; + + // the sequence is `InputOnly` as we aleady loaded the program environment in the previous step. let map_b = EnvLoader::with_path("../env-example-2") - .sequence(EnvSequence::InputOnly) // we already loaded from the environment in map_a + .sequence(EnvSequence::InputOnly) .load()?; let mut env_map = map_a.clone(); diff --git a/examples/optional/src/main.rs b/examples/optional/src/main.rs index 545d2877..c96347a4 100644 --- a/examples/optional/src/main.rs +++ b/examples/optional/src/main.rs @@ -7,8 +7,9 @@ use std::{error, fs::File, io, path::Path}; fn main() -> Result<(), Box> { let path = Path::new("non-existent-env"); - // Rather than checking with `Path::exists` and then opening the file handle, we call `File::open` directly to avoid a race condition where the file is inaccessible between the exist check and open - // Even though we pass the file handle as input, we specify the path so it can be used as context in the error message + // Rather than checking with `Path::exists` and then opening the file handle, we call `File::open` directly to avoid a race condition where the file is inaccessible between the exist check and the open call. + + // The loader is unaware of the file path because we construct it using a reader. We can still inform the reader of the file path using the `path` setter, which allows us to have a more informative error message. let loader = match File::open(path) { Ok(file) => EnvLoader::with_reader(file) .path(path)