Expand description
Typed environment binding primitives for Rust services.
Envbind is an open source Rust library for typed configuration. The
published package and Rust crate name are both envbind.
It is intended for application-boundary configuration in services that follow hexagonal architecture. Domain and use-case code receive typed settings from the boundary instead of reading process environment variables.
The public API is deliberately small: environment readers, a binder coordinator, typed variable specs, reusable validators, and a trait for settings structs.
§Core API
Environmentabstracts raw value lookup.ProcessEnvironmentreads from the process environment.MapEnvironmentprovides deterministic in-memory values for tests.Bindercoordinates oneBindingagainst an environment source.BindingExt::optionalturns any binding spec into an optional binding.ParameterSourcelets application settings structs compose typed values.StringVar,IntVar,FloatVar,BoolVar,ListVar,JsonVar,EnumVar,B64DecodedStringVar, andU16Varparse supported field types.BindErrorreports binding failures without exposing raw values.
Values are treated as sensitive by default. Raw inputs have defensive size limits, and larger JSON, base64, string, or list values must opt in through field-specific limit setters.
§Feature Flags
Envbind currently has no Cargo feature flags. Runtime dependencies are kept focused on parser and validator support for base64, JSON, regular expressions, and URLs.
§Example
use envbind::{
Binder, BoolVar, Environment, MapEnvironment, ParameterSource, StringVar, U16Var,
validators,
};
#[derive(Debug, Clone, PartialEq, Eq)]
struct ServiceSettings {
host: String,
port: u16,
service_name: String,
tracing_enabled: bool,
}
impl ParameterSource for ServiceSettings {
fn bind<E: Environment>(binder: &Binder<E>) -> Result<Self, envbind::BindError> {
Ok(Self {
host: binder.bind(&StringVar::new("SERVICE_HOST").default("127.0.0.1"))?,
port: binder.bind(
&U16Var::new("SERVICE_PORT")
.default(8080)
.validate(validators::u16_in_range(1, 65_535)),
)?,
service_name: binder.bind(&StringVar::new("SERVICE_NAME").default("api"))?,
tracing_enabled: binder.bind(&BoolVar::new("SERVICE_TRACING").default(false))?,
})
}
}
let environment = MapEnvironment::from_pairs([
("SERVICE_HOST", "localhost"),
("SERVICE_PORT", "9090"),
]);
let settings = ServiceSettings::from_environment(environment)?;
assert_eq!(settings.port, 9090);Re-exports§
pub use binder::Binder;pub use binder::Binding;pub use binder::BindingExt;pub use binder::OptionalVar;pub use environment::Environment;pub use environment::MapEnvironment;pub use environment::ProcessEnvironment;pub use error::BindError;pub use error::EnvironmentError;pub use error::ValidationError;pub use error::VariableName;pub use fields::B64DecodedStringVar;pub use fields::BoolVar;pub use fields::EnumVar;pub use fields::FloatVar;pub use fields::IntVar;pub use fields::JsonVar;pub use fields::ListVar;pub use fields::OptionalStringVar;pub use fields::StringVar;pub use fields::U16Var;pub use source::ParameterSource;
Modules§
- binder
- Binding coordinator for typed variable specs.
- environment
- Environment readers used by binders and settings objects.
- error
- Error types for environment binding and validation.
- fields
- Typed variable specs used by settings objects.
- source
- Trait implemented by service-specific settings objects.
- validators
- Reusable validation helpers for typed variable specs.