Env Settings is a Rust library that helps you to initialize structs using environment variables
This Rust library took inspiration from
pydantic's BaseSettings
Python class
sh
cargo add env-settings
cargo add env-settings-derive
When you add the EnvSettings
derive to a struct
, two methods are added to it
rs
fn from_env() -> env_settings_utils::EnvSettingsResult<Self>
It creates a new instance using just the environment variables. If something fails, it returns an env_settings_utils::EnvSettingsError
error
rs
fn new(...) -> env_settings_utils::EnvSettingsResult<Self>
It creates a new instance using a combination of environment variables and parameters. More in detail, every field value can be passed as parameter wrapped in an Option
object. Then if the parameter is Some
, it is used, otherwise the value is recoved from the environment variables. If something fails, it returns an env_settings_utils::EnvSettingsError
error
sh
export name=paolo
export age=42
```rs use envsettingsderive::EnvSettings;
struct MyStruct { name: String,
age: u8,
}
fn main() { let mystruct = MyStruct::fromenv().unwrap(); asserteq!(mystruct.name, "paolo".tostring()); asserteq!(my_struct.age, 42);
let name = "luca";
let my_struct = MyStruct::new(Some(name.to_string()), None).unwrap();
assert_eq!(my_struct.name, name);
assert_eq!(my_struct.age, 42);
} ```
sh
echo "MY_STRUCT_AGE=42\n" > .env
export MY_BIRTH_DATE=01/01/1970
```rs use envsettingsderive::EnvSettings;
struct MyStruct { #[env_settings(default = "paolo")] name: String,
age: u8,
#[env_settings(variable = "MY_BIRTH_DATE")]
birth_date: String,
}
fn main() { let mystruct = MyStruct::fromenv().unwrap(); asserteq!(mystruct.name, "paolo".tostring()); asserteq!(mystruct.age, 42); asserteq!(mystruct.birthdate, "01/01/1970");
let name = "luca";
let my_struct = MyStruct::new(Some(name.to_string()), None, None).unwrap();
assert_eq!(my_struct.name, name);
assert_eq!(my_struct.age, 42);
assert_eq!(my_struct.birth_date, "01/01/1970");
} ```
The current supported parameters for the structs are:
case_insensitive
: whether the environment variables matching should be case insensitive. By default, matching is case sensitive.delay
: whether to delay the lookup for environment variables from compilation time to run time. By default the lookup is performed at compilation timefile_path
: the file path to read to add some environment variables (e.g. .env
). By default, it is not setprefix
: the prefix to add to the name of the struct fields before matching the environment variables. By default, it is not setThe current supported parameters for the fields are:
default
: the default value to use if the environment variable is not found. By default, it is not setvariable
: the environment variable to use for the lookup. By default, the name of the fieldnew
method (if using new
)..env
)Before starting to work on a contribution please read:
When testing run:
sh
cargo test -- --test-threads=1
to prevent tests from competitively interacting with the same file
This project is licensed under the terms of the MIT or Apache 2.0 license.