Skip to main content

lightshuttle_secrets/
lib.rs

1#![deny(missing_docs)]
2
3//! Secret sources and `.env` file loading for LightShuttle.
4//!
5//! This crate provides a pluggable system for loading environment variables and secrets.
6//! It forms a leaf layer of LightShuttle: it has no internal dependencies and is used by
7//! higher layers to interpolate `${env.VAR}` references in manifests.
8//!
9//! The core abstraction is the [`SecretSource`] trait, which any backing store can implement
10//! to expose key-value pairs. A built-in [`EnvFileSource`] implementation parses `.env` files
11//! using POSIX-like syntax (supporting quoted values, comments, and optional `export` prefix).
12//!
13//! # Example: Load a `.env` file
14//!
15//! ```
16//! use lightshuttle_secrets::EnvFileSource;
17//! use std::path::Path;
18//!
19//! # use tempfile::NamedTempFile;
20//! # use std::io::Write as _;
21//! # let mut f = NamedTempFile::new().unwrap();
22//! # f.write_all(b"API_KEY=secret123\nDB_URL=postgres://localhost/db\n").unwrap();
23//! # let path = f.path();
24//! let source = EnvFileSource::load(path)?;
25//! println!("Loaded {} entries from .env", source.len());
26//! # Ok::<(), lightshuttle_secrets::SecretError>(())
27//! ```
28//!
29//! # Example: Load `.env` optionally (default file)
30//!
31//! ```
32//! use lightshuttle_secrets::EnvFileSource;
33//!
34//! # use tempfile::NamedTempFile;
35//! # use std::io::Write as _;
36//! # let mut f = NamedTempFile::new().unwrap();
37//! # f.write_all(b"KEY=value\n").unwrap();
38//! # let path = f.path();
39//! // Returns None if the file does not exist (no error)
40//! if let Some(source) = EnvFileSource::load_optional(path)? {
41//!     println!("Using {} secrets from .env", source.len());
42//! } else {
43//!     println!("No .env file found; using defaults");
44//! }
45//! # Ok::<(), lightshuttle_secrets::SecretError>(())
46//! ```
47
48pub mod error;
49pub mod source;
50
51pub use error::SecretError;
52pub use source::{EnvFileSource, SecretSource};