pub enum Value<'a> {
    None,
    Unit,
    Bool(bool),
    Integer(Integer),
    Float(Float),
    Bytes(Cow<'a, [u8]>),
    String(Cow<'a, str>),
    Sequence(Vec<Value<'a>, Global>),
    Mappings(Vec<(Value<'a>, Value<'a>), Global>),
}
Expand description

A Pot-encoded value. This type can be used to deserialize to and from Pot without knowing the original data structure.

Variants§

§

None

A value representing None.

§

Unit

A value representing unit (()).

§

Bool(bool)

A boolean value

§

Integer(Integer)

An integer value.

§

Float(Float)

A floating point value.

§

Bytes(Cow<'a, [u8]>)

A value containing arbitrary bytes.

§

String(Cow<'a, str>)

A string value.

§

Sequence(Vec<Value<'a>, Global>)

A sequence of values.

§

Mappings(Vec<(Value<'a>, Value<'a>), Global>)

A sequence of key-value mappings.

Implementations§

§

impl<'a> Value<'a>

pub fn from_serialize<T>(value: T) -> Result<Value<'a>, ValueError>where T: Serialize,

Creates a Value from the given Serde-compatible type.

use pot::Value;
use serde_derive::Serialize;

#[derive(Serialize, Debug)]
enum Example {
    Hello,
    World,
}

let original = vec![Example::Hello, Example::World];
let serialized = Value::from_serialize(&original)?;
assert_eq!(
    serialized,
    Value::Sequence(vec![
        Value::from(String::from("Hello")),
        Value::from(String::from("World"))
    ])
);

pub fn deserialize_as<'de, T>(&'de self) -> Result<T, ValueError>where T: Deserialize<'de>,

Attempts to create an instance of T from this value.

use pot::Value;
use serde_derive::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
enum Example {
    Hello,
    World,
}

let original = vec![Example::Hello, Example::World];
let serialized = Value::from_serialize(&original)?;
let deserialized: Vec<Example> = serialized.deserialize_as()?;
assert_eq!(deserialized, original);

pub fn from_sequence<IntoIter, T>(sequence: IntoIter) -> Value<'a>where IntoIter: IntoIterator<Item = T>, T: Into<Value<'a>>,

Returns a new value from an iterator of items that can be converted into a value.

let mappings = Value::from_sequence(Vec::<String>::new());
assert!(matches!(mappings, Value::Sequence(_)));

pub fn from_mappings<IntoIter, K, V>(mappings: IntoIter) -> Value<'a>where IntoIter: IntoIterator<Item = (K, V)>, K: Into<Value<'a>>, V: Into<Value<'a>>,

Returns a new value from an iterator of 2-element tuples representing key-value pairs.

let mappings = Value::from_mappings(HashMap::<String, u32>::new());
assert!(matches!(mappings, Value::Mappings(_)));

pub fn is_empty(&self) -> bool

Returns true if the value contained is considered empty.

// Value::None is always empty.
assert_eq!(Value::None.is_empty(), true);

// All primitive values, including Unit, are always not empty, even if they contain the value 0.
assert_eq!(Value::Unit.is_empty(), false);
assert_eq!(Value::from(false).is_empty(), false);
assert_eq!(Value::from(0_u8).is_empty(), false);
assert_eq!(Value::from(0_f32).is_empty(), false);

// For all other types, having a length of 0 will result in is_empty returning true.
assert_eq!(Value::from(Vec::<u8>::new()).is_empty(), true);
assert_eq!(Value::from(b"").is_empty(), true);
assert_eq!(Value::from(vec![0_u8]).is_empty(), false);

assert_eq!(Value::from("").is_empty(), true);
assert_eq!(Value::from("hi").is_empty(), false);

assert_eq!(Value::Sequence(Vec::new()).is_empty(), true);
assert_eq!(Value::from(vec![Value::None]).is_empty(), false);

assert_eq!(Value::Mappings(Vec::new()).is_empty(), true);
assert_eq!(
    Value::from(vec![(Value::None, Value::None)]).is_empty(),
    false
);

pub fn as_bool(&self) -> bool

Returns the value as a bool.

// Value::None is always false.
assert_eq!(Value::None.as_bool(), false);

// Value::Unit is always true.
assert_eq!(Value::Unit.as_bool(), true);

// Value::Bool will return the contained value
assert_eq!(Value::from(false).as_bool(), false);
assert_eq!(Value::from(true).as_bool(), true);

// All primitive values return true if the value is non-zero.
assert_eq!(Value::from(0_u8).as_bool(), false);
assert_eq!(Value::from(1_u8).as_bool(), true);
assert_eq!(Value::from(0_f32).as_bool(), false);
assert_eq!(Value::from(1_f32).as_bool(), true);

// For all other types, as_bool() returns the result of `!is_empty()`.
assert_eq!(Value::from(Vec::<u8>::new()).as_bool(), false);
assert_eq!(Value::from(b"").as_bool(), false);
assert_eq!(Value::from(vec![0_u8]).as_bool(), true);

assert_eq!(Value::from("").as_bool(), false);
assert_eq!(Value::from("hi").as_bool(), true);

assert_eq!(Value::Sequence(Vec::new()).as_bool(), false);
assert_eq!(Value::from(vec![Value::None]).as_bool(), true);

assert_eq!(Value::Mappings(Vec::new()).as_bool(), false);
assert_eq!(
    Value::from(vec![(Value::None, Value::None)]).as_bool(),
    true
);

pub fn as_integer(&self) -> Option<Integer>

Returns the value as an Integer. Returns None if the value is not a Self::Float or Self::Integer. Also returns None if the value is a float, but cannot be losslessly converted to an integer.

pub fn as_float(&self) -> Option<Float>

Returns the value as an Float. Returns None if the value is not a Self::Float or Self::Integer. Also returns None if the value is an integer, but cannot be losslessly converted to a float.

pub fn as_str(&self) -> Option<&str>

Returns the value as a string, or None if the value is not representable by a string. This will only return a value with variants Self::String and Self::Bytes. Bytes will only be returned if the contained bytes can be safely interpretted as UTF-8.

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the value as bytes, or None if the value is not stored as a representation of bytes. This will only return a value with variants Self::String and Self::Bytes.

pub fn values(&self) -> ValueIter<'_>

Returns an iterator that iterates over all values contained inside of this value. Returns an empty iterator if not a Self::Sequence or Self::Mappings. If a Self::Mappings, only the value portion of the mapping is returned.

pub fn mappings(&self) -> Iter<'_, (Value<'a>, Value<'a>)>

Returns an iterator that iterates over all mappings contained inside of this value. Returns an empty iterator if not a Self::Sequence or Self::Mappings. If a Self::Sequence, the key will always be Self::None.

pub fn into_static(self) -> Value<'static>

Converts self to a 'static lifetime by cloning any borrowed data.

pub fn to_static(&self) -> Value<'static>

Converts self to a 'static lifetime by cloning all data.

Trait Implementations§

§

impl<'a> Clone for Value<'a>

§

fn clone(&self) -> Value<'a>

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
§

impl<'a> Debug for Value<'a>

§

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

Formats the value using the given formatter. Read more
§

impl<'de, 'a> Deserialize<'de> for Value<'a>where 'de: 'a,

§

fn deserialize<D>( deserializer: D ) -> Result<Value<'a>, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,

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

impl<'a> Display for Value<'a>

§

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

Formats the value using the given formatter. Read more
§

impl<'a> From<&'a [u8]> for Value<'a>

§

fn from(bytes: &'a [u8]) -> Value<'a>

Converts to this type from the input type.
§

impl<'a, const N: usize> From<&'a [u8; N]> for Value<'a>

§

fn from(bytes: &'a [u8; N]) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<&'a Value<'a>> for OwnedValue

§

fn from(value: &'a Value<'a>) -> OwnedValue

Converts to this type from the input type.
§

impl<'a> From<&'a str> for Value<'a>

§

fn from(string: &'a str) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<()> for Value<'a>

§

fn from(_: ()) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<Option<Value<'a>>> for Value<'a>

§

fn from(value: Option<Value<'a>>) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<String> for Value<'a>

§

fn from(string: String) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<Value<'a>> for OwnedValue

§

fn from(value: Value<'a>) -> OwnedValue

Converts to this type from the input type.
§

impl<'a> From<Vec<(Value<'a>, Value<'a>), Global>> for Value<'a>

§

fn from(value: Vec<(Value<'a>, Value<'a>), Global>) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<Vec<Value<'a>, Global>> for Value<'a>

§

fn from(value: Vec<Value<'a>, Global>) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<Vec<u8, Global>> for Value<'a>

§

fn from(bytes: Vec<u8, Global>) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<bool> for Value<'a>

§

fn from(value: bool) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<f32> for Value<'a>

§

fn from(value: f32) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<f64> for Value<'a>

§

fn from(value: f64) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<i128> for Value<'a>

§

fn from(value: i128) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<i16> for Value<'a>

§

fn from(value: i16) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<i32> for Value<'a>

§

fn from(value: i32) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<i64> for Value<'a>

§

fn from(value: i64) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<i8> for Value<'a>

§

fn from(value: i8) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<u128> for Value<'a>

§

fn from(value: u128) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<u16> for Value<'a>

§

fn from(value: u16) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<u32> for Value<'a>

§

fn from(value: u32) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<u64> for Value<'a>

§

fn from(value: u64) -> Value<'a>

Converts to this type from the input type.
§

impl<'a> From<u8> for Value<'a>

§

fn from(value: u8) -> Value<'a>

Converts to this type from the input type.
§

impl<'a, K, V> FromIterator<(K, V)> for Value<'a>where K: Into<Value<'a>>, V: Into<Value<'a>>,

§

fn from_iter<T>(iter: T) -> Value<'a>where T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
§

impl<'a, A> FromIterator<A> for Value<'a>where A: Into<Value<'a>>,

§

fn from_iter<T>(iter: T) -> Value<'a>where T: IntoIterator<Item = A>,

Creates a value from an iterator. Read more
§

impl<'a, 'b> PartialEq<Value<'b>> for Value<'a>

§

fn eq(&self, other: &Value<'b>) -> 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.
§

impl<'a> Serialize for Value<'a>

§

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

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> Send for Value<'a>

§

impl<'a> Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere 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 Twhere 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 Twhere 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 Twhere 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.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ApiError for Twhere T: Display + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug,

source§

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