mirror of
https://github.com/darwincereska/envkit.git
synced 2026-06-11 10:23:23 -05:00
feat: updated bool parsing and added tests
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
/// Errors that can occur when parsing environment variables.
|
||||
pub enum EnvError {
|
||||
/// An environment variable was found, but its value was invalid.
|
||||
Invalid { key: String, value: String, message: String },
|
||||
|
||||
/// An environment variable was expected, but it was not found.
|
||||
MissingVar(String),
|
||||
}
|
||||
|
||||
impl EnvError {
|
||||
/// Creates a new `EnvError::Invalid` with the given key, value, and message.
|
||||
pub fn invalid<K, V, M>(key: K, value: V, message: M) -> Self
|
||||
where
|
||||
K: Into<String>,
|
||||
@@ -18,6 +23,7 @@ impl EnvError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new `EnvError::MissingVar` with the given key.
|
||||
pub fn missing<T>(key: T) -> Self
|
||||
where T: Into<String> {
|
||||
Self::MissingVar(key.into())
|
||||
|
||||
+17
-7
@@ -8,68 +8,78 @@ pub trait FromEnv: Sized {
|
||||
fn from_env(value: &str) -> Result<Self, EnvError>;
|
||||
}
|
||||
|
||||
/// Adds support for loading booleans from environment variables.
|
||||
impl FromEnv for bool {
|
||||
fn from_env(value: &str) -> Result<bool, EnvError> {
|
||||
match parser::parse_bool(value) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(e) => Err(e)
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds support for loading numbers from environment variables.
|
||||
impl<T: Number> FromEnv for T {
|
||||
fn from_env(value: &str) -> Result<Self, EnvError> {
|
||||
match parser::parse_number(value) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(e) => Err(e)
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds support for loading strings from environment variables.
|
||||
impl FromEnv for String {
|
||||
fn from_env(value: &str) -> Result<String, EnvError> {
|
||||
Ok(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// A loader for environment variables.
|
||||
pub struct EnvLoader;
|
||||
|
||||
impl EnvLoader {
|
||||
/// Creates a new `EnvLoader` with the given prefix.
|
||||
pub fn with_prefix<P: Into<String>>(&self, prefix: P) -> PrefixedEnvLoader {
|
||||
PrefixedEnvLoader {
|
||||
prefix: prefix.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the value of the environment variable with the given key.
|
||||
pub fn get<T: FromEnv>(&self, key: &str) -> Result<T, EnvError> {
|
||||
let raw = std::env::var(key).map_err(|_| EnvError::missing(key))?;
|
||||
FromEnv::from_env(&raw)
|
||||
}
|
||||
|
||||
/// Gets the value of the environment variable with the given key, or returns the default value if the variable is not set.
|
||||
pub fn get_or<T: FromEnv>(&self, key: &str, default: T) -> T {
|
||||
match self.get(key) {
|
||||
Ok(value) => value,
|
||||
Err(_) => default
|
||||
Err(_) => default,
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the value of the environment variable with the given key, or returns `None` if the variable is not set.
|
||||
pub fn get_opt<T: FromEnv>(&self, key: &str) -> Option<T> {
|
||||
match self.get(key) {
|
||||
Ok(value) => Some(value),
|
||||
Err(_) => None
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A loader for environment variables with a prefix.
|
||||
pub struct PrefixedEnvLoader {
|
||||
prefix: String,
|
||||
}
|
||||
|
||||
impl PrefixedEnvLoader {
|
||||
/// Returns the key with the prefix prepended.
|
||||
fn key(&self, key: &str) -> String {
|
||||
format!("{}{}", self.prefix, key)
|
||||
}
|
||||
|
||||
|
||||
pub fn get<T: FromEnv>(&self, key: &str) -> Result<T, EnvError> {
|
||||
EnvLoader.get(&self.key(key))
|
||||
}
|
||||
@@ -77,14 +87,14 @@ impl PrefixedEnvLoader {
|
||||
pub fn get_or<T: FromEnv>(&self, key: &str, default: T) -> T {
|
||||
match self.get(key) {
|
||||
Ok(value) => value,
|
||||
Err(_) => default
|
||||
Err(_) => default,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_opt<T: FromEnv>(&self, key: &str) -> Option<T> {
|
||||
match self.get(key) {
|
||||
Ok(value) => Some(value),
|
||||
Err(_) => None
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::any::type_name;
|
||||
use crate::error::EnvError;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// A trait for types that can be parsed from a string as a number.
|
||||
pub trait Number: FromStr<Err: Display> {}
|
||||
|
||||
impl Number for i8 {}
|
||||
@@ -20,11 +21,14 @@ impl Number for usize {}
|
||||
impl Number for f32 {}
|
||||
impl Number for f64 {}
|
||||
|
||||
/// Parses a string as a boolean value.
|
||||
pub fn parse_bool(value: &str) -> Result<bool, EnvError> {
|
||||
if value.to_lowercase() == "t" { return Ok(true) }
|
||||
if value.to_lowercase() == "f" { return Ok(false) }
|
||||
if value == "1" { return Ok(true) }
|
||||
if value == "0" { return Ok(false) }
|
||||
if value == "yes" { return Ok(true) }
|
||||
if value == "no" { return Ok(false) }
|
||||
|
||||
match value.parse::<bool>() {
|
||||
Ok(value) => Ok(value),
|
||||
@@ -32,6 +36,7 @@ pub fn parse_bool(value: &str) -> Result<bool, EnvError> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a string as a number of the specified type.
|
||||
pub fn parse_number<T: Number>(value: &str) -> Result<T, EnvError> {
|
||||
match value.parse::<T>() {
|
||||
Ok(value) => Ok(value),
|
||||
|
||||
Reference in New Issue
Block a user