Value

Enum Value 

Source
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 of f64 in the Rust standard library, including NaN and Infinity handling.

  • Int(i64) – A signed 64‑bit integer. Provides the full range of i64 values.

  • Bool(bool) – A boolean value, either true or false.

  • String(Str) – A string value that may be a static slice, an Arc<String>, or an owned String. See Str for details.

  • Map(Map) – An associative container mapping string keys to Values. See Map for the underlying implementation.

  • Array(Array) – An ordered list of Values. See Array for 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

Source

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(_)));
Source

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");
Source

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 contained f64.
  • Int – casts the integer to f64.
  • String – parses the string as f64; on parse failure 0.0 is returned.
  • Booltrue becomes 1.0, false becomes 0.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);
Source

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 as i64; on parse failure 0 is returned.
  • Booltrue becomes 1, false becomes 0.
  • 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);
Source

pub fn as_bool(&self) -> bool

Returns the boolean representation of the value.

Conversion rules:

  • Bool – returns the contained boolean.
  • Int0 is false, any other integer is true.
  • Float0.0 is false, any other number is true.
  • String – empty string is false, otherwise true.
  • Map / Array – empty collections are false, otherwise true.
§Examples
assert!(Value::Bool(true).as_bool());
assert!(!Value::Int(0).as_bool());
assert!(Value::String("text".into()).as_bool());
Source

pub fn as_map(&self) -> Option<&Map>

Returns a reference to the underlying map if the value is a Map.

§Returns
  • Some(&Map) – when the variant is Value::Map.
  • None – for all other variants.
§Examples
let map = Map::new();
let v = Value::Map(map);
assert!(v.as_map().is_some());
Source

pub fn as_array(&self) -> Option<&Array>

Returns a reference to the underlying array if the value is an Array.

§Returns
  • Some(&Array) – when the variant is Value::Array.
  • None – for all other variants.
§Examples
let arr = Array::new();
let v = Value::Array(arr);
assert!(v.as_array().is_some());
Source

pub fn is_float(&self) -> bool

Returns true if the value is a Float.

Source

pub fn is_int(&self) -> bool

Returns true if the value is an Int.

Source

pub fn is_bool(&self) -> bool

Returns true if the value is a Bool.

Source

pub fn is_string(&self) -> bool

Returns true if the value is a String.

Source

pub fn is_map(&self) -> bool

Returns true if the value is a Map.

Source

pub fn is_array(&self) -> bool

Returns true if the value is an Array.

Source

pub fn new_map() -> Self

Creates a new empty Value::Map.

Source

pub fn new_array() -> Self

Creates a new empty Value::Array.

Source

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

Source

pub fn get_index(&self, index: usize) -> Option<&Value>

Safely gets a value from an array by index

Source

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);
Source

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"}"#);
Source

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");
Source§

impl Value

Source

pub fn as_map_mut(&mut self) -> Option<&mut Map>

Returns a mutable reference to the underlying map if the value is a Map.

Source

pub fn as_array_mut(&mut self) -> Option<&mut Array>

Returns a mutable reference to the underlying array if the value is an Array.

Source

pub fn is_none(&self) -> bool

Returns true if the value is None.

Source

pub fn take(&mut self) -> Value

Takes the value out, leaving Value::None in its place.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Value

Available on crate feature serde only.
Source§

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

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

impl Display for Value

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<&String> for Value

Source§

fn from(s: &String) -> Self

Converts to this type from the input type.
Source§

impl From<&'static str> for Value

Source§

fn from(s: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<Arc<String>> for Value

Source§

fn from(s: Arc<String>) -> Self

Converts to this type from the input type.
Source§

impl From<Array> for Value

Source§

fn from(a: Array) -> Self

Converts to this type from the input type.
Source§

impl From<Map> for Value

Source§

fn from(m: Map) -> Self

Converts to this type from the input type.
Source§

impl From<Str> for Value

Source§

fn from(s: Str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(f: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(f: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Value

Source§

fn from(i: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Value

Source§

fn from(u: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(u: u64) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Value> for Array

Source§

fn from_iter<T: IntoIterator<Item = Value>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Index<&str> for Value

Source§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, index: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Value

Source§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<&str> for Value

Source§

fn index_mut(&mut self, index: &str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Value

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Into<bool> for Value

Source§

fn into(self) -> bool

Converts this type into the (usually inferred) input type.
Source§

impl Into<f64> for Value

Source§

fn into(self) -> f64

Converts this type into the (usually inferred) input type.
Source§

impl Into<i128> for Value

Source§

fn into(self) -> i128

Converts this type into the (usually inferred) input type.
Source§

impl Into<i16> for Value

Source§

fn into(self) -> i16

Converts this type into the (usually inferred) input type.
Source§

impl Into<i32> for Value

Source§

fn into(self) -> i32

Converts this type into the (usually inferred) input type.
Source§

impl Into<i64> for Value

Source§

fn into(self) -> i64

Converts this type into the (usually inferred) input type.
Source§

impl Into<i8> for Value

Source§

fn into(self) -> i8

Converts this type into the (usually inferred) input type.
Source§

impl Into<isize> for Value

Source§

fn into(self) -> isize

Converts this type into the (usually inferred) input type.
Source§

impl Into<usize> for Value

Source§

fn into(self) -> usize

Converts this type into the (usually inferred) input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Value

Available on crate feature serde only.
Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<Value> for String

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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