Skip to content

Commit

Permalink
Merge pull request #5 from KevinMGranger/from_io_read
Browse files Browse the repository at this point in the history
Add `from_read` and `from_read_iter`.
  • Loading branch information
Allan authored Jul 18, 2022
2 parents f6c92fb + e7be06b commit 13ff292
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions dotenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod parse;
use std::env::{self, Vars};
use std::ffi::OsStr;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Once;

Expand Down Expand Up @@ -140,6 +141,48 @@ pub fn from_filename_iter<P: AsRef<Path>>(filename: P) -> Result<Iter<File>> {
Ok(iter)
}

/// Loads from any arbitrary [io::Read](std::io::Read).
///
/// Useful when you're reading dotenv info from something like IPC or the network.
///
/// If you're just opening a regular file, use [from_path] or [from_filename].
///
/// # Examples
/// ```no_run
/// # #![cfg(unix)]
/// use std::io::Read;
/// use std::os::unix::net::UnixStream;
///
/// let mut stream = UnixStream::connect("/some/socket").unwrap();
/// dotenvy::from_read(stream).unwrap();
/// ```
pub fn from_read<R: io::Read>(reader: R) -> Result<()> {
let iter = Iter::new(reader);
iter.load()?;
Ok(())
}

/// Like [from_read], but returns an iterator over variables instead of loading into environment.
///
/// # Examples
///
/// ```no_run
/// # #![cfg(unix)]
/// use std::io::Read;
/// use std::os::unix::net::UnixStream;
///
/// let mut stream = UnixStream::connect("/some/socket").unwrap();
/// let iter = dotenvy::from_read_iter(stream);
///
/// for item in iter {
/// let (key, val) = item.unwrap();
/// println!("{}={}", key, val);
/// }
/// ```
pub fn from_read_iter<R: io::Read>(reader: R) -> Iter<R> {
Iter::new(reader)
}

/// This is usually what you want.
/// It loads the .env file located in the environment's current directory or its parents in sequence.
///
Expand Down

0 comments on commit 13ff292

Please sign in to comment.