#[non_exhaustive]pub enum Value {
Show 23 variants
U128(u128),
I128(i128),
U64(u64),
I64(i64),
U32(u32),
I32(i32),
U16(u16),
I16(i16),
U8(u8),
I8(i8),
Bytes(Vec<u8>),
Bytes20([u8; 20]),
Bytes32([u8; 32]),
Bytes36([u8; 36]),
EnumU8(Vec<u8>),
EnumString(Vec<String>),
Identifier(Hash256),
Float(f64),
Text(String),
Bool(bool),
Null,
Array(Vec<Value>),
Map(ValueMap),
}Expand description
A representation of a dynamic value that can handled dynamically
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
U128(u128)
A u128 integer
I128(i128)
A i128 integer
U64(u64)
A u64 integer
I64(i64)
A i64 integer
U32(u32)
A u32 integer
I32(i32)
A i32 integer
U16(u16)
A u16 integer
I16(i16)
A i16 integer
U8(u8)
A u8 integer
I8(i8)
A i8 integer
Bytes(Vec<u8>)
Bytes
Bytes20([u8; 20])
Bytes 20
Bytes32([u8; 32])
Bytes 32
Bytes36([u8; 36])
Bytes 36 : Useful for outpoints
EnumU8(Vec<u8>)
An enumeration of u8
EnumString(Vec<String>)
An enumeration of strings
Identifier(Hash256)
Identifier The identifier is very similar to bytes, however it is serialized to Base58 when converted to a JSON Value
Float(f64)
A float
Text(String)
A string
Bool(bool)
A boolean
Null
Null
Array(Vec<Value>)
An array
Map(ValueMap)
A map
Implementations§
Source§impl Value
impl Value
pub fn convert_from_cbor_map<I, R>(map: I) -> Result<R, Error>
pub fn convert_to_cbor_map<I, R>(map: I) -> Result<R, Error>
pub fn to_cbor_buffer(&self) -> Result<Vec<u8>, Error>
Source§impl Value
impl Value
pub fn convert_from_serde_json_map<I, R>(map: I) -> R
pub fn try_into_validating_json(self) -> Result<JsonValue, Error>
pub fn try_into_validating_btree_map_json( self, ) -> Result<BTreeMap<String, JsonValue>, Error>
pub fn try_to_validating_json(&self) -> Result<JsonValue, Error>
Source§impl Value
impl Value
Sourcepub fn equal_underlying_data(&self, other: &Value) -> bool
pub fn equal_underlying_data(&self, other: &Value) -> bool
Returns true when the data represented by the two Values
is identical, even if they are stored in different but
compatible variants.
- All “bytes-like” variants (
Bytes,Bytes20,Bytes32,Bytes36,Identifier) compare equal when their byte sequences match. - All integer variants (
U*,I*) compare equal when they represent the same numeric value. - Otherwise falls back to normal
==(PartialEq) behaviour.
Source§impl Value
impl Value
pub fn has(&self, key: &str) -> Result<bool, Error>
pub fn get<'a>(&'a self, key: &'a str) -> Result<Option<&'a Value>, Error>
pub fn get_mut<'a>( &'a mut self, key: &'a str, ) -> Result<Option<&'a mut Value>, Error>
pub fn get_value<'a>(&'a self, key: &'a str) -> Result<&'a Value, Error>
pub fn get_value_mut<'a>( &'a mut self, key: &'a str, ) -> Result<&'a mut Value, Error>
pub fn get_optional_value<'a>( &'a self, key: &'a str, ) -> Result<Option<&'a Value>, Error>
pub fn get_optional_value_mut<'a>( &'a mut self, key: &'a str, ) -> Result<Option<&'a mut Value>, Error>
pub fn set_into_value<T>(&mut self, key: &str, value: T) -> Result<(), Error>
pub fn set_into_binary_data( &mut self, key: &str, value: Vec<u8>, ) -> Result<(), Error>
pub fn set_value(&mut self, key: &str, value: Value) -> Result<(), Error>
pub fn insert(&mut self, key: String, value: Value) -> Result<(), Error>
pub fn insert_at_end(&mut self, key: String, value: Value) -> Result<(), Error>
pub fn remove(&mut self, key: &str) -> Result<Value, Error>
pub fn remove_many(&mut self, keys: &[&str]) -> Result<(), Error>
pub fn remove_optional_value( &mut self, key: &str, ) -> Result<Option<Value>, Error>
pub fn remove_optional_value_if_null(&mut self, key: &str) -> Result<(), Error>
pub fn remove_optional_value_if_empty_array( &mut self, key: &str, ) -> Result<(), Error>
pub fn remove_integer<T>(&mut self, key: &str) -> Result<T, Error>
pub fn remove_optional_integer<T>( &mut self, key: &str, ) -> Result<Option<T>, Error>
pub fn remove_identifier(&mut self, key: &str) -> Result<Identifier, Error>
pub fn remove_optional_identifier( &mut self, key: &str, ) -> Result<Option<Identifier>, Error>
pub fn remove_bytes_32(&mut self, key: &str) -> Result<Bytes32, Error>
pub fn remove_optional_bytes_32( &mut self, key: &str, ) -> Result<Option<Bytes32>, Error>
pub fn remove_hash256_bytes(&mut self, key: &str) -> Result<[u8; 32], Error>
pub fn remove_optional_hash256_bytes( &mut self, key: &str, ) -> Result<Option<[u8; 32]>, Error>
pub fn remove_bytes(&mut self, key: &str) -> Result<Vec<u8>, Error>
pub fn remove_optional_bytes( &mut self, key: &str, ) -> Result<Option<Vec<u8>>, Error>
pub fn remove_binary_data(&mut self, key: &str) -> Result<BinaryData, Error>
pub fn remove_optional_binary_data( &mut self, key: &str, ) -> Result<Option<BinaryData>, Error>
pub fn remove_array(&mut self, key: &str) -> Result<Vec<Value>, Error>
pub fn remove_optional_array( &mut self, key: &str, ) -> Result<Option<Vec<Value>>, Error>
pub fn get_optional_integer<T>(&self, key: &str) -> Result<Option<T>, Error>
pub fn get_integer<T>(&self, key: &str) -> Result<T, Error>
pub fn get_optional_str<'a>( &'a self, key: &'a str, ) -> Result<Option<&'a str>, Error>
pub fn get_str<'a>(&'a self, key: &'a str) -> Result<&'a str, Error>
pub fn get_optional_bool(&self, key: &str) -> Result<Option<bool>, Error>
pub fn get_bool<'a>(&'a self, key: &'a str) -> Result<bool, Error>
pub fn get_optional_array(&self, key: &str) -> Result<Option<Vec<Value>>, Error>
pub fn get_array<'a>(&'a self, key: &'a str) -> Result<Vec<Value>, Error>
pub fn get_optional_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>( &'a self, key: &'a str, ) -> Result<Option<I>, Error>
pub fn get_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>( &'a self, key: &'a str, ) -> Result<I, Error>
pub fn get_optional_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>( &'a mut self, key: &'a str, ) -> Result<Option<I>, Error>
pub fn get_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>( &'a mut self, key: &'a str, ) -> Result<I, Error>
pub fn get_optional_array_slice<'a>( &'a self, key: &'a str, ) -> Result<Option<&'a [Value]>, Error>
pub fn get_array_ref<'a>( &'a self, key: &'a str, ) -> Result<&'a Vec<Value>, Error>
pub fn get_optional_array_mut_ref<'a>( &'a mut self, key: &'a str, ) -> Result<Option<&'a mut Vec<Value>>, Error>
pub fn get_array_mut_ref<'a>( &'a mut self, key: &'a str, ) -> Result<&'a mut Vec<Value>, Error>
pub fn get_array_slice<'a>(&'a self, key: &'a str) -> Result<&'a [Value], Error>
pub fn get_optional_binary_data<'a>( &'a self, key: &'a str, ) -> Result<Option<BinaryData>, Error>
pub fn get_binary_data<'a>(&'a self, key: &'a str) -> Result<BinaryData, Error>
pub fn get_optional_bytes<'a>( &'a self, key: &'a str, ) -> Result<Option<Vec<u8>>, Error>
pub fn get_bytes<'a>(&'a self, key: &'a str) -> Result<Vec<u8>, Error>
pub fn get_optional_bytes_into<T: From<Vec<u8>>>( &self, key: &str, ) -> Result<Option<T>, Error>
pub fn get_optional_bytes_try_into<T: TryFrom<Vec<u8>, Error = Error>>( &self, key: &str, ) -> Result<Option<T>, Error>
pub fn get_bytes_into<T: From<Vec<u8>>>(&self, key: &str) -> Result<T, Error>
pub fn get_bytes_try_into<T: TryFrom<Vec<u8>, Error = Error>>( &self, key: &str, ) -> Result<T, Error>
pub fn get_optional_hash256<'a>( &'a self, key: &'a str, ) -> Result<Option<[u8; 32]>, Error>
pub fn get_identifier<'a>(&'a self, key: &'a str) -> Result<Identifier, Error>
pub fn get_optional_identifier<'a>( &'a self, key: &'a str, ) -> Result<Option<Identifier>, Error>
pub fn get_hash256<'a>(&'a self, key: &'a str) -> Result<[u8; 32], Error>
pub fn get_hash256_as_bs58_string<'a>( &'a self, key: &'a str, ) -> Result<String, Error>
Sourcepub fn inner_optional_array_of_strings<'a, I: FromIterator<String>>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Option<I>
pub fn inner_optional_array_of_strings<'a, I: FromIterator<String>>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Option<I>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_recursive_optional_array_of_strings<'a>(
document_type: &'a [(Value, Value)],
prefix: String,
recursive_key: &'a str,
key: &'a str,
) -> BTreeSet<String>
pub fn inner_recursive_optional_array_of_strings<'a>( document_type: &'a [(Value, Value)], prefix: String, recursive_key: &'a str, key: &'a str, ) -> BTreeSet<String>
Retrieves the value of a key from a map if it’s an array of strings. This is useful for constructing the required fields in a contract. For the DPNS Contract we would get as a result { “label”, “normalizedLabel”, “normalizedParentDomainName”, “preorderSalt”, “records”, “subdomainRules”, “subdomainRules.allowSubdomains”, }
Sourcepub fn inner_optional_array(
document_type: &[(Value, Value)],
key: &str,
) -> Result<Option<Vec<Value>>, Error>
pub fn inner_optional_array( document_type: &[(Value, Value)], key: &str, ) -> Result<Option<Vec<Value>>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_array_mut_ref<'a>(
document_type: &'a mut [(Value, Value)],
key: &'a str,
) -> Result<&'a mut Vec<Value>, Error>
pub fn inner_array_mut_ref<'a>( document_type: &'a mut [(Value, Value)], key: &'a str, ) -> Result<&'a mut Vec<Value>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_optional_array_mut_ref<'a>(
document_type: &'a mut [(Value, Value)],
key: &'a str,
) -> Result<Option<&'a mut Vec<Value>>, Error>
pub fn inner_optional_array_mut_ref<'a>( document_type: &'a mut [(Value, Value)], key: &'a str, ) -> Result<Option<&'a mut Vec<Value>>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_array_ref<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<&'a Vec<Value>, Error>
pub fn inner_array_ref<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<&'a Vec<Value>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_array_owned(
document_type: &[(Value, Value)],
key: &str,
) -> Result<Vec<Value>, Error>
pub fn inner_array_owned( document_type: &[(Value, Value)], key: &str, ) -> Result<Vec<Value>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_optional_array_slice<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<&'a [Value]>, Error>
pub fn inner_optional_array_slice<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<&'a [Value]>, Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_array_slice<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<&'a [Value], Error>
pub fn inner_array_slice<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<&'a [Value], Error>
Retrieves the value of a key from a map if it’s an array of strings.
Sourcepub fn inner_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<I, Error>
pub fn inner_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<I, Error>
Gets the inner map from a map and converts it to a string map
Sourcepub fn inner_optional_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<I>, Error>
pub fn inner_optional_string_ref_map<'a, I: FromIterator<(String, &'a Value)>>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<I>, Error>
Gets the inner map from a map and converts it to a string map
Sourcepub fn inner_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
document_type: &'a mut [(Value, Value)],
key: &'a str,
) -> Result<I, Error>
pub fn inner_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>( document_type: &'a mut [(Value, Value)], key: &'a str, ) -> Result<I, Error>
Gets the inner map from a map and converts it to a string map
Sourcepub fn inner_optional_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>(
document_type: &'a mut [(Value, Value)],
key: &'a str,
) -> Result<Option<I>, Error>
pub fn inner_optional_string_mut_ref_map<'a, I: FromIterator<(String, &'a mut Value)>>( document_type: &'a mut [(Value, Value)], key: &'a str, ) -> Result<Option<I>, Error>
Gets the inner map from a map and converts it to a string map
Sourcepub fn inner_optional_btree_map<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<BTreeMap<String, &'a Value>>, Error>
pub fn inner_optional_btree_map<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<BTreeMap<String, &'a Value>>, Error>
Gets the inner btree map from a map
Sourcepub fn inner_optional_index_map<'a, T>(
document_type: &'a [(Value, Value)],
key: &'a str,
sort_property: &'a str,
) -> Result<Option<IndexMap<String, &'a Value>>, Error>
pub fn inner_optional_index_map<'a, T>( document_type: &'a [(Value, Value)], key: &'a str, sort_property: &'a str, ) -> Result<Option<IndexMap<String, &'a Value>>, Error>
Gets the inner index map sorted by a specified property
Sourcepub fn inner_optional_bool_value(
document_type: &[(Value, Value)],
key: &str,
) -> Result<Option<bool>, Error>
pub fn inner_optional_bool_value( document_type: &[(Value, Value)], key: &str, ) -> Result<Option<bool>, Error>
Gets the inner bool value from a map
Sourcepub fn inner_bool_value(
document_type: &[(Value, Value)],
key: &str,
) -> Result<bool, Error>
pub fn inner_bool_value( document_type: &[(Value, Value)], key: &str, ) -> Result<bool, Error>
Gets the inner bool value from a map
Sourcepub fn inner_optional_integer_value<T>(
document_type: &[(Value, Value)],
key: &str,
) -> Result<Option<T>, Error>
pub fn inner_optional_integer_value<T>( document_type: &[(Value, Value)], key: &str, ) -> Result<Option<T>, Error>
Gets the inner integer value from a map if it exists
Sourcepub fn inner_integer_value<T>(
document_type: &[(Value, Value)],
key: &str,
) -> Result<T, Error>
pub fn inner_integer_value<T>( document_type: &[(Value, Value)], key: &str, ) -> Result<T, Error>
Gets the inner integer value from a map
Sourcepub fn inner_optional_text_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<&'a str>, Error>
pub fn inner_optional_text_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<&'a str>, Error>
Retrieves the value of a key from a map if it’s a string.
Sourcepub fn inner_text_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<&'a str, Error>
pub fn inner_text_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<&'a str, Error>
Retrieves the value of a key from a map if it’s a string.
Sourcepub fn inner_optional_hash256_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<[u8; 32]>, Error>
pub fn inner_optional_hash256_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<[u8; 32]>, Error>
Retrieves the value of a key from a map if it’s a hash256.
Sourcepub fn inner_hash256_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<[u8; 32], Error>
pub fn inner_hash256_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<[u8; 32], Error>
Retrieves the value of a key from a map if it’s a string.
Sourcepub fn inner_optional_binary_data_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<BinaryData>, Error>
pub fn inner_optional_binary_data_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<BinaryData>, Error>
Retrieves the value of a key from a map if it’s a byte array.
Sourcepub fn inner_binary_data_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<BinaryData, Error>
pub fn inner_binary_data_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<BinaryData, Error>
Retrieves the value of a key from a map if it’s a byte array.
Sourcepub fn inner_optional_bytes_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<Vec<u8>>, Error>
pub fn inner_optional_bytes_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<Vec<u8>>, Error>
Retrieves the val
Retrieves the value of a key from a map if it’s a byte array.
Sourcepub fn inner_bytes_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Vec<u8>, Error>
pub fn inner_bytes_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Vec<u8>, Error>
Retrieves the value of a key from a map if it’s a byte array.
Sourcepub fn inner_optional_bytes_slice_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<&'a [u8]>, Error>
pub fn inner_optional_bytes_slice_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<&'a [u8]>, Error>
Retrieves the value of a key from a map if it’s a byte array.
Sourcepub fn inner_optional_array_slice_value<'a>(
document_type: &'a [(Value, Value)],
key: &'a str,
) -> Result<Option<&'a [Value]>, Error>
pub fn inner_optional_array_slice_value<'a>( document_type: &'a [(Value, Value)], key: &'a str, ) -> Result<Option<&'a [Value]>, Error>
Gets the inner array value from a borrowed ValueMap
pub fn get_from_map<'a>( map: &'a [(Value, Value)], search_key: &'a str, ) -> Result<&'a Value, Error>
pub fn get_mut_from_map<'a>( map: &'a mut [(Value, Value)], search_key: &'a str, ) -> Result<&'a mut Value, Error>
Sourcepub fn get_optional_from_map<'a>(
map: &'a [(Value, Value)],
search_key: &'a str,
) -> Option<&'a Value>
pub fn get_optional_from_map<'a>( map: &'a [(Value, Value)], search_key: &'a str, ) -> Option<&'a Value>
Gets a value from a map
Sourcepub fn get_optional_mut_from_map<'a>(
map: &'a mut [(Value, Value)],
search_key: &'a str,
) -> Option<&'a mut Value>
pub fn get_optional_mut_from_map<'a>( map: &'a mut [(Value, Value)], search_key: &'a str, ) -> Option<&'a mut Value>
Gets a value from a map
Sourcepub fn insert_in_map<'a>(
map: &'a mut ValueMap,
inserting_key: &'a str,
inserting_value: Value,
)
pub fn insert_in_map<'a>( map: &'a mut ValueMap, inserting_key: &'a str, inserting_value: Value, )
Inserts into a map If the element already existed it will replace it
Sourcepub fn insert_in_map_string_value(
map: &mut ValueMap,
inserting_key: String,
inserting_value: Value,
)
pub fn insert_in_map_string_value( map: &mut ValueMap, inserting_key: String, inserting_value: Value, )
Inserts into a map If the element already existed it will replace it
Sourcepub fn push_to_map_string_value(
map: &mut ValueMap,
inserting_key: String,
inserting_value: Value,
)
pub fn push_to_map_string_value( map: &mut ValueMap, inserting_key: String, inserting_value: Value, )
Inserts into a map If the element already existed it will replace it
Source§impl Value
impl Value
pub fn remove_value_at_path(&mut self, path: &str) -> Result<Value, Error>
pub fn remove_optional_value_at_path( &mut self, path: &str, ) -> Result<Option<Value>, Error>
pub fn remove_values_matching_path( &mut self, path: &str, ) -> Result<Vec<Value>, Error>
pub fn remove_value_at_path_into<T: TryFrom<Value, Error = Error>>( &mut self, path: &str, ) -> Result<T, Error>
pub fn remove_value_at_path_as_bytes( &mut self, path: &str, ) -> Result<Vec<u8>, Error>
pub fn remove_values_at_paths<'a>( &'a mut self, paths: Vec<&'a str>, ) -> Result<BTreeMap<&'a str, Value>, Error>
pub fn remove_values_matching_paths<'a>( &'a mut self, paths: Vec<&'a str>, ) -> Result<BTreeMap<&'a str, Vec<Value>>, Error>
pub fn get_value_at_path<'a>(&'a self, path: &str) -> Result<&'a Value, Error>
pub fn get_optional_value_at_path<'a>( &'a self, path: &'a str, ) -> Result<Option<&'a Value>, Error>
pub fn get_mut_value_at_path<'a>( &'a mut self, path: &'a str, ) -> Result<&'a mut Value, Error>
pub fn get_optional_mut_value_at_path<'a>( &'a mut self, path: &'a str, ) -> Result<Option<&'a mut Value>, Error>
pub fn get_integer_at_path<T>(&self, path: &str) -> Result<T, Error>
pub fn get_optional_integer_at_path<T>( &self, path: &str, ) -> Result<Option<T>, Error>
pub fn set_value_at_full_path( &mut self, path: &str, value: Value, ) -> Result<(), Error>
pub fn set_value_at_path( &mut self, path: &str, key: &str, value: Value, ) -> Result<(), Error>
Source§impl Value
impl Value
Sourcepub fn pointer(&self, pointer: &str) -> Option<&Value>
pub fn pointer(&self, pointer: &str) -> Option<&Value>
Looks up a value by a Platform Value Pointer.
Platform Value Pointer defines a string syntax for identifying a specific value within a Platform Value document.
A Pointer is a Unicode string with the reference tokens separated by /.
Inside tokens / is replaced by ~1 and ~ is replaced by ~0. The
addressed value is returned and if there is no such value None is
returned.
For more information read RFC6901.
§Examples
let data = platform_value!({
"x": {
"y": ["z", "zz"]
}
});
assert_eq!(data.pointer("/x/y/1").unwrap(), &platform_value!("zz"));
assert_eq!(data.pointer("/a/b/c"), None);Sourcepub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value>
pub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value>
Looks up a value by a Platform Value Pointer and returns a mutable reference to that value.
Platform Value Pointer defines a string syntax for identifying a specific value within a Platform Value document.
A Pointer is a Unicode string with the reference tokens separated by /.
Inside tokens / is replaced by ~1 and ~ is replaced by ~0. The
addressed value is returned and if there is no such value None is
returned.
For more information read RFC6901.
§Example of Use
use platform_value::Value;
use platform_value::platform_value;
let mut value: Value = platform_value!({"x": 1.0, "y": 2.0});
// Check value using read-only pointer
assert_eq!(value.pointer("/x"), Some(&1.0.into()));
// Change value with direct assignment
*value.pointer_mut("/x").unwrap() = 1.5.into();
// Check that new value was written
assert_eq!(value.pointer("/x"), Some(&1.5.into()));
// Or change the value only if it exists
value.pointer_mut("/x").map(|v| *v = 1.5.into());
// "Steal" ownership of a value. Can replace with any valid Value.
let old_x = value.pointer_mut("/x").map(Value::take).unwrap();
assert_eq!(old_x, 1.5);
assert_eq!(value.pointer("/x").unwrap(), &Value::Null);Source§impl Value
impl Value
Sourcepub fn replace_at_path(
&mut self,
path: &str,
replacement_type: ReplacementType,
) -> Result<(), Error>
pub fn replace_at_path( &mut self, path: &str, replacement_type: ReplacementType, ) -> Result<(), Error>
If the Value is a Map, replaces the value at the path inside the map.
This is used to set inner values as Identifiers or BinaryData, or from Identifiers or
BinaryData to base58 or base64 strings.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
value.replace_at_path("foods.food_id", ReplacementType::Identifier).expect("expected to replace at path with identifier");
assert_eq!(value.get_value_at_path("foods.food_id"), Ok(&Value::Identifier([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
let mut tangerine_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
]
);
let mut mandarin_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
]
);
let mut oranges_value = Value::Array(
vec![
tangerine_value,
mandarin_value
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), oranges_value),
]
);
value.replace_at_path("foods[].food_id", ReplacementType::Identifier).expect("expected to replace at path with identifier");
assert_eq!(value.get_value_at_path("foods[0].food_id"), Ok(&Value::Identifier([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
Sourcepub fn replace_at_paths<'a, I: IntoIterator<Item = &'a str>>(
&mut self,
paths: I,
replacement_type: ReplacementType,
) -> Result<(), Error>
pub fn replace_at_paths<'a, I: IntoIterator<Item = &'a str>>( &mut self, paths: I, replacement_type: ReplacementType, ) -> Result<(), Error>
Calls replace_at_path for every path in a given array.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("grapes")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
(Value::Text(String::from("oranges")), Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)])),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
let paths = vec!["foods.grapes", "foods.oranges"];
value.replace_at_paths(paths, ReplacementType::Identifier).expect("expected to replace at paths with identifier");
assert_eq!(value.get_value_at_path("foods.grapes"), Ok(&Value::Identifier([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
assert_eq!(value.get_value_at_path("foods.oranges"), Ok(&Value::Identifier([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
Sourcepub fn replace_integer_type_at_path(
&mut self,
path: &str,
replacement_type: IntegerReplacementType,
) -> Result<(), Error>
pub fn replace_integer_type_at_path( &mut self, path: &str, replacement_type: IntegerReplacementType, ) -> Result<(), Error>
If the Value is a Map, replaces the value at the path inside the map.
This is used to set inner values as Identifiers or BinaryData, or from Identifiers or
BinaryData to base58 or base64 strings.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::U8(5)),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
value.replace_integer_type_at_path("foods.food_id", IntegerReplacementType::U32).expect("expected to replace at path with identifier");
assert_eq!(value.get_value_at_path("foods.food_id"), Ok(&Value::U32(5)));
let mut tangerine_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::U128(8)),
]
);
let mut mandarin_value = Value::Map(
vec![
(Value::Text(String::from("food_id")), Value::U32(2)),
]
);
let mut oranges_value = Value::Array(
vec![
tangerine_value,
mandarin_value
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), oranges_value),
]
);
value.replace_integer_type_at_path("foods[].food_id", IntegerReplacementType::U16).expect("expected to replace at path with identifier");
assert_eq!(value.get_value_at_path("foods[0].food_id"), Ok(&Value::U16(8)));
Sourcepub fn replace_integer_type_at_paths<'a, I: IntoIterator<Item = &'a str>>(
&mut self,
paths: I,
replacement_type: IntegerReplacementType,
) -> Result<(), Error>
pub fn replace_integer_type_at_paths<'a, I: IntoIterator<Item = &'a str>>( &mut self, paths: I, replacement_type: IntegerReplacementType, ) -> Result<(), Error>
Calls replace_at_path for every path in a given array.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("grapes")), Value::U16(5)),
(Value::Text(String::from("oranges")), Value::I32(6)),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
let paths = vec!["foods.grapes", "foods.oranges"];
value.replace_integer_type_at_paths(paths, IntegerReplacementType::U32).expect("expected to replace at paths with identifier");
assert_eq!(value.get_value_at_path("foods.grapes"), Ok(&Value::U32(5)));
assert_eq!(value.get_value_at_path("foods.oranges"), Ok(&Value::U32(6)));
Sourcepub fn replace_to_binary_types_of_root_value_when_setting_at_path(
&mut self,
path: &str,
identifier_paths: HashSet<&str>,
binary_paths: HashSet<&str>,
) -> Result<(), Error>
pub fn replace_to_binary_types_of_root_value_when_setting_at_path( &mut self, path: &str, identifier_paths: HashSet<&str>, binary_paths: HashSet<&str>, ) -> Result<(), Error>
replace_to_binary_types_when_setting_with_path will replace a value with a corresponding
binary type (Identifier or Binary Data) if that data is in one of the given paths.
Paths can either be terminal, or can represent an object or an array (with values) where
all subvalues must be set to the binary type.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
use platform_value::{Error, Identifier, ReplacementType, Value};
let mut inner_inner_value = Value::Map(
vec![
(Value::Text(String::from("mandarins")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
(Value::Text(String::from("tangerines")), Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)])),
]
);
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("grapes")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
(Value::Text(String::from("oranges")), inner_inner_value),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
let identifier_paths = HashSet::from(["foods.oranges.tangerines"]);
value.replace_to_binary_types_of_root_value_when_setting_at_path("foods.oranges", identifier_paths, HashSet::new()).expect("expected to replace at paths with identifier");
assert_eq!(value.get_value_at_path("foods.oranges.tangerines"), Ok(&Value::Identifier([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
Sourcepub fn replace_to_binary_types_when_setting_with_path(
&mut self,
path: &str,
identifier_paths: HashSet<&str>,
binary_paths: HashSet<&str>,
) -> Result<(), Error>
pub fn replace_to_binary_types_when_setting_with_path( &mut self, path: &str, identifier_paths: HashSet<&str>, binary_paths: HashSet<&str>, ) -> Result<(), Error>
replace_to_binary_types_when_setting_with_path will replace a value with a corresponding
binary type (Identifier or Binary Data) if that data is in one of the given paths.
Paths can either be terminal, or can represent an object or an array (with values) where
all subvalues must be set to the binary type.
Either returns Err(Error::Structure("reason")) or Err(Error::ByteLengthNot32BytesError))
if the replacement can not happen.
use platform_value::{Error, Identifier, ReplacementType, Value};
let mut inner_inner_value = Value::Map(
vec![
(Value::Text(String::from("mandarins")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
(Value::Text(String::from("tangerines")), Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)])),
]
);
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("grapes")), Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string())),
(Value::Text(String::from("oranges")), inner_inner_value),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
let identifier_paths = HashSet::from(["foods.oranges.tangerines"]);
let oranges = value.get_mut_value_at_path("foods.oranges").unwrap();
oranges.replace_to_binary_types_when_setting_with_path("foods.oranges", identifier_paths, HashSet::new()).expect("expected to replace at paths with identifier");
assert_eq!(value.get_value_at_path("foods.oranges.tangerines"), Ok(&Value::Identifier([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
Sourcepub fn clean_recursive(self) -> Result<Value, Error>
pub fn clean_recursive(self) -> Result<Value, Error>
Cleans all values and removes null inner values at any depth. if the replacement can not happen.
let mut inner_value = Value::Map(
vec![
(Value::Text(String::from("grapes")), Value::Null),
(Value::Text(String::from("oranges")), Value::I32(6)),
]
);
let mut value = Value::Map(
vec![
(Value::Text(String::from("foods")), inner_value),
]
);
value = value.clean_recursive().unwrap();
assert_eq!(value.get_optional_value_at_path("foods.grapes"), Ok(None));Source§impl Value
impl Value
Sourcepub fn into_identifier_bytes(self) -> Result<Vec<u8>, Error>
pub fn into_identifier_bytes(self) -> Result<Vec<u8>, Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.into_identifier_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
let value = Value::Text("a811".to_string());
assert_eq!(value.into_identifier_bytes(), Ok(vec![98, 155, 36]));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
assert_eq!(value.into_identifier_bytes(), Ok(vec![104, 101, 108]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_identifier_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.into_identifier_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_identifier_bytes(&self) -> Result<Vec<u8>, Error>
pub fn to_identifier_bytes(&self) -> Result<Vec<u8>, Error>
If the Value is a ref to a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.to_identifier_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
let value = Value::Text("a811".to_string());
assert_eq!(value.to_identifier_bytes(), Ok(vec![98, 155, 36]));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
assert_eq!(value.to_identifier_bytes(), Ok(vec![104, 101, 108]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.to_identifier_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.to_identifier_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_binary_bytes(self) -> Result<Vec<u8>, Error>
pub fn into_binary_bytes(self) -> Result<Vec<u8>, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.into_binary_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
let value = Value::Text("a811".to_string());
assert_eq!(value.into_binary_bytes(), Ok(vec![107, 205, 117]));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
assert_eq!(value.into_binary_bytes(), Ok(vec![104, 101, 108]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_binary_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.into_binary_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_binary_data(self) -> Result<BinaryData, Error>
pub fn into_binary_data(self) -> Result<BinaryData, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![104, 101, 108, 108, 111]))); ///
let value = Value::Text("a811".to_string());
assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![107, 205, 117])));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![104, 101, 108])));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_binary_data(), Ok(BinaryData::new(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
let value = Value::Bool(true);
assert_eq!(value.into_binary_data(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_binary_bytes(&self) -> Result<Vec<u8>, Error>
pub fn to_binary_bytes(&self) -> Result<Vec<u8>, Error>
If the Value is a ref to a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.to_binary_bytes(), Ok(vec![104, 101, 108, 108, 111])); ///
let value = Value::Text("a811".to_string());
assert_eq!(value.to_binary_bytes(), Ok(vec![107, 205, 117]));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108)]);
assert_eq!(value.to_binary_bytes(), Ok(vec![104, 101, 108]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.to_binary_bytes(), Ok(vec![5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.to_binary_bytes(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_hash256(self) -> Result<[u8; 32], Error>
pub fn into_hash256(self) -> Result<[u8; 32], Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.into_hash256(), Ok([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50])); ///
let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
assert_eq!(value.into_hash256(), Ok([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117]));
let value = Value::Text("a811".to_string());
assert_eq!(value.into_hash256(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.into_hash256(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.into_hash256(), Ok([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_hash256(), Ok([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.into_hash256(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_hash256(&self) -> Result<[u8; 32], Error>
pub fn to_hash256(&self) -> Result<[u8; 32], Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.to_hash256(), Ok([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50])); ///
let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
assert_eq!(value.to_hash256(), Ok([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117]));
let value = Value::Text("a811".to_string());
assert_eq!(value.to_hash256(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.to_hash256(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.to_hash256(), Ok([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101]));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.to_hash256(), Ok([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5]));
let value = Value::Bool(true);
assert_eq!(value.to_hash256(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_bytes_20(self) -> Result<Bytes20, Error>
pub fn into_bytes_20(self) -> Result<Bytes20, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Bytes20 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]);
assert_eq!(value.into_bytes_20(), Ok(Bytes20([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]))); ///
let value = Value::Text("WRdZY72e8yUfNJ5VC9ckzga7ysE=".to_string());
assert_eq!(value.into_bytes_20(), Ok(Bytes20([89, 23, 89, 99, 189, 158, 243, 37, 31, 52, 158, 85, 11, 215, 36, 206, 6, 187, 202, 193])));
let value = Value::Text("a811".to_string());
assert_eq!(value.into_bytes_20(), Err(Error::ByteLengthNot20BytesError("buffer was not 20 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.into_bytes_20(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 20, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104)]);
assert_eq!(value.into_bytes_20(), Ok(Bytes20([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104])));
let value = Value::Bool(true);
assert_eq!(value.into_bytes_20(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_bytes_20(&self) -> Result<Bytes20, Error>
pub fn to_bytes_20(&self) -> Result<Bytes20, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Bytes20 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]);
assert_eq!(value.to_bytes_20(), Ok(Bytes20([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108]))); ///
let value = Value::Text("WRdZY72e8yUfNJ5VC9ckzga7ysE=".to_string());
assert_eq!(value.to_bytes_20(), Ok(Bytes20([89, 23, 89, 99, 189, 158, 243, 37, 31, 52, 158, 85, 11, 215, 36, 206, 6, 187, 202, 193])));
let value = Value::Text("a811".to_string());
assert_eq!(value.to_bytes_20(), Err(Error::ByteLengthNot20BytesError("buffer was not 20 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.to_bytes_20(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 20, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104)]);
assert_eq!(value.to_bytes_20(), Ok(Bytes20([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104])));
let value = Value::Bool(true);
assert_eq!(value.to_bytes_20(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_bytes_32(self) -> Result<Bytes32, Error>
pub fn into_bytes_32(self) -> Result<Bytes32, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Bytes32 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.into_bytes_32(), Ok(Bytes32([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
let value = Value::Text("ViN2Q6crZW1IYSNjAP5smv6avijtGTr2bxMs142MnHU=".to_string());
assert_eq!(value.into_bytes_32(), Ok(Bytes32([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
let value = Value::Text("a811".to_string());
assert_eq!(value.into_bytes_32(), Err(Error::ByteLengthNot32BytesError("buffer was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.into_bytes_32(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 32, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.into_bytes_32(), Ok(Bytes32([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_bytes_32(), Ok(Bytes32([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
let value = Value::Bool(true);
assert_eq!(value.into_bytes_32(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_bytes_32(&self) -> Result<Bytes32, Error>
pub fn to_bytes_32(&self) -> Result<Bytes32, Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
let value = Value::Text("ViN2Q6crZW1IYSNjAP5smv6avijtGTr2bxMs142MnHU=".to_string());
assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
let value = Value::Text("a811".to_string());
assert_eq!(value.to_bytes_32(), Err(Error::ByteLengthNot32BytesError("buffer was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.to_bytes_32(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 32, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.to_bytes_32(), Ok(Bytes32::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
let value = Value::Bool(true);
assert_eq!(value.to_bytes_32(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_bytes_36(self) -> Result<Bytes36, Error>
pub fn into_bytes_36(self) -> Result<Bytes36, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Bytes36 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]);
assert_eq!(value.into_bytes_36(), Ok(Bytes36([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]))); ///
let value = Value::Text("sNr9aH0VUnzvKDADkuJUlZ4Yj7Yd7gbNnnOR4ANNat2g498D".to_string());
assert_eq!(value.into_bytes_36(), Ok(Bytes36([176, 218, 253, 104, 125, 21, 82, 124, 239, 40, 48, 3, 146, 226, 84, 149, 158, 24, 143, 182, 29, 238, 6, 205, 158, 115, 145, 224, 3, 77, 106, 221, 160, 227, 223, 3])));
let value = Value::Text("a811".to_string());
assert_eq!(value.into_bytes_36(), Err(Error::ByteLengthNot36BytesError("buffer was not 36 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.into_bytes_36(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 into bytes 36, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101)]);
assert_eq!(value.into_bytes_36(), Ok(Bytes36([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 101, 101, 101, 101])));
let value = Value::Bool(true);
assert_eq!(value.into_bytes_36(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_bytes_36(&self) -> Result<Bytes36, Error>
pub fn to_bytes_36(&self) -> Result<Bytes36, Error>
If the Value is a Bytes, a Text using base 64 or Vector of U8, returns the
associated Bytes36 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]);
assert_eq!(value.to_bytes_36(), Ok(Bytes36([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 50, 51, 52, 53]))); ///
let value = Value::Text("sNr9aH0VUnzvKDADkuJUlZ4Yj7Yd7gbNnnOR4ANNat2g498D".to_string());
assert_eq!(value.to_bytes_36(), Ok(Bytes36([176, 218, 253, 104, 125, 21, 82, 124, 239, 40, 48, 3, 146, 226, 84, 149, 158, 24, 143, 182, 29, 238, 6, 205, 158, 115, 145, 224, 3, 77, 106, 221, 160, 227, 223, 3])));
let value = Value::Text("a811".to_string());
assert_eq!(value.to_bytes_36(), Err(Error::ByteLengthNot36BytesError("buffer was not 36 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.to_bytes_36(), Err(Error::StructureError("value was a string, but could not be decoded from base 64 to bytes 36, error: Invalid last symbol 105, offset 5.".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101), Value::U8(101)]);
assert_eq!(value.to_bytes_36(), Ok(Bytes36([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 101, 101, 101, 101])));
let value = Value::Bool(true);
assert_eq!(value.to_bytes_36(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn into_identifier(self) -> Result<Identifier, Error>
pub fn into_identifier(self) -> Result<Identifier, Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Identifier data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.into_identifier(), Ok(Identifier::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
assert_eq!(value.into_identifier(), Ok(Identifier::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
let value = Value::Text("a811".to_string());
assert_eq!(value.into_identifier(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.into_identifier(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.into_identifier(), Ok(Identifier::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.into_identifier(), Ok(Identifier::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
let value = Value::Bool(true);
assert_eq!(value.into_identifier(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Sourcepub fn to_identifier(&self) -> Result<Identifier, Error>
pub fn to_identifier(&self) -> Result<Identifier, Error>
If the Value is a Bytes, a Text using base 58 or Vector of U8, returns the
associated Identifier data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]);
assert_eq!(value.to_identifier(), Ok(Identifier::new([104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50, 104, 101, 108, 108, 111, 32, 12, 50]))); ///
let value = Value::Text("6oFRdsUNiAtXscRn52atKYCiF8RBnH9vbUzhtzY3d83e".to_string());
assert_eq!(value.to_identifier(), Ok(Identifier::new([86, 35, 118, 67, 167, 43, 101, 109, 72, 97, 35, 99, 0, 254, 108, 154, 254, 154, 190, 40, 237, 25, 58, 246, 111, 19, 44, 215, 141, 140, 156, 117])));
let value = Value::Text("a811".to_string());
assert_eq!(value.to_identifier(), Err(Error::StructureError("value was a string, could be decoded from base 58, but was not 32 bytes long".to_string())));
let value = Value::Text("a811Ii".to_string());
assert_eq!(value.to_identifier(), Err(Error::StructureError("value was a string, but could not be decoded from base 58".to_string())));
let value = Value::Array(vec![Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101), Value::U8(108),Value::U8(104), Value::U8(101)]);
assert_eq!(value.to_identifier(), Ok(Identifier::new([104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101, 108, 104, 101])));
let value = Value::Identifier([5u8;32]);
assert_eq!(value.to_identifier(), Ok(Identifier::new([5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5,5, 5, 5,5,5,5,5,5])));
let value = Value::Bool(true);
assert_eq!(value.to_identifier(), Err(Error::StructureError("value are not bytes, a string, or an array of values representing bytes".to_string())));Source§impl Value
impl Value
Sourcepub fn into_btree_string_map(self) -> Result<BTreeMap<String, Value>, Error>
pub fn into_btree_string_map(self) -> Result<BTreeMap<String, Value>, Error>
If the Value is a Map, returns a the associated BTreeMap<String, Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.into_btree_string_map(), Ok(BTreeMap::from([(String::from("key"), Value::Float(18.))])));
let value = Value::Bool(true);
assert_eq!(value.into_btree_string_map(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn to_btree_ref_string_map(&self) -> Result<BTreeMap<String, &Value>, Error>
pub fn to_btree_ref_string_map(&self) -> Result<BTreeMap<String, &Value>, Error>
If the Value is a Map, returns a the associated BTreeMap<String, Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.to_btree_ref_string_map(), Ok(BTreeMap::from([(String::from("key"), &Value::Float(18.))])));
let value = Value::Bool(true);
assert_eq!(value.to_btree_ref_string_map(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn to_ref_string_map<'a, I: FromIterator<(String, &'a Value)>>(
&'a self,
) -> Result<I, Error>
pub fn to_ref_string_map<'a, I: FromIterator<(String, &'a Value)>>( &'a self, ) -> Result<I, Error>
If the Value is a Map, returns a the associated BTreeMap<String, Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.to_ref_string_map::<BTreeMap<_,_>>(), Ok(BTreeMap::from([(String::from("key"), &Value::Float(18.))])));
assert_eq!(value.to_ref_string_map::<Vec<(_,_)>>(), Ok(vec![(String::from("key"), &Value::Float(18.))]));
let value = Value::Bool(true);
assert_eq!(value.to_ref_string_map::<Vec<(_,_)>>(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn to_ref_string_map_mut<'a, I: FromIterator<(String, &'a mut Value)>>(
&'a mut self,
) -> Result<I, Error>
pub fn to_ref_string_map_mut<'a, I: FromIterator<(String, &'a mut Value)>>( &'a mut self, ) -> Result<I, Error>
If the Value is a Map, returns a the associated BTreeMap<String, Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.to_ref_string_map_mut::<BTreeMap<_,_>>(), Ok(BTreeMap::from([(String::from("key"), &mut Value::Float(18.))])));
assert_eq!(value.to_ref_string_map_mut::<Vec<(_,_)>>(), Ok(vec![(String::from("key"), &mut Value::Float(18.))]));
let mut value = Value::Bool(true);
assert_eq!(value.to_ref_string_map_mut::<Vec<(_,_)>>(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn map_into_btree_string_map(
map: ValueMap,
) -> Result<BTreeMap<String, Value>, Error>
pub fn map_into_btree_string_map( map: ValueMap, ) -> Result<BTreeMap<String, Value>, Error>
Takes a ValueMap which is a Vec<(Value, Value)>
Returns a BTreeMap<String, Value> as long as each Key is a String
Returns Err(Error::Structure("reason")) otherwise.
Sourcepub fn map_ref_into_btree_string_map(
map: &ValueMap,
) -> Result<BTreeMap<String, &Value>, Error>
pub fn map_ref_into_btree_string_map( map: &ValueMap, ) -> Result<BTreeMap<String, &Value>, Error>
Takes a ref to a ValueMap which is a &Vec<(Value, Value)>
Returns a BTreeMap<String, &Value> as long as each Key is a String
Returns Err(Error::Structure("reason")) otherwise.
Sourcepub fn map_ref_into_indexed_string_map<'a, T>(
map: &'a ValueMap,
sort_key: &str,
) -> Result<IndexMap<String, &'a Value>, Error>
pub fn map_ref_into_indexed_string_map<'a, T>( map: &'a ValueMap, sort_key: &str, ) -> Result<IndexMap<String, &'a Value>, Error>
Takes a ref to a ValueMap which is a &Vec<(Value, Value)>
Also takes a sort_key
Returns a IndexMap<String, &Value> as long as each Key is a String
The index map is in the order sorted by the sort key
The type T is the type of the value of the sort key
Returns Err(Error::Structure("reason")) otherwise.
Sourcepub fn map_ref_into_string_map<'a, I: FromIterator<(String, &'a Value)>>(
map: &'a ValueMap,
) -> Result<I, Error>
pub fn map_ref_into_string_map<'a, I: FromIterator<(String, &'a Value)>>( map: &'a ValueMap, ) -> Result<I, Error>
Takes a ref to a ValueMap which is a &Vec<(Value, Value)>
Returns a BTreeMap<String, &Value> as long as each Key is a String
Returns Err(Error::Structure("reason")) otherwise.
Sourcepub fn map_mut_ref_into_string_map<'a, I: FromIterator<(String, &'a mut Value)>>(
map: &'a mut ValueMap,
) -> Result<I, Error>
pub fn map_mut_ref_into_string_map<'a, I: FromIterator<(String, &'a mut Value)>>( map: &'a mut ValueMap, ) -> Result<I, Error>
Takes a ref to a ValueMap which is a &Vec<(Value, Value)>
Returns a BTreeMap<String, &Value> as long as each Key is a String
Returns Err(Error::Structure("reason")) otherwise.
Source§impl Value
impl Value
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if the Value is an Integer. Returns false otherwise.
let value = Value::U64(17);
assert!(value.is_integer());Sourcepub fn is_integer_can_fit_in_64_bits(&self) -> bool
pub fn is_integer_can_fit_in_64_bits(&self) -> bool
Returns true if the Value is an integer that fits in 64 bits (u64/i64).
Returns false otherwise.
let value = Value::U128(17);
assert!(value.is_integer_can_fit_in_64_bits());Sourcepub fn as_integer<T>(&self) -> Option<T>
pub fn as_integer<T>(&self) -> Option<T>
If the Value is a Integer, returns a reference to the associated Integer data.
Returns None otherwise.
let value = Value::U64(17);
// We can read the number
let r_value : u64 = value.as_integer().unwrap();
assert_eq!(17, r_value);Sourcepub fn into_integer<T>(self) -> Result<T, Error>
pub fn into_integer<T>(self) -> Result<T, Error>
If the Value is a Integer, returns a the associated Integer data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::U64(17);
let r_value : Result<u64,Error> = value.into_integer();
assert_eq!(r_value, Ok(17));
let value = Value::Bool(true);
let r_value : Result<u64,Error> = value.into_integer();
assert_eq!(r_value, Err(Error::StructureError("value is not an integer".to_string())));Sourcepub fn to_integer<T>(&self) -> Result<T, Error>
pub fn to_integer<T>(&self) -> Result<T, Error>
If the Value is a Integer, returns a the associated Integer data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::U64(17);
let r_value : Result<u64,Error> = value.to_integer();
assert_eq!(r_value, Ok(17));
let value = Value::Bool(true);
let r_value : Result<u64,Error> = value.to_integer();
assert_eq!(r_value, Err(Error::StructureError("value is not an integer, found bool true".to_string())));Sourcepub fn to_integer_broad_conversion<T>(&self) -> Result<T, Error>
pub fn to_integer_broad_conversion<T>(&self) -> Result<T, Error>
If the Value is an Integer, a String or a Float or even a Bool, returns the
associated Integer data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::U64(17);
let r_value : Result<u64,Error> = value.to_integer_broad_conversion();
assert_eq!(r_value, Ok(17));
let value = Value::Text("17".to_string());
let r_value : Result<u64,Error> = value.to_integer_broad_conversion();
assert_eq!(r_value, Ok(17));
let value = Value::Bool(true);
let r_value : Result<u64,Error> = value.to_integer_broad_conversion();
assert_eq!(r_value, Ok(1));Sourcepub fn is_bytes(&self) -> bool
pub fn is_bytes(&self) -> bool
Returns true if the Value is a Bytes. Returns false otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert!(value.is_bytes());Sourcepub fn is_any_bytes_type(&self) -> bool
pub fn is_any_bytes_type(&self) -> bool
Returns true if the Value is a Bytes. Returns false otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert!(value.is_any_bytes_type());
let value = Value::Identifier([1u8;32]);
assert!(value.is_any_bytes_type());
let value = Value::Bytes20([1u8;20]);
assert!(value.is_any_bytes_type());
let value = Value::Bytes32([1u8;32]);
assert!(value.is_any_bytes_type());
let value = Value::Bytes36([1u8;36]);
assert!(value.is_any_bytes_type());Sourcepub fn as_bytes(&self) -> Option<&Vec<u8>>
pub fn as_bytes(&self) -> Option<&Vec<u8>>
If the Value is a Bytes, returns a reference to the associated bytes vector.
Returns None otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(std::str::from_utf8(value.as_bytes().unwrap()).unwrap(), "hello");Sourcepub fn as_bytes_mut(&mut self) -> Option<&mut Vec<u8>>
pub fn as_bytes_mut(&mut self) -> Option<&mut Vec<u8>>
If the Value is a Bytes, returns a mutable reference to the associated bytes vector.
Returns None otherwise.
let mut value = Value::Bytes(vec![104, 101, 108, 108, 111]);
value.as_bytes_mut().unwrap().clear();
assert_eq!(value, Value::Bytes(vec![]));Sourcepub fn into_bytes(self) -> Result<Vec<u8>, Error>
pub fn into_bytes(self) -> Result<Vec<u8>, Error>
If the Value is a Bytes, returns a the associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.into_bytes(), Ok(vec![104, 101, 108, 108, 111]));
let value = Value::Bool(true);
assert_eq!(value.into_bytes(), Err(Error::StructureError("value are not bytes".to_string())));Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, Error>
pub fn to_bytes(&self) -> Result<Vec<u8>, Error>
If the Value is a ref to Bytes, returns a the associated Vec<u8> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.to_bytes(), Ok(vec![104, 101, 108, 108, 111]));
let value = Value::Bool(true);
assert_eq!(value.to_bytes(), Err(Error::StructureError("ref value are not bytes found bool true instead".to_string())));Sourcepub fn to_binary_data(&self) -> Result<BinaryData, Error>
pub fn to_binary_data(&self) -> Result<BinaryData, Error>
If the Value is a ref to Bytes, returns a the associated BinaryData data as Ok.
BinaryData wraps VecErr(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.to_binary_data(), Ok(BinaryData::new(vec![104, 101, 108, 108, 111])));
let value = Value::Bool(true);
assert_eq!(value.to_binary_data(), Err(Error::StructureError("ref value are not bytes found bool true instead".to_string())));Sourcepub fn as_bytes_slice(&self) -> Result<&[u8], Error>
pub fn as_bytes_slice(&self) -> Result<&[u8], Error>
If the Value is a ref to Bytes, returns a the associated &[u8] data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.as_bytes_slice(), Ok(vec![104, 101, 108, 108, 111].as_slice()));
let value = Value::Bool(true);
assert_eq!(value.as_bytes_slice(), Err(Error::StructureError("ref value are not bytes slice".to_string())));Sourcepub fn is_float(&self) -> bool
pub fn is_float(&self) -> bool
Returns true if the Value is a Float. Returns false otherwise.
let value = Value::Float(17.0.into());
assert!(value.is_float());Sourcepub fn as_float(&self) -> Option<f64>
pub fn as_float(&self) -> Option<f64>
If the Value is a Float, returns a reference to the associated float data.
Returns None otherwise.
let value = Value::Float(17.0.into());
// We can read the float number
assert_eq!(value.as_float().unwrap(), 17.0_f64);Sourcepub fn into_float(self) -> Result<f64, Error>
pub fn into_float(self) -> Result<f64, Error>
If the Value is a Float, returns a the associated f64 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Float(17.);
assert_eq!(value.into_float(), Ok(17.));
let value = Value::Bool(true);
assert_eq!(value.into_float(), Err(Error::StructureError("value is not a float".to_string())));Sourcepub fn to_float(&self) -> Result<f64, Error>
pub fn to_float(&self) -> Result<f64, Error>
If the Value is a Float, returns a the associated f64 data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Float(17.);
assert_eq!(value.to_float(), Ok(17.));
let value = Value::Bool(true);
assert_eq!(value.to_float(), Err(Error::StructureError("value is not a float".to_string())));Sourcepub fn is_text(&self) -> bool
pub fn is_text(&self) -> bool
Returns true if the Value is a Text. Returns false otherwise.
let value = Value::Text(String::from("hello"));
assert!(value.is_text());Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
If the Value is a Text, returns a reference to the associated String data.
Returns None otherwise.
let value = Value::Text(String::from("hello"));
// We can read the String
assert_eq!(value.as_text().unwrap(), "hello");Sourcepub fn as_text_mut(&mut self) -> Option<&mut String>
pub fn as_text_mut(&mut self) -> Option<&mut String>
If the Value is a Text, returns a mutable reference to the associated String data.
Returns None otherwise.
let mut value = Value::Text(String::from("hello"));
value.as_text_mut().unwrap().clear();
assert_eq!(value.as_text().unwrap(), &String::from(""));Sourcepub fn into_text(self) -> Result<String, Error>
pub fn into_text(self) -> Result<String, Error>
If the Value is a String, returns a the associated String data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Text(String::from("hello"));
assert_eq!(value.into_text().as_deref(), Ok("hello"));
let value = Value::Bool(true);
assert_eq!(value.into_text(), Err(Error::StructureError("value is not a string".to_string())));Sourcepub fn to_text(&self) -> Result<String, Error>
pub fn to_text(&self) -> Result<String, Error>
If the Value is a String, returns a the associated String data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Text(String::from("hello"));
assert_eq!(value.to_text().as_deref(), Ok("hello"));
let value = Value::Bool(true);
assert_eq!(value.to_text(), Err(Error::StructureError("value is not a string".to_string())));Sourcepub fn to_str(&self) -> Result<&str, Error>
pub fn to_str(&self) -> Result<&str, Error>
If the Value is a String, returns a the associated &str data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Text(String::from("hello"));
assert_eq!(value.to_str(), Ok("hello"));
let value = Value::Bool(true);
assert_eq!(value.to_str(), Err(Error::StructureError("value is not a string".to_string())));Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
If the Value is a String, returns a reference to the associated String data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Text(String::from("hello"));
assert_eq!(value.as_str(), Some("hello"));
let value = Value::Bool(true);
assert_eq!(value.as_str(), None);Sourcepub fn is_bool(&self) -> bool
pub fn is_bool(&self) -> bool
Returns true if the Value is a Bool. Returns false otherwise.
let value = Value::Bool(false);
assert!(value.is_bool());Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
If the Value is a Bool, returns a copy of the associated boolean value. Returns None
otherwise.
let value = Value::Bool(false);
assert_eq!(value.as_bool().unwrap(), false);Sourcepub fn into_bool(self) -> Result<bool, Error>
pub fn into_bool(self) -> Result<bool, Error>
If the Value is a Bool, returns a the associated bool data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bool(false);
assert_eq!(value.into_bool(), Ok(false));
let value = Value::Float(17.);
assert_eq!(value.into_bool(), Err(Error::StructureError("value is not a bool".to_string())));Sourcepub fn to_bool(&self) -> Result<bool, Error>
pub fn to_bool(&self) -> Result<bool, Error>
If the Value is a Bool, returns a the associated bool data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let value = Value::Bool(false);
assert_eq!(value.to_bool(), Ok(false));
let value = Value::Float(17.);
assert_eq!(value.to_bool(), Err(Error::StructureError("value is not a bool".to_string())));Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if the Value is a Null. Returns false otherwise.
let value = Value::Null;
assert!(value.is_null());Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Returns true if the Value is an Array. Returns false otherwise.
let value = Value::Array(
vec![
Value::Text(String::from("foo")),
Value::Text(String::from("bar"))
]
);
assert!(value.is_array());Sourcepub fn as_array(&self) -> Option<&Vec<Value>>
pub fn as_array(&self) -> Option<&Vec<Value>>
If the Value is an Array, returns a reference to the associated vector. Returns None
otherwise.
let value = Value::Array(
vec![
Value::Text(String::from("foo")),
Value::Text(String::from("bar"))
]
);
// The length of `value` is 2 elements.
assert_eq!(value.as_array().unwrap().len(), 2);Sourcepub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>
If the Value is an Array, returns a mutable reference to the associated vector.
Returns None otherwise.
let mut value = Value::Array(
vec![
Value::Text(String::from("foo")),
Value::Text(String::from("bar"))
]
);
value.as_array_mut().unwrap().clear();
assert_eq!(value, Value::Array(vec![]));Sourcepub fn to_array_mut(&mut self) -> Result<&mut Vec<Value>, Error>
pub fn to_array_mut(&mut self) -> Result<&mut Vec<Value>, Error>
If the Value is an Array, returns a mutable reference to the associated vector.
Returns None otherwise.
let mut value = Value::Array(
vec![
Value::Text(String::from("foo")),
Value::Text(String::from("bar"))
]
);
value.to_array_mut().unwrap().clear();
assert_eq!(value, Value::Array(vec![]));Sourcepub fn to_array_slice(&self) -> Result<&[Value], Error>
pub fn to_array_slice(&self) -> Result<&[Value], Error>
If the Value is a Array, returns a the associated &[Value] slice as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Array(
vec![
Value::U64(17),
Value::Float(18.),
]
);
assert_eq!(value.to_array_slice(), Ok(vec![Value::U64(17), Value::Float(18.)].as_slice()));
let value = Value::Bool(true);
assert_eq!(value.to_array_slice(), Err(Error::StructureError("value is not an array got bool true".to_string())));Sourcepub fn to_array_ref(&self) -> Result<&Vec<Value>, Error>
pub fn to_array_ref(&self) -> Result<&Vec<Value>, Error>
If the Value is a Array, returns a the associated Vec<&Value> array as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Array(
vec![
Value::U64(17),
Value::Float(18.),
]
);
assert_eq!(value.to_array_ref(), Ok(&vec![Value::U64(17), Value::Float(18.)]));
let value = Value::Bool(true);
assert_eq!(value.to_array_ref(), Err(Error::StructureError("value is not an array got bool true".to_string())));Sourcepub fn to_array_owned(&self) -> Result<Vec<Value>, Error>
pub fn to_array_owned(&self) -> Result<Vec<Value>, Error>
If the Value is a Array, returns a the associated Vec<Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Array(
vec![
Value::U64(17),
Value::Float(18.),
]
);
assert_eq!(value.to_array_owned(), Ok(vec![Value::U64(17), Value::Float(18.)]));
let value = Value::Bool(true);
assert_eq!(value.to_array_owned(), Err(Error::StructureError("value is not an owned array got bool true".to_string())));Sourcepub fn into_array(self) -> Result<Vec<Value>, Error>
pub fn into_array(self) -> Result<Vec<Value>, Error>
If the Value is a Array, returns a the associated Vec<Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Array(
vec![
Value::U64(17),
Value::Float(18.),
]
);
assert_eq!(value.into_array(), Ok(vec![Value::U64(17), Value::Float(18.)]));
let value = Value::Bool(true);
assert_eq!(value.into_array(), Err(Error::StructureError("value is not an array (into) got bool true".to_string())));Sourcepub fn as_slice(&self) -> Result<&[Value], Error>
pub fn as_slice(&self) -> Result<&[Value], Error>
If the Value is a Array, returns a the associated Vec<Value> data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Array(
vec![
Value::U64(17),
Value::Float(18.),
]
);
assert_eq!(value.as_slice(), Ok(vec![Value::U64(17), Value::Float(18.)].as_slice()));
let value = Value::Bool(true);
assert_eq!(value.as_slice(), Err(Error::StructureError("value is not a slice got bool true".to_string())));Sourcepub fn is_map(&self) -> bool
pub fn is_map(&self) -> bool
Returns true if the Value is a Map. Returns false otherwise.
let value = Value::Map(
vec![
(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
]
);
assert!(value.is_map());Sourcepub fn as_map(&self) -> Option<&Vec<(Value, Value)>>
pub fn as_map(&self) -> Option<&Vec<(Value, Value)>>
If the Value is a Map, returns a reference to the associated Map data. Returns None
otherwise.
let value = Value::Map(
vec![
(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
]
);
// The length of data is 1 entry (1 key/value pair).
assert_eq!(value.as_map().unwrap().len(), 1);
// The content of the first element is what we expect
assert_eq!(
value.as_map().unwrap().get(0).unwrap(),
&(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
);Sourcepub fn as_map_mut(&mut self) -> Option<&mut Vec<(Value, Value)>>
pub fn as_map_mut(&mut self) -> Option<&mut Vec<(Value, Value)>>
If the Value is a Map, returns a mutable reference to the associated Map Data.
Returns None otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
]
);
value.as_map_mut().unwrap().clear();
assert_eq!(value, Value::Map(vec![]));
assert_eq!(value.as_map().unwrap().len(), 0);Sourcepub fn to_map_mut(&mut self) -> Result<&mut ValueMap, Error>
pub fn to_map_mut(&mut self) -> Result<&mut ValueMap, Error>
If the Value is a Map, returns a mutable reference to the associated Map Data.
Returns Err otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
]
);
value.to_map_mut().unwrap().clear();
assert_eq!(value, Value::Map(vec![]));
assert_eq!(value.as_map().unwrap().len(), 0);Sourcepub fn into_map(self) -> Result<ValueMap, Error>
pub fn into_map(self) -> Result<ValueMap, Error>
If the Value is a Map, returns a the associated ValueMap which is a Vec<(Value, Value)>
data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.into_map(), Ok(vec![(Value::Text(String::from("key")), Value::Float(18.))]));
let value = Value::Bool(true);
assert_eq!(value.into_map(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn to_map(&self) -> Result<&ValueMap, Error>
pub fn to_map(&self) -> Result<&ValueMap, Error>
If the Value is a Map, returns a the associated ValueMap which is a Vec<(Value, Value)>
data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.to_map(), Ok(&vec![(Value::Text(String::from("key")), Value::Float(18.))]));
let value = Value::Bool(true);
assert_eq!(value.to_map(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn to_map_ref(&self) -> Result<&ValueMap, Error>
pub fn to_map_ref(&self) -> Result<&ValueMap, Error>
If the Value is a Map, returns the associated ValueMap ref which is a &Vec<(Value, Value)>
data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.to_map_ref(), Ok(&vec![(Value::Text(String::from("key")), Value::Float(18.))]));
let value = Value::Bool(true);
assert_eq!(value.to_map_ref(), Err(Error::StructureError("value is not a map".to_string())))Sourcepub fn as_map_mut_ref(&mut self) -> Result<&mut ValueMap, Error>
pub fn as_map_mut_ref(&mut self) -> Result<&mut ValueMap, Error>
If the Value is a Map, returns the associated ValueMap ref which is a &Vec<(Value, Value)>
data as Ok.
Returns Err(Error::Structure("reason")) otherwise.
let mut value = Value::Map(
vec![
(Value::Text(String::from("key")), Value::Float(18.)),
]
);
assert_eq!(value.as_map_mut_ref(), Ok(&mut vec![(Value::Text(String::from("key")), Value::Float(18.))]));
let mut value = Value::Bool(true);
assert_eq!(value.as_map_mut_ref(), Err(Error::StructureError("value is not a map".to_string())))Trait Implementations§
Source§impl<'__de, __Context> BorrowDecode<'__de, __Context> for Value
impl<'__de, __Context> BorrowDecode<'__de, __Context> for Value
Source§fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl From<&BinaryData> for Value
impl From<&BinaryData> for Value
Source§fn from(value: &BinaryData) -> Self
fn from(value: &BinaryData) -> Self
Source§impl From<&Identifier> for Value
impl From<&Identifier> for Value
Source§fn from(value: &Identifier) -> Self
fn from(value: &Identifier) -> Self
Source§impl<'a> From<&'a Value> for Unexpected<'a>
impl<'a> From<&'a Value> for Unexpected<'a>
Source§impl<const N: usize> From<[(String, Value); N]> for Value
impl<const N: usize> From<[(String, Value); N]> for Value
Source§fn from(arr: [(String, Value); N]) -> Self
fn from(arr: [(String, Value); N]) -> Self
Converts a [(String, Value); N] into a Value.
use platform_value::Value;
let map1 = Value::from([("1".to_string(), Value::from(2)), ("3".to_string(), Value::from(4))]);
let map2: Value = [("1".to_string(), Value::from(2)), ("3".to_string(), Value::from(4))].into();
assert_eq!(map1, map2);Source§impl<const N: usize> From<[(Value, Value); N]> for Value
impl<const N: usize> From<[(Value, Value); N]> for Value
Source§fn from(arr: [(Value, Value); N]) -> Self
fn from(arr: [(Value, Value); N]) -> Self
Converts a [(Value, Value); N] into a Value.
use platform_value::Value;
let map1 = Value::from([(Value::from(1), Value::from(2)), (Value::from(3), Value::from(4))]);
let map2: Value = [(Value::from(1), Value::from(2)), (Value::from(3), Value::from(4))].into();
assert_eq!(map1, map2);Source§impl From<BinaryData> for Value
impl From<BinaryData> for Value
Source§fn from(value: BinaryData) -> Self
fn from(value: BinaryData) -> Self
Source§impl From<Identifier> for Value
impl From<Identifier> for Value
Source§fn from(value: Identifier) -> Self
fn from(value: Identifier) -> Self
Source§impl<I> Index<I> for Valuewhere
I: Index,
impl<I> Index<I> for Valuewhere
I: Index,
Source§fn index(&self, index: I) -> &Value
fn index(&self, index: I) -> &Value
Index into a serde_json::Value using the syntax value[0] or
value["k"].
Returns Value::Null if the type of self does not match the type of
the index, for example if the index is a string and self is an array
or a number. Also returns Value::Null if the given key does not exist
in the map or the given index is not within the bounds of the array.
For retrieving deeply nested values, you should have a look at the
Value::pointer method.
§Examples
let data = platform_value!({
"x": {
"y": ["z", "zz"]
}
});
assert_eq!(data["x"]["y"], platform_value!(["z", "zz"]));
assert_eq!(data["x"]["y"][0], platform_value!("z"));
assert_eq!(data["a"], platform_value!(null)); // returns null for undefined values
assert_eq!(data["a"]["b"], platform_value!(null)); // does not panicSource§impl<I> IndexMut<I> for Valuewhere
I: Index,
impl<I> IndexMut<I> for Valuewhere
I: Index,
Source§fn index_mut(&mut self, index: I) -> &mut Value
fn index_mut(&mut self, index: I) -> &mut Value
Write into a serde_json::Value using the syntax value[0] = ... or
value["k"] = ....
If the index is a number, the value must be an array of length bigger than the index. Indexing into a value that is not an array or an array that is too small will panic.
If the index is a string, the value must be an object or null which is treated like an empty object. If the key is not already present in the object, it will be inserted with a value of null. Indexing into a value that is neither an object nor null will panic.
§Examples
let mut data = platform_value!({ "x": 0 });
// replace an existing key
data["x"] = platform_value!(1);
// insert a new key
data["y"] = platform_value!([false, false, false]);
// replace an array value
data["y"][0] = platform_value!(true);
// inserted a deeply nested key
data["a"]["b"]["c"]["d"] = platform_value!(true);
println!("{}", data);