NamedValue

Struct NamedValue 

Source
pub struct NamedValue { /* private fields */ }
Expand description

Named single value

Associates a human-readable name with a single Value, facilitating identification, retrieval, and display in configurations, parameter passing, and complex data structures.

§Features

  • Provides stable name identification for values
  • Automatically dereferences to the inner Value via Deref, allowing direct access to Value methods
  • Supports serde serialization and deserialization

§Use Cases

  • Configuration item encapsulation (e.g., "port", "timeout", etc.)
  • Named output of key values in logs/monitoring
  • Quick location by name in collections

§Example

use common_rs::util::value::{NamedValue, Value};

let named = NamedValue::new("flag", Value::Bool(true));
// Call Value methods through Deref
assert_eq!(named.as_bool().unwrap(), true);

Implementations§

Source§

impl NamedValue

Source

pub fn new(name: impl Into<String>, value: Value) -> Self

Create a new named value

Creates a binding instance between a name and a value.

§Parameters
  • name - Name of the value
  • value - Content of the value
§Returns

Returns a newly created NamedValue instance

§Example
use common_rs::util::value::{NamedValue, Value};

let named = NamedValue::new("timeout", Value::Int32(30));
assert_eq!(named.name(), "timeout");
Source

pub fn name(&self) -> &str

Get a reference to the name

Returns a read-only name slice bound to this value.

§Returns

Returns a string slice &str of the name

§Example
use common_rs::util::value::{NamedValue, Value};

let named = NamedValue::new("host", Value::String("localhost".to_string()));
assert_eq!(named.name(), "host");
Source

pub fn set_name(&mut self, name: impl Into<String>)

Set a new name

Updates the name bound to the current instance.

§Parameters
  • name - The new name
§Example
use common_rs::util::value::{NamedValue, Value};

let mut named = NamedValue::new("old_name", Value::Bool(true));
named.set_name("new_name");
assert_eq!(named.name(), "new_name");

Methods from Deref<Target = Value>§

Source

pub fn get<T>(&self) -> ValueResult<T>
where Self: ValueGetter<T>,

Generic getter method

Automatically selects the correct getter method based on the target type, performing strict type checking.

§Type Parameters
  • T - The target type to retrieve
§Returns

If types match, returns the value of the corresponding type; otherwise returns an error

§Example
use crate::util::value::Value;

let value = Value::Int32(42);

// Through type inference
let num: i32 = value.get().unwrap();
assert_eq!(num, 42);

// Explicitly specify type parameter
let num = value.get::<i32>().unwrap();
assert_eq!(num, 42);

// Different type
let text = Value::String("hello".to_string());
let s: String = text.get().unwrap();
assert_eq!(s, "hello");

// Boolean value
let flag = Value::Bool(true);
let b: bool = flag.get().unwrap();
assert_eq!(b, true);
Source

pub fn set<T>(&mut self, value: T) -> ValueResult<()>
where Self: ValueSetter<T>,

Generic setter method

Automatically selects the correct setter method based on the target type, performing strict type checking.

§Type Parameters
  • T - The target type to set
§Parameters
  • value - The value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

§Example
use crate::util::value::Value;

let mut value = Value::Empty(DataType::Int32);

// Through type inference
value.set(42i32).unwrap();
assert_eq!(value.get_int32().unwrap(), 42);

// Explicitly specify type parameter
value.set::<i32>(100).unwrap();
assert_eq!(value.get_int32().unwrap(), 100);

// String type
let mut text = Value::Empty(DataType::String);
text.set("hello".to_string()).unwrap();
assert_eq!(text.get_string().unwrap(), "hello");
Source

pub fn data_type(&self) -> DataType

Get the data type of the value

§Returns

Returns the data type corresponding to this value

§Example
use crate::util::value::{Value, DataType};

let value = Value::Int32(42);
assert_eq!(value.data_type(), DataType::Int32);

let empty = Value::Empty(DataType::String);
assert_eq!(empty.data_type(), DataType::String);
Source

pub fn is_empty(&self) -> bool

Check if the value is empty

§Returns

Returns true if the value is empty

§Example
use crate::util::value::{Value, DataType};

let value = Value::Int32(42);
assert!(!value.is_empty());

let empty = Value::Empty(DataType::String);
assert!(empty.is_empty());
Source

pub fn clear(&mut self)

Clear the value while preserving the type

Sets the current value to empty but retains its data type.

§Example
use crate::util::value::{Value, DataType};

let mut value = Value::Int32(42);
value.clear();
assert!(value.is_empty());
assert_eq!(value.data_type(), DataType::Int32);
Source

pub fn set_type(&mut self, data_type: DataType)

Set the data type

If the new type differs from the current type, clears the value and sets the new type.

§Parameters
  • data_type - The data type to set
§Example
use crate::util::value::{Value, DataType};

let mut value = Value::Int32(42);
value.set_type(DataType::String);
assert!(value.is_empty());
assert_eq!(value.data_type(), DataType::String);
Source

pub fn get_bool(&self) -> ValueResult<bool>

Get boolean value

§Returns

If types match, returns the boolean value; otherwise returns an error

§Example
use crate::util::value::Value;

let value = Value::Bool(true);
assert_eq!(value.get_bool().unwrap(), true);
Source

pub fn get_char(&self) -> ValueResult<char>

Get character value

§Returns

If types match, returns the character value; otherwise returns an error

§Example
use crate::util::value::Value;

let value = Value::Char('A');
assert_eq!(value.get_char().unwrap(), 'A');
Source

pub fn get_int8(&self) -> ValueResult<i8>

Get int8 value

§Returns

If types match, returns the int8 value; otherwise returns an error

Source

pub fn get_int16(&self) -> ValueResult<i16>

Get int16 value

§Returns

If types match, returns the int16 value; otherwise returns an error

Source

pub fn get_int32(&self) -> ValueResult<i32>

Get int32 value

§Returns

If types match, returns the int32 value; otherwise returns an error

Source

pub fn get_int64(&self) -> ValueResult<i64>

Get int64 value

§Returns

If types match, returns the int64 value; otherwise returns an error

Source

pub fn get_int128(&self) -> ValueResult<i128>

Get int128 value

§Returns

If types match, returns the int128 value; otherwise returns an error

Source

pub fn get_uint8(&self) -> ValueResult<u8>

Get uint8 value

§Returns

If types match, returns the uint8 value; otherwise returns an error

Source

pub fn get_uint16(&self) -> ValueResult<u16>

Get uint16 value

§Returns

If types match, returns the uint16 value; otherwise returns an error

Source

pub fn get_uint32(&self) -> ValueResult<u32>

Get uint32 value

§Returns

If types match, returns the uint32 value; otherwise returns an error

Source

pub fn get_uint64(&self) -> ValueResult<u64>

Get uint64 value

§Returns

If types match, returns the uint64 value; otherwise returns an error

Source

pub fn get_uint128(&self) -> ValueResult<u128>

Get uint128 value

§Returns

If types match, returns the uint128 value; otherwise returns an error

Source

pub fn get_float32(&self) -> ValueResult<f32>

Get float32 value

§Returns

If types match, returns the float32 value; otherwise returns an error

Source

pub fn get_float64(&self) -> ValueResult<f64>

Get float64 value

§Returns

If types match, returns the float64 value; otherwise returns an error

Source

pub fn get_string(&self) -> ValueResult<&str>

Get string reference

§Returns

If types match, returns a reference to the string; otherwise returns an error

§Example
use crate::util::value::Value;

let value = Value::String("hello".to_string());
assert_eq!(value.get_string().unwrap(), "hello");
Source

pub fn get_date(&self) -> ValueResult<NaiveDate>

Get date value

§Returns

If types match, returns the date value; otherwise returns an error

Source

pub fn get_time(&self) -> ValueResult<NaiveTime>

Get time value

§Returns

If types match, returns the time value; otherwise returns an error

Source

pub fn get_datetime(&self) -> ValueResult<NaiveDateTime>

Get datetime value

§Returns

If types match, returns the datetime value; otherwise returns an error

Source

pub fn get_instant(&self) -> ValueResult<DateTime<Utc>>

Get UTC instant value

§Returns

If types match, returns the UTC instant value; otherwise returns an error

Source

pub fn get_biginteger(&self) -> ValueResult<BigInt>

Get big integer value

§Returns

If types match, returns the big integer value; otherwise returns an error

§Example
use crate::util::value::Value;
use num_bigint::BigInt;

let value = Value::BigInteger(BigInt::from(123456789));
assert_eq!(value.get_biginteger().unwrap(), BigInt::from(123456789));
Source

pub fn get_bigdecimal(&self) -> ValueResult<BigDecimal>

Get big decimal value

§Returns

If types match, returns the big decimal value; otherwise returns an error

§Example
use crate::util::value::Value;
use bigdecimal::BigDecimal;

let value = Value::BigDecimal(BigDecimal::from(123.456));
assert_eq!(value.get_bigdecimal().unwrap(), BigDecimal::from(123.456));
Source

pub fn as_bool(&self) -> ValueResult<bool>

Convert to boolean value

§Returns

Attempts to convert the value to a boolean, supporting conversion from various types

§Example
use crate::util::value::Value;

let value = Value::Int32(1);
assert_eq!(value.as_bool().unwrap(), true);

let value = Value::String("true".to_string());
assert_eq!(value.as_bool().unwrap(), true);
Source

pub fn as_int32(&self) -> ValueResult<i32>

Convert to int32 value

§Returns

Attempts to convert the value to i32, supporting conversion from various types

§Example
use crate::util::value::Value;

let value = Value::Int8(42);
assert_eq!(value.as_int32().unwrap(), 42);

let value = Value::String("123".to_string());
assert_eq!(value.as_int32().unwrap(), 123);
Source

pub fn as_int64(&self) -> ValueResult<i64>

Convert to int64 value

§Returns

Attempts to convert the value to i64, supporting conversion from various types

§Example
use crate::util::value::Value;

let value = Value::Int32(42);
assert_eq!(value.as_int64().unwrap(), 42i64);

let value = Value::Float64(123.456);
assert_eq!(value.as_int64().unwrap(), 123i64);
Source

pub fn as_float64(&self) -> ValueResult<f64>

Convert to float64 value

§Returns

Attempts to convert the value to f64, supporting conversion from various types

§Example
use crate::util::value::Value;

let value = Value::Int32(42);
assert_eq!(value.as_float64().unwrap(), 42.0);

let value = Value::Bool(true);
assert_eq!(value.as_float64().unwrap(), 1.0);
Source

pub fn as_string(&self) -> ValueResult<String>

Convert to string

§Returns

Converts the value to its string representation

§Example
use crate::util::value::Value;

let value = Value::Int32(42);
assert_eq!(value.as_string().unwrap(), "42");

let value = Value::Bool(true);
assert_eq!(value.as_string().unwrap(), "true");
Source

pub fn set_bool(&mut self, value: bool) -> ValueResult<()>

Set boolean value

§Parameters
  • value - The boolean value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

§Example
use crate::util::value::Value;

let mut value = Value::Empty(DataType::Bool);
value.set_bool(true).unwrap();
assert_eq!(value.get_bool().unwrap(), true);
Source

pub fn set_char(&mut self, value: char) -> ValueResult<()>

Set character value

§Parameters
  • value - The character value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_int8(&mut self, value: i8) -> ValueResult<()>

Set int8 value

§Parameters
  • value - The int8 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_int16(&mut self, value: i16) -> ValueResult<()>

Set int16 value

§Parameters
  • value - The int16 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_int32(&mut self, value: i32) -> ValueResult<()>

Set int32 value

§Parameters
  • value - The int32 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_int64(&mut self, value: i64) -> ValueResult<()>

Set int64 value

§Parameters
  • value - The int64 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_int128(&mut self, value: i128) -> ValueResult<()>

Set int128 value

§Parameters
  • value - The int128 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_uint8(&mut self, value: u8) -> ValueResult<()>

Set uint8 value

§Parameters
  • value - The uint8 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_uint16(&mut self, value: u16) -> ValueResult<()>

Set uint16 value

§Parameters
  • value - The uint16 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_uint32(&mut self, value: u32) -> ValueResult<()>

Set uint32 value

§Parameters
  • value - The uint32 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_uint64(&mut self, value: u64) -> ValueResult<()>

Set uint64 value

§Parameters
  • value - The uint64 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_uint128(&mut self, value: u128) -> ValueResult<()>

Set uint128 value

§Parameters
  • value - The uint128 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_float32(&mut self, value: f32) -> ValueResult<()>

Set float32 value

§Parameters
  • value - The float32 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_float64(&mut self, value: f64) -> ValueResult<()>

Set float64 value

§Parameters
  • value - The float64 value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_string(&mut self, value: String) -> ValueResult<()>

Set string value

§Parameters
  • value - The string value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

§Example
use crate::util::value::Value;

let mut value = Value::Empty(DataType::String);
value.set_string("hello".to_string()).unwrap();
assert_eq!(value.get_string().unwrap(), "hello");
Source

pub fn set_date(&mut self, value: NaiveDate) -> ValueResult<()>

Set date value

§Parameters
  • value - The date value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_time(&mut self, value: NaiveTime) -> ValueResult<()>

Set time value

§Parameters
  • value - The time value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_datetime(&mut self, value: NaiveDateTime) -> ValueResult<()>

Set datetime value

§Parameters
  • value - The datetime value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_instant(&mut self, value: DateTime<Utc>) -> ValueResult<()>

Set UTC instant value

§Parameters
  • value - The UTC instant value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

Source

pub fn set_biginteger(&mut self, value: BigInt) -> ValueResult<()>

Set big integer value

§Parameters
  • value - The big integer value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

§Example
use crate::util::value::Value;
use num_bigint::BigInt;

let mut value = Value::Empty(DataType::BigInteger);
value.set_biginteger(BigInt::from(123456789)).unwrap();
assert_eq!(value.get_biginteger().unwrap(), BigInt::from(123456789));
Source

pub fn set_bigdecimal(&mut self, value: BigDecimal) -> ValueResult<()>

Set big decimal value

§Parameters
  • value - The big decimal value to set
§Returns

If setting succeeds, returns Ok(()); otherwise returns an error

§Example
use crate::util::value::Value;
use bigdecimal::BigDecimal;

let mut value = Value::Empty(DataType::BigDecimal);
value.set_bigdecimal(BigDecimal::from(123.456)).unwrap();
assert_eq!(value.get_bigdecimal().unwrap(), BigDecimal::from(123.456));

Trait Implementations§

Source§

impl Clone for NamedValue

Source§

fn clone(&self) -> NamedValue

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 NamedValue

Source§

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

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

impl Deref for NamedValue

Source§

fn deref(&self) -> &Self::Target

Dereference to the inner Value

Allows direct invocation of methods on Value, for example: named.as_i32().

§Returns

Returns an immutable reference &Value to the inner value.

§Example
use common_rs::util::value::{NamedValue, Value};

let named = NamedValue::new("flag", Value::Bool(true));
// Call Value methods through Deref
assert_eq!(named.as_bool().unwrap(), true);
Source§

type Target = Value

The resulting type after dereferencing.
Source§

impl DerefMut for NamedValue

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutable dereference to the inner Value

Allows in-place modification of the inner value (provided Value itself offers corresponding mutable methods).

§Returns

Returns a mutable reference &mut Value to the inner value.

Source§

impl<'de> Deserialize<'de> for NamedValue

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 From<NamedValue> for NamedMultiValues

Source§

fn from(named: NamedValue) -> Self

Construct NamedMultiValues from NamedValue

Reuses the name and promotes the single value to a MultiValues containing only one element.

Source§

impl PartialEq for NamedValue

Source§

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

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 NamedValue

Auto Trait Implementations§

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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, 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>,