[][src]Struct env_plus::EnvLoader

pub struct EnvLoader { /* fields omitted */ }

The entry point of the library

The EnvLoader uses few default values which are listed below

  • A file - '.env_plus' which is relative to the current directory. You can specify your own file with fn change_file
  • A comment style - '//' which makes the program ignore everything after it. By putting it on the beginning of the line, the whole line is marked as a comment
  • Delimiter - '=' once placed in each line, everything on the left is marked as a key and on the right as value (only the first = is used). Change with fn change_delimiter
  • Overwrite - bool (default false) which specifies if the already exisiting ENVs will be replaced or not if you have a var with the same name. Change with fn overwrite_envs

Implementations

impl EnvLoader[src]

pub fn new() -> EnvLoader[src]

Create a new EnvLoader instance.

Examples

// .env_plus
 
// Double slash is used for commenting, equal sign is used for assiging a value by default.
SECRET=YOUR_SECRET
// main.rs
use env_plus::EnvLoader;
 
fn main() {
    EnvLoader::new()
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

pub fn change_file(mut self: Self, path: String) -> Self[src]

Changes the file which will be used to parse ENVs from. Any file can be used as long as you set its comment style and delimiter.

Examples

// my_special_file.extension
 
SECRET=YOUR_SECRET
// main.rs
use env_plus::EnvLoader;
 
fn main() {
    EnvLoader::new()
    .change_file(String::from("./my_special_file.extension"))
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
 
    // YOUR_SECRET will be in your special file which we loaded above. 
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

pub fn change_comment(mut self: Self, comment: String) -> Self[src]

Sets a new value to be marked as a comment in the file and not be loaded.

Examples

// .env_plus 
 
--This is a now a comment
SECRET=YOUR_SECRET
// main.rs
use env_plus::EnvLoader;
 
fn main() {
    EnvLoader::new()
    .change_comment(String::from("--"))
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

pub fn change_delimiter(mut self: Self, delimiter: String) -> Self[src]

Change the delimiter that will be used to parse the file lines. The default delimiter is =

Examples

// .env_plus
 
SECRET===YOUR_SECRET
// main.rs 
use env_plus::EnvLoader;
 
fn main() {
    EnvLoader::new()
    .change_delimiter(String::from("==="))
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

pub fn overwrite_envs(mut self: Self, overwrite: bool) -> Self[src]

If true is passed, all current ENV vars that have the same names as the ones in the file will be overwritten, otherwise they won't.

Examples

// .env_plus
 
SECRET=YOUR_SECRET
// main.rs
use env_plus::EnvLoader;
 
fn main() {
    std::env::set_var("SECRET", "MY_SECRET");
 
    EnvLoader::new()
    .overwrite_envs(true)
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

pub fn activate(self)[src]

Activate the module and load your ENV file.

Examples

// special.env 
 
@ I really love my comment design.
SECRET||YOUR_SECRET
// main.rs 
use env_plus::EnvLoader;
 
fn main() {
    EnvLoader::new()
    .change_delimiter(String::from("||"))
    .change_comment(String::from("@"))
    .change_file(String::from("./special.env"))
    .activate();
 
    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

Trait Implementations

impl Clone for EnvLoader[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.