chore: add documentation for env string
This commit is contained in:
parent
e479a1e40e
commit
c3caa22b94
4 changed files with 52 additions and 20 deletions
|
@ -1,20 +1,46 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
#[doc(hidden)]
|
/// A trait for converting value to EnvString.
|
||||||
|
///
|
||||||
|
/// This trait automatically implemented for any type which implements the
|
||||||
|
/// [`Display`] trait. As such, `ToEnvString` shouldn't be implemented directly:
|
||||||
|
/// [`Display`] should be implemented instead, and you get the `ToEnvString`
|
||||||
|
/// implementation for free.
|
||||||
|
///
|
||||||
|
/// [`Display`]: std::fmt::Display
|
||||||
pub trait ToEnvString {
|
pub trait ToEnvString {
|
||||||
|
/// Converts the giving value to a `EnvString`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// basic usage
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use itconfig::{EnvString, ToEnvString};
|
||||||
|
/// let i = 5;
|
||||||
|
/// let five = EnvString::from("5");
|
||||||
|
/// assert_eq!(five, i.to_env_string());
|
||||||
|
/// ```
|
||||||
fn to_env_string(&self) -> EnvString;
|
fn to_env_string(&self) -> EnvString;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
/// Simple and safe type conversions that may fail in a controlled way under
|
||||||
|
/// some circumstances.
|
||||||
|
///
|
||||||
|
/// This trait automatically implemented for all standard primitives. If you
|
||||||
|
/// want to use your custom type in the library you need to implement
|
||||||
|
/// `ToEnvString` and `FromEnvString` manually.
|
||||||
pub trait FromEnvString: Sized {
|
pub trait FromEnvString: Sized {
|
||||||
|
/// The type returned in the event of a conversion error.
|
||||||
type Err;
|
type Err;
|
||||||
|
|
||||||
|
/// Performs the conversion.
|
||||||
fn from_env_string(s: &EnvString) -> Result<Self, Self::Err>;
|
fn from_env_string(s: &EnvString) -> Result<Self, Self::Err>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ToEnvString for T
|
impl<T> ToEnvString for T
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: std::fmt::Display,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_env_string(&self) -> EnvString {
|
fn to_env_string(&self) -> EnvString {
|
||||||
|
@ -68,6 +94,7 @@ impl FromEnvString for bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "array")]
|
#[cfg(feature = "array")]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ArrayEnvError {
|
pub enum ArrayEnvError {
|
||||||
|
@ -90,8 +117,7 @@ where
|
||||||
.and_then(|vec| {
|
.and_then(|vec| {
|
||||||
vec.iter()
|
vec.iter()
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
v.to_env_string()
|
FromEnvString::from_env_string(&v.to_env_string())
|
||||||
.parse::<T>()
|
|
||||||
.map_err(|_| ArrayEnvError::FailedToParse)
|
.map_err(|_| ArrayEnvError::FailedToParse)
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<T>, _>>()
|
.collect::<Result<Vec<T>, _>>()
|
||||||
|
@ -115,13 +141,20 @@ impl FromEnvString for &'static str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
/// Wrapper under String type.
|
||||||
|
///
|
||||||
|
/// When we read the environment variable, we automatically convert the value
|
||||||
|
/// to EnvString and then convert it to your expected type.
|
||||||
|
///
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct EnvString(String);
|
pub struct EnvString(String);
|
||||||
|
|
||||||
impl EnvString {
|
impl<T> From<T> for EnvString
|
||||||
pub fn parse<T: FromEnvString>(&self) -> Result<T, T::Err> {
|
where
|
||||||
FromEnvString::from_env_string(self)
|
T: ToEnvString,
|
||||||
|
{
|
||||||
|
fn from(val: T) -> Self {
|
||||||
|
val.to_env_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
/// The error type for operations interacting with environment variables
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum EnvError {
|
pub enum EnvError {
|
||||||
|
/// The specified environment variable was not present in the current process's environment.
|
||||||
MissingVariable(String),
|
MissingVariable(String),
|
||||||
|
|
||||||
|
/// Failed to parse the specified environment variable.
|
||||||
FailedToParse(String),
|
FailedToParse(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::prelude::*;
|
use crate::envstr::*;
|
||||||
|
use crate::error::*;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
/// This function is similar as `get_env`, but it unwraps result with panic on error.
|
/// This function is similar as `get_env`, but it unwraps result with panic on error.
|
||||||
|
@ -123,17 +124,14 @@ where
|
||||||
.and_then(|env_str| parse_env_variable(env_name, env_str))
|
.and_then(|env_str| parse_env_variable(env_name, env_str))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
fn parse_env_variable<T>(env_name: &str, env_str: EnvString) -> Result<T, EnvError>
|
fn parse_env_variable<T>(env_name: &str, env_str: EnvString) -> Result<T, EnvError>
|
||||||
where
|
where
|
||||||
T: FromEnvString,
|
T: FromEnvString,
|
||||||
{
|
{
|
||||||
env_str
|
FromEnvString::from_env_string(&env_str)
|
||||||
.parse::<T>()
|
|
||||||
.map_err(|_| EnvError::FailedToParse(env_name.to_string()))
|
.map_err(|_| EnvError::FailedToParse(env_name.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
fn make_panic<T>(e: EnvError) -> T {
|
fn make_panic<T>(e: EnvError) -> T {
|
||||||
panic!("{}", e)
|
panic!("{}", e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,6 +140,7 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![deny(
|
#![deny(
|
||||||
missing_debug_implementations,
|
missing_debug_implementations,
|
||||||
|
missing_docs,
|
||||||
unstable_features,
|
unstable_features,
|
||||||
unused_imports,
|
unused_imports,
|
||||||
unused_qualifications
|
unused_qualifications
|
||||||
|
@ -149,18 +150,14 @@
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
pub mod envstr;
|
mod envstr;
|
||||||
mod error;
|
mod error;
|
||||||
mod getenv;
|
mod getenv;
|
||||||
|
|
||||||
|
pub use self::envstr::*;
|
||||||
pub use self::error::*;
|
pub use self::error::*;
|
||||||
pub use self::getenv::*;
|
pub use self::getenv::*;
|
||||||
|
|
||||||
pub mod prelude {
|
|
||||||
pub use crate::envstr::*;
|
|
||||||
pub use crate::error::*;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "macro")]
|
#[cfg(feature = "macro")]
|
||||||
extern crate itconfig_macro;
|
extern crate itconfig_macro;
|
||||||
#[cfg(feature = "macro")]
|
#[cfg(feature = "macro")]
|
||||||
|
|
Reference in a new issue