Skip to main content

EnvConfig

Struct EnvConfig 

Source
pub struct EnvConfig { /* private fields */ }
Expand description

Layered environment configuration source combining .env files, explicit overrides, and OS env vars.

Implementations§

Source§

impl EnvConfig

Layering and typed extraction methods for EnvConfig.

Source

pub fn new() -> Self

Creates an empty EnvConfig without reading process environment variables.

§Returns
  • Self - Empty configuration map.
Source

pub fn from_process_env() -> Self

Creates an EnvConfig populated from the current process environment (std::env::vars).

§Returns
  • Self - Snapshot of process environment variables.
Source

pub fn from_dotenv_str(content: &str) -> Result<Self, EnvError>

Creates an EnvConfig by parsing .env text directly.

§Arguments
  • &str - Raw .env content.
§Returns
  • Result<Self, EnvError> - Populated config or syntax error.
Source

pub fn with_prefix<S>(self, prefix: S) -> Self
where S: Into<String>,

Sets an automatic key prefix applied to all subsequent lookups (e.g. "APP_").

§Arguments
  • S - Key prefix string.
§Returns
  • Self - Updated configuration.
Source

pub fn merge_dotenv_file(&mut self, path: &Path) -> Result<&mut Self, EnvError>

Merges all key-value pairs from a .env file on disk into self (overwriting existing keys).

§Arguments
  • &Path - Path to .env file.
§Returns
  • Result<&mut Self, EnvError> - Mutable reference to self or I/O / syntax error.
Source

pub fn merge_process_env(&mut self) -> &mut Self

Merges current OS environment variables on top of loaded .env defaults.

§Returns
  • &mut Self - Mutable reference for chaining.
Source

pub fn set<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: Into<String>, V: Into<String>,

Inserts or overrides a single key-value pair.

§Arguments
  • K - Environment variable name.
  • V - Environment variable value.
§Returns
  • &mut Self - Mutable reference for chaining.
Source

pub fn get(&self, key: &str) -> Option<&str>

Resolves a key with any configured prefix and returns its borrowed value.

§Arguments
  • &str - Unprefixed key name.
§Returns
  • Option<&str> - Borrowed value if present.
Source

pub fn require(&self, key: &str) -> Result<String, EnvError>

Returns a required non-empty string value or EnvError::MissingKey.

§Arguments
  • &str - Key name.
§Returns
  • Result<String, EnvError> - Owned value or missing-key error.
Source

pub fn require_secret(&self, key: &str) -> Result<SecretString, EnvError>

Returns a wrapped SecretString for a required secret key.

§Arguments
  • &str - Secret key name.
§Returns
  • Result<SecretString, EnvError> - Secret wrapper or missing-key error.
Source

pub fn get_or(&self, key: &str, default: &str) -> String

Returns a string value or default when absent or empty.

§Arguments
  • &str - Key name.
  • &str - Fallback string.
§Returns
  • String - Resolved value or fallback.
Source

pub fn parse_or<T>(&self, key: &str, default: T) -> Result<T, EnvError>
where T: FromStr, T::Err: Display,

Parses a key into any FromStr type, returning default when absent.

§Arguments
  • &str - Key name.
  • T - Fallback value when key is absent.
§Returns
  • Result<T, EnvError> - Parsed value, fallback, or parse error.
Source

pub fn get_bool_or(&self, key: &str, default: bool) -> Result<bool, EnvError>

Parses a boolean value (true/false/1/0/yes/no/on/off, case-insensitive).

§Arguments
  • &str - Key name.
  • bool - Fallback value when key is absent.
§Returns
  • Result<bool, EnvError> - Parsed boolean or parse error.
Source

pub fn get_duration_or( &self, key: &str, default: Duration, ) -> Result<Duration, EnvError>

Parses a human duration (250ms, 5s, 2m, 1h, or bare milliseconds integer).

§Arguments
  • &str - Key name.
  • Duration - Fallback duration when absent.
§Returns
  • Result<Duration, EnvError> - Parsed duration or parse error.
Source

pub fn get_list(&self, key: &str) -> Vec<String>

Splits a comma-separated environment value into trimmed non-empty strings.

§Arguments
  • &str - Key name.
§Returns
  • Vec<String> - List of trimmed entries (empty if key is absent).
Source

pub fn len(&self) -> usize

Returns the total number of stored entries.

§Returns
  • usize - Entry count.
Source

pub fn is_empty(&self) -> bool

Returns whether zero entries are stored.

§Returns
  • bool - true when empty.

Trait Implementations§

Source§

impl Clone for EnvConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EnvConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EnvConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for EnvConfig

§

impl Freeze for EnvConfig
where *const LeafNode<String, String> is !null: Freeze, PhantomData<(Owned, LeafOrInternal)>: Freeze, PhantomData<Box<(String, String)>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for EnvConfig

§

impl RefUnwindSafe for EnvConfig

§

impl Send for EnvConfig
where NodeRef<Owned, String, String, LeafOrInternal>: Send, BoxRaw<(String, String)>: Send, RawVecInner: Send,

§

impl Sync for EnvConfig
where NodeRef<Owned, String, String, LeafOrInternal>: Sync, BoxRaw<(String, String)>: Sync, RawVecInner: Sync,

§

impl Unpin for EnvConfig
where *const LeafNode<String, String> is !null: Unpin, Box<(String, String)>: Unpin, *const u8 is !null: Unpin,

§

impl UnsafeUnpin for EnvConfig
where *const LeafNode<String, String> is !null: UnsafeUnpin, PhantomData<(Owned, LeafOrInternal)>: UnsafeUnpin, PhantomData<Box<(String, String)>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin,

§

impl UnwindSafe for EnvConfig
where BTreeMap<String, String>: UnwindSafe, RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.