Struct ContextBuilder

Source
pub struct ContextBuilder<M: ContextMarker> { /* private fields */ }
Expand description

Builder for type-safe context configuration The builder is used to create a context with environment values and a default value.

§Example

use env_type::context::{ContextBuilder, ContextMarker};
use env_type::types::{EnvType};

struct TestContext;

impl ContextMarker for TestContext {
 type Value = String;
}

let context = ContextBuilder::<TestContext>::default()
 .with_value(EnvType::Dev, "dev".to_string())
 .with_value(EnvType::Test, "test".to_string())
 .with_default("default".to_string())
 .build();

assert_eq!(context.get_for_env(&EnvType::Dev), Some("dev".to_string()));
assert_eq!(context.get_for_env(&EnvType::Test), Some("test".to_string()));
assert_eq!(context.get_for_env(&EnvType::Stg), Some("default".to_string()));

Implementations§

Source§

impl<M: ContextMarker> ContextBuilder<M>

ContextBuilder implementation Create a new ContextBuilder with the environment values and default value.

Source

pub fn with_value(self, env: EnvType, value: M::Value) -> Self

Source

pub fn with_values<I>(self, envs: I, value: M::Value) -> Self
where I: IntoIterator<Item = EnvType>, M::Value: Clone,

Source

pub fn with_default(self, value: M::Value) -> Self

Source

pub fn build(self) -> Context<M>

Trait Implementations§

Source§

impl<M: ContextMarker> Default for ContextBuilder<M>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<M> Freeze for ContextBuilder<M>
where <M as ContextMarker>::Value: Freeze,

§

impl<M> RefUnwindSafe for ContextBuilder<M>

§

impl<M> Send for ContextBuilder<M>

§

impl<M> Sync for ContextBuilder<M>

§

impl<M> Unpin for ContextBuilder<M>
where <M as ContextMarker>::Value: Unpin, M: Unpin,

§

impl<M> UnwindSafe for ContextBuilder<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.