| Crates.io | env-convert |
| lib.rs | env-convert |
| version | 0.1.1 |
| created_at | 2023-07-27 01:37:38.496319+00 |
| updated_at | 2023-07-31 00:55:38.912395+00 |
| description | Conversion of environment variables with default values. |
| homepage | |
| repository | https://github.com/code-butter/rust-env-convert |
| max_upload_size | |
| id | 927001 |
| size | 8,209 |
A convenience library for dealing with environment variables, handling basic type conversions and default values.
get_default_env_var takes the environment variable name and the default value as a string if it does not exist.
It returns an EnvVar that can be converted into a Result with the conversion type. You can then use that
result to detect if the conversion happened successfully.
get_env_var takes the environment variable name and returns a Result<EnvVar,std::env::VarError>.
let max_connections_result: Result<u32,_> = get_default_env_var("MAX_DB_CONNECTIONS", "5").into();
let max_connections = max_connections_result.expect("MAX_DB_CONNECTIONS must be an integer");
let env_result: Result<String,_> = get_env_var("DEFAULT_GREETING").expect("DEFAULT_GREETING is required").into();
// You can also convert strings directly
let default_greeting: String = get_default_env_var("DEFAULT_GREETING", "hello").into();
The following conversions are implemented in this library:
i8, u16, usize...)f32, f64)To implement your own, you can implement the From<EnvVar> trait for Result<YourType, EnvVarConversionError>.
Until this reaches version 1, I will make an attempt to keep the API stable but no guarantees. After that this library will follow semantic versioning.