pub enum Value {
Float(f64),
Int(i64),
Bool(bool),
String(Str),
Map(Map),
Array(Array),
None,
}Expand description
A dynamically‑typed value used throughout the library.
This enum can represent the primitive scalar types supported by the
library as well as compound collection types. It is deliberately
exhaustive so that a Value can be stored in heterogeneous containers
(e.g., maps or arrays) without losing type information.
§Variants
-
Float(f64)– A 64‑bit floating‑point number. Mirrors the behaviour off64in the Rust standard library, includingNaNandInfinityhandling. -
Int(i64)– A signed 64‑bit integer. Provides the full range ofi64values. -
Bool(bool)– A boolean value, eithertrueorfalse. -
String(Str)– A string value that may be a static slice, anArc<String>, or an ownedString. SeeStrfor details. -
Map(Map)– An associative container mapping string keys toValues. SeeMapfor the underlying implementation. -
Array(Array)– An ordered list ofValues. SeeArrayfor the underlying implementation.
§Examples
let v = Value::from(3.14);
assert!(matches!(v, Value::Float(_)));
let s = Value::from("hello");
assert_eq!(s.as_str(), "hello");
assert_eq!(v.as_str(), "3.14");Variants§
Float(f64)
64‑bit floating‑point number.
Int(i64)
64‑bit signed integer.
Bool(bool)
Boolean value.
String(Str)
String value, using the library’s flexible Str type.
Map(Map)
Map (dictionary) of string keys to Values.
Array(Array)
Array (list) of Values.
None
None value
Implementations§
Source§impl Value
impl Value
Sourcepub fn from<T: Into<Value>>(value: T) -> Self
pub fn from<T: Into<Value>>(value: T) -> Self
Creates a Value from any type that implements Into<Value>.
///
This is a convenience method that allows for easy conversion of
common types into Value instances.
§Examples
let v_float = Value::from(3.14);
let v_int = Value::from(42);
let v_bool = Value::from(true);
let v_string = Value::from("hello");
assert!(matches!(v_float, Value::Float(_)));
assert!(matches!(v_int, Value::Int(_)));
assert!(matches!(v_bool, Value::Bool(_)));
assert!(matches!(v_string, Value::String(_)));Sourcepub fn as_str(&self) -> Cow<'_, str>
pub fn as_str(&self) -> Cow<'_, str>
Returns a string representation of the value.
If the value is a String, a borrowed &str is returned.
Otherwise the value is formatted with to_string() and an owned
String is returned inside a Cow::Owned.
§Examples
let v = Value::String(Str::from_static("hello"));
assert_eq!(v.as_str(), "hello");
let v = Value::Int(42);
assert_eq!(v.as_str(), "42");Sourcepub fn as_float(&self) -> f64
pub fn as_float(&self) -> f64
Attempts to interpret the value as a 64‑bit floating‑point number.
The conversion rules are:
Float– returns the containedf64.Int– casts the integer tof64.String– parses the string asf64; on parse failure0.0is returned.Bool–truebecomes1.0,falsebecomes0.0.- Any other variant – returns
0.0.
§Examples
assert_eq!(Value::Float(3.14).as_float(), 3.14);
assert_eq!(Value::Int(2).as_float(), 2.0);
assert_eq!(Value::Bool(true).as_float(), 1.0);
assert_eq!(Value::String("2.5".into()).as_float(), 2.5);Sourcepub fn as_int(&self) -> i64
pub fn as_int(&self) -> i64
Attempts to interpret the value as a signed 64‑bit integer.
The conversion rules are:
Int– returns the contained integer.Float– truncates the floating‑point value.String– parses the string asi64; on parse failure0is returned.Bool–truebecomes1,falsebecomes0.- Any other variant – returns
0.
§Examples
assert_eq!(Value::Int(7).as_int(), 7);
assert_eq!(Value::Float(3.9).as_int(), 3);
assert_eq!(Value::Bool(false).as_int(), 0);
assert_eq!(Value::String("42".into()).as_int(), 42);Sourcepub fn as_bool(&self) -> bool
pub fn as_bool(&self) -> bool
Returns the boolean representation of the value.
Conversion rules:
Bool– returns the contained boolean.Int–0isfalse, any other integer istrue.Float–0.0isfalse, any other number istrue.String– empty string isfalse, otherwisetrue.Map/Array– empty collections arefalse, otherwisetrue.
§Examples
assert!(Value::Bool(true).as_bool());
assert!(!Value::Int(0).as_bool());
assert!(Value::String("text".into()).as_bool());Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Safely gets a value from a map by key, returning None if not a map or key doesn’t exist
Sourcepub fn get_index(&self, index: usize) -> Option<&Value>
pub fn get_index(&self, index: usize) -> Option<&Value>
Safely gets a value from an array by index
Sourcepub fn from_json(s: &str) -> Result<Self, Error>
pub fn from_json(s: &str) -> Result<Self, Error>
Deserialises a JSON string into a Value.
Returns a serde_json::Error if the JSON cannot be parsed.
§Examples
use anyval::Value;
let json = r#"{"key":"value","num":42}"#;
let val = Value::from_json(json).unwrap();
assert_eq!(val["key"].as_str(), "value");
assert_eq!(val["num"].as_int(), 42);§Array example
use anyval::Value;
let json = r#"["value",42,true]"#;
let val = Value::from_json(json).unwrap();
assert_eq!(val[0].as_str(), "value");
assert_eq!(val[1].as_int(), 42);
assert_eq!(val[2].as_bool(), true);Sourcepub fn to_json(&self) -> Result<String, Error>
pub fn to_json(&self) -> Result<String, Error>
Serialises the value to a JSON string.
Returns a serde_json::Error if the value cannot be serialised.
§Examples
use anyval::Value;
let val = Value::from("hello");
let json = val.to_json().unwrap();
assert_eq!(json, r#""hello""#);§Map example
use anyval::{Value, Map};
let mut map = Map::new();
map["key"] = "value".into();
let val = Value::Map(map);
let json = val.to_json().unwrap();
assert_eq!(json, r#"{"key":"value"}"#);Sourcepub fn to_json_writer<W: Write>(&self, writer: W) -> Result<(), Error>
pub fn to_json_writer<W: Write>(&self, writer: W) -> Result<(), Error>
Serialises the value to a JSON writer.
Returns a serde_json::Error if the value cannot be serialised.
§Examples
use anyval::Value;
let val = Value::from(42);
let mut buffer = Vec::new();
val.to_json_writer(&mut buffer).unwrap();
assert_eq!(String::from_utf8(buffer).unwrap(), "42");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Value
serde only.