Struct irx_config::value::Value

source ·
pub struct Value { /* private fields */ }
Expand description

This structure represent key-value based configuration data.

IMPORTANT: All functionality related to the sealed state only affects the display/debugging output.

Implementations§

source§

impl Value

source

pub fn try_from<T: Serialize>(value: T) -> Result<Self>

Try to create Value structure from any type which implements Serialize trait and make key names to be case-sensitive. If successful return instance of Value structure.

Errors

If any errors will occur during construction then error will be returned.

Example
use irx_config::{json, Value};

let data = Value::try_from(json!(
{
    "name": "John Doe",
    "age": 43,
    "phones": [
        "+44 1234567",
        "+44 2345678"
    ]
}))?;
source

pub fn try_from_with_case<T: Serialize>(value: T, case_on: bool) -> Result<Self>

Try to create Value structure from any type which implements Serialize trait and make key names to be case-sensitive/insensitive. If successful return instance of Value structure.

Errors

If any errors will occur during construction then error will be returned.

source

pub fn with_case(on: bool) -> Self

Create default Value structure with key names to be case-sensitive/insensitive.

source

pub fn is_case_sensitive(&self) -> bool

Return true if key names is case-sensitive, otherwise return false.

source

pub fn merge(self, value: &Value) -> Self

Merge a input Value to the given Value structure. The key names will use case-sensitivity of given Value during merge. Return merged result Value structure. If given Value was sealed and merge operation was mutating then it will be in SealedState::Mutated.

Example
use irx_config::{json, Value};

let mut person = Value::try_from(json!({
    "name": "John Doe",
    "age": 43
}))?;

let phones = Value::try_from(json!({
    "phones": [
        "+44 1234567",
        "+44 2345678"
    ]
}))?;

person = person.merge(&phones);
source

pub fn merge_with_case(self, value: &Value, case_on: bool) -> Self

Merge a input Value to the given Value structure. The key names will be case-sensitive or case-insensitive during merge, according to case_on parameter. Return merged result Value structure. If given Value was sealed and merge operation was mutating then it will be in SealedState::Mutated.

source

pub fn get_by_keys<I, K, T>(&self, keys: I) -> Result<Option<T>>
where I: IntoIterator<Item = K>, K: AsRef<str>, T: DeserializeOwned,

Return deserialized data of any type which implements Deserialize trait for given key path represented as iterator. If given key path does not exists Ok(None) will be returned.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::{json, Value};

let logger = Value::try_from(json!({
    "logger": {
        "id": 42,
        "host": "localhost"
    }
}))?;

let id: u32 = logger.get_by_keys(["logger", "id"])?.unwrap();
source

pub fn get_by_key_path<T, P>(&self, path: P) -> Result<Option<T>>
where T: DeserializeOwned, P: AsRef<str>,

Return deserialized data of any type which implements Deserialize trait for given key path represented as string with default keys level delimiter DEFAULT_KEYS_SEPARATOR. If given key path does not exists Ok(None) will be returned.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::{json, Value};

let logger = Value::try_from(json!({
    "logger": {
        "id": 42,
        "host": "localhost"
    }
}))?;

let id: u32 = logger.get_by_key_path("logger:id")?.unwrap();
source

pub fn get_by_key_path_with_delim<T, P, D>( &self, path: P, delim: D ) -> Result<Option<T>>
where T: DeserializeOwned, P: AsRef<str>, D: AsRef<str>,

Return deserialized data of any type which implements Deserialize trait for given key path represented as string with given keys level delimiter. If given key path does not exists Ok(None) will be returned.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::{json, Value};

let logger = Value::try_from(json!({
    "logger": {
        "id": 42,
        "host": "localhost"
    }
}))?;

let host: String = logger.get_by_key_path_with_delim("logger/host", "/")?.unwrap();
source

pub fn get<T: DeserializeOwned>(&self) -> Result<T>

Return deserialized data of any type which implements Deserialize trait.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::{json, Value};
use serde::Deserialize;

#[derive(Deserialize)]
struct Logger {
    pub id: u32,
    pub host: String,
}

#[derive(Deserialize)]
struct Config {
    logger: Logger,
}

let config = Value::try_from(json!({
    "logger": {
        "id": 42,
        "host": "localhost"
    }
}))?;

let config: Config = config.get()?;
source

pub fn set_by_keys<I, K, T>( &mut self, keys: I, value: T ) -> Result<Option<Self>>
where I: IntoIterator<Item = K>, K: AsRef<str>, T: Serialize,

Set value of any type which implements Serialize trait for given key path represented as iterator. If Value was sealed and set operation was successful then it will be in SealedState::Mutated. Return previous value for same key path if any.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::Value;

let mut value = Value::default();
value.set_by_keys(["logger", "id"], 42)?;
source

pub fn set_by_key_path<T, P>( &mut self, path: P, value: T ) -> Result<Option<Self>>
where T: Serialize, P: AsRef<str>,

Set value of any type which implements Serialize trait for given key path represented as string with default keys level delimiter DEFAULT_KEYS_SEPARATOR. If Value was sealed and set operation was successful then it will be in SealedState::Mutated. Return previous value for same key path if any.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::Value;

let mut value = Value::default();
value.set_by_key_path("logger:id", 42)?;
source

pub fn set_by_key_path_with_delim<T, P, D>( &mut self, path: P, delim: D, value: T ) -> Result<Option<Self>>
where T: Serialize, P: AsRef<str>, D: AsRef<str>,

Set value of any type which implements Serialize trait for given key path represented as string with given keys level delimiter. If Value was sealed and set operation was successful then it will be in SealedState::Mutated. Return previous value for same key path if any.

Errors

If any errors will occur then error will be returned.

Example
use irx_config::Value;

let mut value = Value::default();
value.set_by_key_path_with_delim("logger/id", "/", 42)?;
source

pub fn as_bytes(&self) -> Vec<u8>

Return Value structure as a sequence of bytes.

source

pub fn seal<S>(&mut self, suffix: S) -> &mut Self
where S: AsRef<str>,

Seal secret values in Value structure with given suffix. Such values will be obfuscated with ******** during display/debugging output. If not set then all values will be displayed as is.

Example
use irx_config::Value;

let mut value = Value::try_from(json!({
    "user": "user name",
    "password_sealed_": "secret"
}))?;

value.seal("_sealed_");
source

pub fn is_sealed(&self) -> bool

Return true if Value is sealed, otherwise return false.

source

pub fn sealed_state(&self) -> SealedState

Return sealed state SealedState.

Trait Implementations§

source§

impl Clone for Value

source§

fn clone(&self) -> Value

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Value

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl Default for Value

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Value

source§

fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Value

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl PartialEq for Value

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Value

source§

fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Value

Auto Trait Implementations§

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,