[][src]Enum figment::value::Value

pub enum Value {
    String(TagString),
    Char(Tagchar),
    Bool(Tagbool),
    Num(TagNum),
    Empty(TagEmpty),
    Dict(TagDict),
    Array(TagVec<Value>),
}

An enum representing all possible figment value variants.

Note that Value implements From<T> for all reasonable T:

use figment::value::Value;

let v = Value::from("hello");
assert_eq!(v.as_str(), Some("hello"));

Variants

String(TagString)

A string.

Char(Tagchar)

A character.

Bool(Tagbool)

A boolean.

Num(TagNum)

A numeric value.

Empty(TagEmpty)

A value with no value.

Dict(TagDict)

A dictionary: a map from String to Value.

Array(TagVec<Value>)

A sequence/array/vector.

Implementations

impl Value[src]

pub fn serialize<T: Serialize>(value: T) -> Result<Self, Error>[src]

Serialize a Value from any T: Serialize.

use figment::value::{Value, Empty};

let value = Value::serialize(10i8).unwrap();
assert_eq!(value.to_i128(), Some(10));

let value = Value::serialize(()).unwrap();
assert_eq!(value, Empty::Unit.into());

let value = Value::serialize(vec![4, 5, 6]).unwrap();
assert_eq!(value, vec![4, 5, 6].into());

pub fn deserialize<'de, T: Deserialize<'de>>(&self) -> Result<T, Error>[src]

Deserialize self into any deserializable T.

use figment::value::Value;

let value = Value::from("hello");
let string: String = value.deserialize().unwrap();
assert_eq!(string, "hello");

pub fn find(self, path: &str) -> Option<Value>[src]

Looks up and returns the value at path path, where path is of the form a.b.c where a, b, and c are keys to dictionaries. If the key is empty, simply returns self. If the key is not empty and self or any of the values for non-leaf keys in the path are not dictionaries, returns None.

This method consumes self. See Value::find_ref() for a non-consuming variant.

Example

use figment::{value::Value, util::map};

let value = Value::from(map! {
    "apple" => map! {
        "bat" => map! {
            "pie" => 4usize,
        },
        "cake" => map! {
            "pumpkin" => 10usize,
        }
    }
});

assert!(value.clone().find("apple").is_some());
assert!(value.clone().find("apple.bat").is_some());
assert!(value.clone().find("apple.cake").is_some());

assert_eq!(value.clone().find("apple.bat.pie").unwrap().to_u128(), Some(4));
assert_eq!(value.clone().find("apple.cake.pumpkin").unwrap().to_u128(), Some(10));

assert!(value.clone().find("apple.pie").is_none());
assert!(value.clone().find("pineapple").is_none());

pub fn find_ref<'a>(&'a self, path: &str) -> Option<&'a Value>[src]

Exactly like Value::find() but does not consume self, returning a reference to the found value, if any, instead.

Example

use figment::{value::Value, util::map};

let value = Value::from(map! {
    "apple" => map! {
        "bat" => map! {
            "pie" => 4usize,
        },
        "cake" => map! {
            "pumpkin" => 10usize,
        }
    }
});

assert!(value.find_ref("apple").is_some());
assert!(value.find_ref("apple.bat").is_some());
assert!(value.find_ref("apple.cake").is_some());

assert_eq!(value.find_ref("apple.bat.pie").unwrap().to_u128(), Some(4));
assert_eq!(value.find_ref("apple.cake.pumpkin").unwrap().to_u128(), Some(10));

assert!(value.find_ref("apple.pie").is_none());
assert!(value.find_ref("pineapple").is_none());

pub fn tag(&self) -> Tag[src]

Returns the Tag applied to this value.

use figment::{Figment, Profile, value::Value, util::map};

let map: Value = Figment::from(("key", "value")).extract().unwrap();
let value = map.find_ref("key").expect("value");
assert_eq!(value.as_str(), Some("value"));
assert!(!value.tag().is_default());
assert_eq!(value.tag().profile(), Some(Profile::Global));

let map: Value = Figment::from(("key", map!["key2" => 123])).extract().unwrap();
let value = map.find_ref("key.key2").expect("value");
assert_eq!(value.to_i128(), Some(123));
assert!(!value.tag().is_default());
assert_eq!(value.tag().profile(), Some(Profile::Global));

pub fn as_str(self: &Value) -> Option<&str>[src]

Converts self into a &str if self is a Value::String.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.as_str();

pub fn into_string(self: Value) -> Option<String>[src]

Converts self into a String if self is a Value::String.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.into_string();

pub fn to_char(self: &Value) -> Option<char>[src]

Converts self into a char if self is a Value::Char.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.to_char();

pub fn to_bool(self: &Value) -> Option<bool>[src]

Converts self into a bool if self is a Value::Bool.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.to_bool();

pub fn to_num(self: &Value) -> Option<Num>[src]

Converts self into a Num if self is a Value::Num.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.to_num();

pub fn to_empty(self: &Value) -> Option<Empty>[src]

Converts self into a Empty if self is a Value::Empty.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.to_empty();

pub fn as_dict(self: &Value) -> Option<&Dict>[src]

Converts self into a &Dict if self is a Value::Dict.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.as_dict();

pub fn into_dict(self: Value) -> Option<Dict>[src]

Converts self into a Dict if self is a Value::Dict.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.into_dict();

pub fn as_array(self: &Value) -> Option<&[Value]>[src]

Converts self into a &[Value] if self is a Value::Array.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.as_array();

pub fn into_array(self: Value) -> Option<Vec<Value>>[src]

Converts self into a Vec<Value> if self is a Value::Array.

Example

use figment::value::Value;

let value: Value = 123.into();
let converted = value.into_array();

pub fn to_u128(&self) -> Option<u128>[src]

Converts self into a u128 if self is an unsigned Value::Num variant.

Example

use figment::value::Value;

let value: Value = 123u8.into();
let converted = value.to_u128();
assert_eq!(converted, Some(123));

pub fn to_i128(&self) -> Option<i128>[src]

Converts self into a u128 if self is an unsigned Value::Num variant.

Example

use figment::value::Value;

let value: Value = 123i8.into();
let converted = value.to_i128();
assert_eq!(converted, Some(123));

pub fn to_actual(&self) -> Actual[src]

Converts self into the corresponding Actual.

See also Num::to_actual() and Empty::to_actual(), which are called internally by this method.

Example

use figment::{value::Value, error::Actual};

assert_eq!(Value::from('a').to_actual(), Actual::Char('a'));
assert_eq!(Value::from(&[1, 2, 3]).to_actual(), Actual::Seq);

Trait Implementations

impl Clone for Value[src]

impl Debug for Value[src]

impl<'de> Deserialize<'de> for Value[src]

impl<'de, '_> Deserializer<'de> for &'_ Value[src]

type Error = Error

The error type that can be returned if some error occurs during deserialization. Read more

impl<'a, T: Into<Value> + Clone> From<&'a [T; 1]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 2]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 3]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 4]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 5]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 6]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 7]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T; 8]> for Value[src]

impl<'a, T: Into<Value> + Clone> From<&'a [T]> for Value[src]

impl<'a> From<&'a str> for Value[src]

impl<K: AsRef<str>, V: Into<Value>> From<BTreeMap<K, V>> for Value[src]

impl From<Empty> for Value[src]

impl From<Num> for Value[src]

impl From<String> for Value[src]

impl From<Tag> for Value[src]

impl<'a, T: Into<Value>> From<Vec<T>> for Value[src]

impl From<bool> for Value[src]

impl From<char> for Value[src]

impl From<f32> for Value[src]

impl From<f64> for Value[src]

impl From<i128> for Value[src]

impl From<i16> for Value[src]

impl From<i32> for Value[src]

impl From<i64> for Value[src]

impl From<i8> for Value[src]

impl From<isize> for Value[src]

impl From<u128> for Value[src]

impl From<u16> for Value[src]

impl From<u32> for Value[src]

impl From<u64> for Value[src]

impl From<u8> for Value[src]

impl From<usize> for Value[src]

impl FromStr for Value[src]

type Err = Infallible

The associated error which can be returned from parsing.

impl PartialEq<Value> for Value[src]

impl Serialize for Value[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,