Skip to content

Commit

Permalink
More documentation in examples
Browse files Browse the repository at this point in the history
This adds more description in the examples.
We also use `unwrap` sometimes when calling `env::var` for simplicity.
  • Loading branch information
allan2 committed Oct 5, 2024
1 parent 9a9dd35 commit f040116
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/find/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This example shows finding an env file by filename.

use dotenvy::EnvLoader;
use std::{
env, error, fs, io,
Expand Down
11 changes: 7 additions & 4 deletions examples/modify-macro/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn error::Error>> {
println!("HOST={}", env::var("HOST")?);
Ok(())
fn main() {
println!("HOST={}", env::var("HOST").unwrap());
}
12 changes: 7 additions & 5 deletions examples/modify-tokio-macro/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn error::Error>> {
println!("HOST={}", env::var("HOST")?);
Ok(())
async fn main() {
println!("HOST={}", env::var("HOST").unwrap());
}
12 changes: 8 additions & 4 deletions examples/modify-tokio/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn error::Error>> {
let loader = EnvLoader::with_path("../env-example");
unsafe { loader.load_and_modify() }?;
Expand All @@ -16,7 +20,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.enable_all()
.build()?
.block_on(async {
println!("HOST={}", env::var("HOST")?);
println!("HOST={}", env::var("HOST").unwrap());
Ok::<_, VarError>(())
})?;

Expand Down
8 changes: 6 additions & 2 deletions examples/modify/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -9,13 +12,14 @@ fn main() -> Result<(), Box<dyn error::Error>> {
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(())
Expand Down
5 changes: 4 additions & 1 deletion examples/multiple-files/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use dotenvy::{EnvLoader, EnvSequence};
use std::error;

fn main() -> Result<(), Box<dyn error::Error>> {
// 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();
Expand Down
5 changes: 3 additions & 2 deletions examples/optional/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::{error, fs::File, io, path::Path};
fn main() -> Result<(), Box<dyn error::Error>> {
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)
Expand Down

0 comments on commit f040116

Please sign in to comment.