pub struct Value { /* private fields */ }Expand description
Main value type This is an enum that can hold any of the supported value types
Implementations§
source§impl Value
impl Value
sourcepub fn new(inner: InnerValue) -> Self
pub fn new(inner: InnerValue) -> Self
Creates a new value from the given inner value
sourcepub fn into_inner(self) -> InnerValue
pub fn into_inner(self) -> InnerValue
Consumes the value and returns the inner value
sourcepub fn inner(&self) -> &InnerValue
pub fn inner(&self) -> &InnerValue
Returns a reference to the inner value
sourcepub fn inner_mut(&mut self) -> &mut InnerValue
pub fn inner_mut(&mut self) -> &mut InnerValue
Returns a mutable reference to the inner value
sourcepub fn clear_flag(&mut self, flag: u8)
pub fn clear_flag(&mut self, flag: u8)
Clears the given flag
sourcepub fn serialize_tagged(self) -> Result<String, Error>
pub fn serialize_tagged(self) -> Result<String, Error>
Serializes the value to a tagged value This is useful for serialization, as it will preserve the type of integers
sourcepub fn deserialize_tagged(value: &str) -> Result<Self, Error>
pub fn deserialize_tagged(value: &str) -> Result<Self, Error>
Deserializes a tagged value
sourcepub fn bool(inner: impl Into<Bool>) -> Self
pub fn bool(inner: impl Into<Bool>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn u8(inner: impl Into<U8>) -> Self
pub fn u8(inner: impl Into<U8>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn i8(inner: impl Into<I8>) -> Self
pub fn i8(inner: impl Into<I8>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn u16(inner: impl Into<U16>) -> Self
pub fn u16(inner: impl Into<U16>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn i16(inner: impl Into<I16>) -> Self
pub fn i16(inner: impl Into<I16>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn u32(inner: impl Into<U32>) -> Self
pub fn u32(inner: impl Into<U32>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn i32(inner: impl Into<I32>) -> Self
pub fn i32(inner: impl Into<I32>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn u64(inner: impl Into<U64>) -> Self
pub fn u64(inner: impl Into<U64>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn i64(inner: impl Into<I64>) -> Self
pub fn i64(inner: impl Into<I64>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn float(inner: impl Into<Float>) -> Self
pub fn float(inner: impl Into<Float>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn fixed(inner: impl Into<Fixed>) -> Self
pub fn fixed(inner: impl Into<Fixed>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn currency(inner: impl Into<Currency>) -> Self
pub fn currency(inner: impl Into<Currency>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn string(inner: impl Into<Str>) -> Self
pub fn string(inner: impl Into<Str>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn range(inner: impl Into<Range>) -> Self
pub fn range(inner: impl Into<Range>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn array(inner: impl Into<Array>) -> Self
pub fn array(inner: impl Into<Array>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn object(inner: impl Into<Object>) -> Self
pub fn object(inner: impl Into<Object>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn int(inner: impl Into<I64>) -> Self
pub fn int(inner: impl Into<I64>) -> Self
Creates a new value from the given inner value
Use with the types defined in crate::types
sourcepub fn resolve(self, other: Self) -> Result<(Value, Value), Error>
pub fn resolve(self, other: Self) -> Result<(Value, Value), Error>
Resolves the type of two values based on a priority system. If successful, both returned values are guaranteed to be of the same type
Consumes both values
For details on type resolution, see Value::type_for_comparison
§Example
use polyvalue::Value;
use polyvalue::ValueType;
let a = Value::from(1.0);
let b = Value::from(2);
let (a, b) = a.resolve(b).expect("Could not resolve types");
assert!(a.own_type() == ValueType::Float);
assert!(b.own_type() == ValueType::Float);sourcepub fn own_type(&self) -> ValueType
pub fn own_type(&self) -> ValueType
Returns the type of the value
§Example
use polyvalue::Value;
use polyvalue::ValueType;
let value = Value::from(1.0);
assert!(value.own_type() == ValueType::Float);sourcepub fn either_type(&self, other: &Self, match_on: ValueType) -> bool
pub fn either_type(&self, other: &Self, match_on: ValueType) -> bool
returns true if either value is of the given type
§Example
use polyvalue::Value;
use polyvalue::ValueType;
let a = Value::from(1.0);
let b = Value::from(2);
assert!(a.either_type(&b, ValueType::Float));sourcepub fn as_a<T>(self) -> Result<T, Error>
pub fn as_a<T>(self) -> Result<T, Error>
Resolves a value to the given type
Use with the types defined in crate::types
Useful for enforcing a specific type, when you still wish to allow type-cooersion Float to Int, for example
§Example
use polyvalue::Value;
use polyvalue::types::I64;
let value = Value::from(1.0);
let int = value.as_a::<I64>().expect("Value could not be converted to int!");sourcepub fn if_is_a<T>(&self, type_name: ValueType) -> Option<T>
pub fn if_is_a<T>(&self, type_name: ValueType) -> Option<T>
Resolves a value to the given type, if the type matches a condition
Use with the types defined in crate::types
Returns None if the type does not match
§Example
use polyvalue::{ValueType, Value};
let value = Value::from(1.0);
if let Some(value) = value.if_is_a::<i64>(ValueType::Numeric) {
println!("Value is a number: {}", value);
}sourcepub fn as_type(self, type_name: ValueType) -> Result<Value, Error>
pub fn as_type(self, type_name: ValueType) -> Result<Value, Error>
Resolves a value to the given type Will fail if the value cannot be converted to the given type,
For virtual types, the highest-priority match will be used For any, the input value will be returned For numeric, the value will be converted to fixed For collection, the value will be converted to object
Similar to Value::as_a, but for type names in the ValueType enum
intead of types in the crate::types module
§Example
use polyvalue::Value;
use polyvalue::ValueType;
let value = Value::from(1.0);
let int = value.as_type(ValueType::Int).expect("Value could not be converted to int!");sourcepub fn type_for_comparison(&self, other: &Self) -> ValueType
pub fn type_for_comparison(&self, other: &Self) -> ValueType
Resolves the type of two values based on a priority system in order to determine how 2 values should be compared
The priority system is designed to prevent loss of information when comparing values of different types For example, Object -> Array would lose information on non-numeric keys Wheres Array -> Object would not
If both values are the same type, return that type Otherwise, cooerce both values to the same type using the order of priority:
- Object
- Array
- String
- Currency
- Fixed
- Float
- Int
- Bool
sourcepub fn is_truthy(&self) -> bool
pub fn is_truthy(&self) -> bool
Returns true if the value is truthy This is a convenience method for boolean values
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the value For strings, this is the length of the string in characters For collection types, this is the number of elements For everything else, this is 1 (the length of the array it would resolve to)
sourcepub fn to_json_string(&self) -> String
pub fn to_json_string(&self) -> String
Returns the value as a JSON string Returned value should be valid JSON
sourcepub fn weak_equality(self, other: Self) -> Result<bool, Error>
pub fn weak_equality(self, other: Self) -> Result<bool, Error>
Compares two values, ignoring type Consumes both values
Trait Implementations§
source§impl ArithmeticOperationExt for Value
impl ArithmeticOperationExt for Value
source§fn arithmetic_op(
self,
right: Self,
operation: ArithmeticOperation
) -> Result<Self, Error>
fn arithmetic_op( self, right: Self, operation: ArithmeticOperation ) -> Result<Self, Error>
Error::UnsupportedOperation will be returned Read moresource§impl BitwiseOperationExt for Value
impl BitwiseOperationExt for Value
source§fn bitwise_op(
self,
right: Self,
operation: BitwiseOperation
) -> Result<Self, Error>
fn bitwise_op( self, right: Self, operation: BitwiseOperation ) -> Result<Self, Error>
Error::UnsupportedOperation will be returned Read moresource§impl BooleanOperationExt for Value
impl BooleanOperationExt for Value
source§fn boolean_op(
self,
right: Self,
operation: BooleanOperation
) -> Result<Self, Error>
fn boolean_op( self, right: Self, operation: BooleanOperation ) -> Result<Self, Error>
Error::UnsupportedOperation will be returned Read moresource§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl From<CurrencyInner> for Value
impl From<CurrencyInner> for Value
source§fn from(value: CurrencyInner) -> Self
fn from(value: CurrencyInner) -> Self
source§impl From<InnerValue> for Value
impl From<InnerValue> for Value
source§fn from(inner: InnerValue) -> Value
fn from(inner: InnerValue) -> Value
source§impl From<ObjectInner> for Value
impl From<ObjectInner> for Value
source§fn from(value: ObjectInner) -> Self
fn from(value: ObjectInner) -> Self
source§impl From<RangeInclusive<<I64 as ValueTrait>::Inner>> for Value
impl From<RangeInclusive<<I64 as ValueTrait>::Inner>> for Value
source§fn from(value: RangeInner) -> Self
fn from(value: RangeInner) -> Self
source§impl From<Value> for InnerValue
impl From<Value> for InnerValue
source§fn from(value: Value) -> InnerValue
fn from(value: Value) -> InnerValue
source§impl From<f64> for Value
impl From<f64> for Value
source§fn from(value: FloatInner) -> Self
fn from(value: FloatInner) -> Self
source§impl IndexingMutationExt for Value
impl IndexingMutationExt for Value
source§fn get_index_mut(&mut self, index: &Value) -> Result<&mut Value, Error>
fn get_index_mut(&mut self, index: &Value) -> Result<&mut Value, Error>
Error::Index if the index is not found Read moresource§fn get_indices_mut(&mut self, index: &Value) -> Result<Vec<&mut Value>, Error>
fn get_indices_mut(&mut self, index: &Value) -> Result<Vec<&mut Value>, Error>
Error::Index if any of the indices are not found Read moresource§fn set_index(&mut self, index: &Value, value: Value) -> Result<(), Error>
fn set_index(&mut self, index: &Value, value: Value) -> Result<(), Error>
Error::Index if the index is not found Read moresource§impl IndexingOperationExt for Value
impl IndexingOperationExt for Value
source§impl MatchingOperationExt for Value
impl MatchingOperationExt for Value
source§impl Ord for Value
impl Ord for Value
source§impl PartialEq for Value
impl PartialEq for Value
source§impl PartialOrd for Value
impl PartialOrd for Value
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more