Value

Enum Value 

Source
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Text(SmartStr),
    Array(Arc<[Value]>),
    Object(Arc<IndexMap<Key, Value>>),
    Binary(Arc<[u8]>),
}
Expand description

Unified runtime representation for all parameter values.

This enum covers all possible value types that parameters can hold. Collections use Arc for cheap cloning and thread-safe sharing.

§Examples

use paramdef::core::Value;

// Primitive values
let null = Value::Null;
let boolean = Value::Bool(true);
let integer = Value::Int(42);
let float = Value::Float(3.14);
let text = Value::text("hello");

// Type checking
assert!(null.is_null());
assert!(boolean.is_bool());
assert_eq!(integer.as_int(), Some(42));

Variants§

§

Null

Absence of a value.

§

Bool(bool)

Boolean value.

§

Int(i64)

64-bit signed integer.

§

Float(f64)

64-bit floating point.

§

Text(SmartStr)

Text string using stack-optimized storage.

§

Array(Arc<[Value]>)

Ordered array of values.

§

Object(Arc<IndexMap<Key, Value>>)

Key-value object with insertion-order preservation.

Uses IndexMap to maintain the order in which fields were inserted, providing consistent serialization and iteration order.

§

Binary(Arc<[u8]>)

Binary data.

Implementations§

Source§

impl Value

Source

pub const fn is_null(&self) -> bool

Returns true if this is a Null value.

Source

pub const fn is_bool(&self) -> bool

Returns true if this is a Bool value.

Source

pub const fn is_int(&self) -> bool

Returns true if this is an Int value.

Source

pub const fn is_float(&self) -> bool

Returns true if this is a Float value.

Source

pub const fn is_text(&self) -> bool

Returns true if this is a Text value.

Source

pub const fn is_array(&self) -> bool

Returns true if this is an Array value.

Source

pub const fn is_object(&self) -> bool

Returns true if this is an Object value.

Source

pub const fn is_binary(&self) -> bool

Returns true if this is a Binary value.

Source

pub const fn is_numeric(&self) -> bool

Returns true if this is a numeric value (Int or Float).

Source

pub const fn as_bool(&self) -> Option<bool>

Returns the boolean value if this is a Bool.

Source

pub const fn as_int(&self) -> Option<i64>

Returns the integer value if this is an Int.

Source

pub const fn as_float(&self) -> Option<f64>

Returns the float value if this is a Float.

Source

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

Returns the text value if this is a Text.

Source

pub fn as_array(&self) -> Option<&[Value]>

Returns the array if this is an Array.

Source

pub fn as_object(&self) -> Option<&IndexMap<Key, Value>>

Returns the object if this is an Object.

Source

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

Returns the binary data if this is a Binary.

Source

pub fn as_f64(&self) -> Option<f64>

Returns the numeric value as f64, converting if necessary.

Source

pub fn as_i64(&self) -> Option<i64>

Returns the numeric value as i64, converting if possible.

Source§

impl Value

Source

pub fn is_empty(&self) -> bool

Returns true if this value is considered empty.

  • Null is empty
  • Empty string is empty
  • Empty array is empty
  • Empty object is empty
  • Empty binary is empty
§Examples
use paramdef::core::Value;

assert!(Value::Null.is_empty());
assert!(Value::text("").is_empty());
assert!(Value::array([]).is_empty());
assert!(!Value::text("hello").is_empty());
Source

pub const fn type_name(&self) -> &'static str

Returns the type name as a string.

§Examples
use paramdef::core::Value;

assert_eq!(Value::Null.type_name(), "null");
assert_eq!(Value::Bool(true).type_name(), "bool");
assert_eq!(Value::Int(42).type_name(), "int");
assert_eq!(Value::text("hello").type_name(), "text");
Source§

impl Value

Source

pub fn text(s: impl Into<SmartStr>) -> Self

Creates a text value from a string.

§Examples
use paramdef::core::Value;

let value = Value::text("hello");
assert_eq!(value.as_text(), Some("hello"));
Source

pub fn array(values: impl IntoIterator<Item = Value>) -> Self

Creates an array value from an iterator.

Uses the iterator’s size_hint() to pre-allocate the Vec, avoiding reallocation during construction.

§Examples
use paramdef::core::Value;

let value = Value::array([Value::Int(1), Value::Int(2), Value::Int(3)]);
assert_eq!(value.as_array().map(|a| a.len()), Some(3));
Source

pub fn array_with_capacity( capacity: usize, values: impl IntoIterator<Item = Value>, ) -> Self

Creates an array value with pre-allocated capacity.

Use this when you know the number of elements in advance to avoid reallocation. For dynamic sizes, use Value::array which uses the iterator’s size_hint().

§Examples
use paramdef::core::Value;

// Pre-allocate for 1000 elements
let mut values = Vec::with_capacity(1000);
for i in 0..1000 {
    values.push(Value::Int(i));
}
let value = Value::array_with_capacity(1000, values);
Source

pub fn object(pairs: impl IntoIterator<Item = (impl Into<Key>, Value)>) -> Self

Creates an object value from key-value pairs.

Uses the iterator’s size_hint() to pre-allocate the IndexMap, avoiding rehashing during construction. Field order is preserved.

§Examples
use paramdef::core::Value;

let value = Value::object([
    ("name", Value::text("Alice")),
    ("age", Value::Int(30)),
]);
Source

pub fn object_with_capacity( capacity: usize, pairs: impl IntoIterator<Item = (impl Into<Key>, Value)>, ) -> Self

Creates an object value with pre-allocated capacity.

Use this when you know the number of key-value pairs in advance to avoid rehashing. For dynamic sizes, use Value::object which uses the iterator’s size_hint(). Field order is preserved.

§Examples
use paramdef::core::Value;

// Pre-allocate for 100 entries
let mut pairs = Vec::with_capacity(100);
for i in 0..100 {
    pairs.push((format!("key_{i}"), Value::Int(i)));
}
let value = Value::object_with_capacity(100, pairs);
Source

pub fn binary(bytes: impl IntoIterator<Item = u8>) -> Self

Creates a binary value from bytes.

§Examples
use paramdef::core::Value;

let value = Value::binary([0x00, 0x01, 0x02]);
assert_eq!(value.as_binary().map(|b| b.len()), Some(3));

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Default for Value

Source§

fn default() -> Value

Returns the “default value” for a type. Read more
Source§

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

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<&str> for Value

Source§

fn from(v: &str) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Option<T>> for Value

Source§

fn from(v: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(v: String) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Value

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Value

Source§

fn from(json: Value) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Vec<T>> for Value

Source§

fn from(v: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Value

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Value

Source§

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

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 StructuralPartialEq for Value

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>,