pub struct EnvFileSource { /* private fields */ }Expand description
Loads secrets from a .env file.
This struct parses a .env file at construction time and caches the key-value pairs.
Once constructed, the source can be cloned and reused; each call to load returns
a copy of the cached map.
§Supported syntax
KEY=VALUE: plain valueKEY="quoted value": double-quoted values with quotes strippedKEY='quoted value': single-quoted values with quotes strippedexport KEY=VALUE: optionalexportprefix followed by whitespace (ignored)# comment: lines starting with#are skipped- Blank lines: ignored
- Inline comments:
KEY=VALUE # comment(trailing comments on unquoted values only; quoted values preserve all characters including#) - UTF-8 BOM: stripped from the start of the file if present
§Errors
Construction fails if the file does not exist, is unreadable, or contains invalid syntax.
Use load_optional for paths that may not exist.
Implementations§
Source§impl EnvFileSource
impl EnvFileSource
Sourcepub fn load(path: impl Into<PathBuf>) -> Result<Self, SecretError>
pub fn load(path: impl Into<PathBuf>) -> Result<Self, SecretError>
Load from path, requiring the file to exist.
Returns SecretError::FileNotFound if the file does not exist, or
SecretError::Io if the file is unreadable. Use this when the path
was explicitly provided by the user (e.g. via --env-file CLI flag).
For optional files (like the default .env), use load_optional instead.
§Errors
Returns an error if the file does not exist, cannot be read, or contains invalid syntax.
§Examples
use lightshuttle_secrets::EnvFileSource;
let source = EnvFileSource::load(".env")?;
println!("Loaded {} secrets", source.len());Sourcepub fn load_optional(
path: impl Into<PathBuf>,
) -> Result<Option<Self>, SecretError>
pub fn load_optional( path: impl Into<PathBuf>, ) -> Result<Option<Self>, SecretError>
Load from path if it exists, returning None if the file is absent.
This is useful for optional configuration files like the default .env path.
If the file is absent, no error is raised. If the file exists but is malformed,
an error is returned.
§Errors
Returns an error only if the file exists but cannot be read or contains invalid syntax.
§Examples
use lightshuttle_secrets::EnvFileSource;
if let Some(source) = EnvFileSource::load_optional(".env")? {
println!("Using {} secrets", source.len());
} else {
println!("No .env file; using defaults");
}