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
ValueviaDeref, allowing direct access toValuemethods - Supports
serdeserialization 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.to::<bool>().unwrap(), true);Implementations§
Source§impl NamedValue
impl NamedValue
Sourcepub fn new(name: impl Into<String>, value: Value) -> Self
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 valuevalue- 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");Methods from Deref<Target = Value>§
Sourcepub fn get<T>(&self) -> ValueResult<T>where
Self: ValueGetter<T>,
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.
get<T>() performs strict type matching. It does not do cross-type conversion.
For example, Value::Int32(42).get::<i64>() fails, while
Value::Int32(42).to::<i64>() succeeds.
§Supported Generic Types
Value::get<T>() currently supports the following T:
boolchari8,i16,i32,i64,i128u8,u16,u32,u64,u128f32,f64StringNaiveDate,NaiveTime,NaiveDateTime,DateTime<Utc>BigInt,BigDecimalisize,usizeDurationUrlHashMap<String, String>serde_json::Value
§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);Sourcepub fn to<T>(&self) -> ValueResult<T>where
Self: ValueConverter<T>,
pub fn to<T>(&self) -> ValueResult<T>where
Self: ValueConverter<T>,
Generic conversion method
Converts the current value to the target type according to the conversion
rules defined by [ValueConverter<T>].
§Supported Target Types And Source Variants
Value::to<T>() currently supports the following target types:
boolValue::BoolValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::String, parsed asbool
charValue::Char
i8Value::Int8
i16Value::Int16
i32Value::Int32Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int64,Value::Int128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::Float32,Value::Float64Value::String, parsed asi32Value::BigInteger,Value::BigDecimal
i64Value::Int64Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::Float32,Value::Float64Value::String, parsed asi64Value::BigInteger,Value::BigDecimal
i128Value::Int128
u8Value::UInt8Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::String, parsed asu8
u16Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::String, parsed asu16
u32Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::String, parsed asu32
u64Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::String, parsed asu64
u128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::String, parsed asu128
f32Value::Float32,Value::Float64Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::String, parsed asf32Value::BigInteger,Value::BigDecimal
f64Value::Float64Value::BoolValue::CharValue::Int8,Value::Int16,Value::Int32,Value::Int64,Value::Int128Value::UInt8,Value::UInt16,Value::UInt32,Value::UInt64,Value::UInt128Value::Float32Value::String, parsed asf64Value::BigInteger,Value::BigDecimal
StringValue::StringValue::Bool,Value::Char- all integer and floating-point variants
Value::Date,Value::Time,Value::DateTime,Value::InstantValue::BigInteger,Value::BigDecimalValue::IntSize,Value::UIntSizeValue::Duration, formatted as<nanoseconds>nsValue::UrlValue::StringMap, serialized as JSON textValue::Json, serialized as JSON text
NaiveDateValue::Date
NaiveTimeValue::Time
NaiveDateTimeValue::DateTime
DateTime<Utc>Value::Instant
BigIntValue::BigInteger
BigDecimalValue::BigDecimal
isizeValue::IntSize
usizeValue::UIntSize
DurationValue::DurationValue::String, parsed from<nanoseconds>ns
UrlValue::UrlValue::String, parsed as URL text
HashMap<String, String>Value::StringMap
serde_json::ValueValue::JsonValue::String, parsed as JSON textValue::StringMap, converted to a JSON object
Any target type not listed above is not supported by Value::to<T>().
§Type Parameters
T- The target type to convert to
§Returns
Returns the converted value on success, or an error if conversion is not supported or fails.
§Example
use crate::util::value::Value;
let value = Value::Int32(42);
let num: i64 = value.to().unwrap();
assert_eq!(num, 42);
let text: String = value.to().unwrap();
assert_eq!(text, "42");Sourcepub fn set<T>(&mut self, value: T) -> ValueResult<()>where
Self: ValueSetter<T>,
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.
§Supported Generic Types
Value::set<T>(value) currently supports the following T:
boolchari8,i16,i32,i64,i128u8,u16,u32,u64,u128f32,f64String,&strNaiveDate,NaiveTime,NaiveDateTime,DateTime<Utc>BigInt,BigDecimalisize,usizeDurationUrlHashMap<String, String>serde_json::Value
§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");Sourcepub fn data_type(&self) -> DataType
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);Sourcepub fn set_type(&mut self, data_type: DataType)
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);Sourcepub fn get_bool(&self) -> ValueResult<bool>
pub fn get_bool(&self) -> ValueResult<bool>
Sourcepub fn get_char(&self) -> ValueResult<char>
pub fn get_char(&self) -> ValueResult<char>
Sourcepub fn get_int8(&self) -> ValueResult<i8>
pub fn get_int8(&self) -> ValueResult<i8>
Sourcepub fn get_int16(&self) -> ValueResult<i16>
pub fn get_int16(&self) -> ValueResult<i16>
Sourcepub fn get_int32(&self) -> ValueResult<i32>
pub fn get_int32(&self) -> ValueResult<i32>
Sourcepub fn get_int64(&self) -> ValueResult<i64>
pub fn get_int64(&self) -> ValueResult<i64>
Sourcepub fn get_int128(&self) -> ValueResult<i128>
pub fn get_int128(&self) -> ValueResult<i128>
Sourcepub fn get_uint8(&self) -> ValueResult<u8>
pub fn get_uint8(&self) -> ValueResult<u8>
Sourcepub fn get_uint16(&self) -> ValueResult<u16>
pub fn get_uint16(&self) -> ValueResult<u16>
Sourcepub fn get_uint32(&self) -> ValueResult<u32>
pub fn get_uint32(&self) -> ValueResult<u32>
Sourcepub fn get_uint64(&self) -> ValueResult<u64>
pub fn get_uint64(&self) -> ValueResult<u64>
Sourcepub fn get_uint128(&self) -> ValueResult<u128>
pub fn get_uint128(&self) -> ValueResult<u128>
Sourcepub fn get_float32(&self) -> ValueResult<f32>
pub fn get_float32(&self) -> ValueResult<f32>
Sourcepub fn get_float64(&self) -> ValueResult<f64>
pub fn get_float64(&self) -> ValueResult<f64>
Sourcepub fn get_string(&self) -> ValueResult<&str>
pub fn get_string(&self) -> ValueResult<&str>
Sourcepub fn get_date(&self) -> ValueResult<NaiveDate>
pub fn get_date(&self) -> ValueResult<NaiveDate>
Sourcepub fn get_time(&self) -> ValueResult<NaiveTime>
pub fn get_time(&self) -> ValueResult<NaiveTime>
Sourcepub fn get_datetime(&self) -> ValueResult<NaiveDateTime>
pub fn get_datetime(&self) -> ValueResult<NaiveDateTime>
Sourcepub fn get_instant(&self) -> ValueResult<DateTime<Utc>>
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
Sourcepub fn get_biginteger(&self) -> ValueResult<BigInt>
pub fn get_biginteger(&self) -> ValueResult<BigInt>
Sourcepub fn get_bigdecimal(&self) -> ValueResult<BigDecimal>
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));Sourcepub fn set_bool(&mut self, value: bool) -> ValueResult<()>
pub fn set_bool(&mut self, value: bool) -> ValueResult<()>
Sourcepub fn set_char(&mut self, value: char) -> ValueResult<()>
pub fn set_char(&mut self, value: char) -> ValueResult<()>
Sourcepub fn set_int8(&mut self, value: i8) -> ValueResult<()>
pub fn set_int8(&mut self, value: i8) -> ValueResult<()>
Sourcepub fn set_int16(&mut self, value: i16) -> ValueResult<()>
pub fn set_int16(&mut self, value: i16) -> ValueResult<()>
Sourcepub fn set_int32(&mut self, value: i32) -> ValueResult<()>
pub fn set_int32(&mut self, value: i32) -> ValueResult<()>
Sourcepub fn set_int64(&mut self, value: i64) -> ValueResult<()>
pub fn set_int64(&mut self, value: i64) -> ValueResult<()>
Sourcepub fn set_int128(&mut self, value: i128) -> ValueResult<()>
pub fn set_int128(&mut self, value: i128) -> ValueResult<()>
Sourcepub fn set_uint8(&mut self, value: u8) -> ValueResult<()>
pub fn set_uint8(&mut self, value: u8) -> ValueResult<()>
Sourcepub fn set_uint16(&mut self, value: u16) -> ValueResult<()>
pub fn set_uint16(&mut self, value: u16) -> ValueResult<()>
Sourcepub fn set_uint32(&mut self, value: u32) -> ValueResult<()>
pub fn set_uint32(&mut self, value: u32) -> ValueResult<()>
Sourcepub fn set_uint64(&mut self, value: u64) -> ValueResult<()>
pub fn set_uint64(&mut self, value: u64) -> ValueResult<()>
Sourcepub fn set_uint128(&mut self, value: u128) -> ValueResult<()>
pub fn set_uint128(&mut self, value: u128) -> ValueResult<()>
Sourcepub fn set_float32(&mut self, value: f32) -> ValueResult<()>
pub fn set_float32(&mut self, value: f32) -> ValueResult<()>
Sourcepub fn set_float64(&mut self, value: f64) -> ValueResult<()>
pub fn set_float64(&mut self, value: f64) -> ValueResult<()>
Sourcepub fn set_string(&mut self, value: String) -> ValueResult<()>
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");Sourcepub fn set_date(&mut self, value: NaiveDate) -> ValueResult<()>
pub fn set_date(&mut self, value: NaiveDate) -> ValueResult<()>
Sourcepub fn set_time(&mut self, value: NaiveTime) -> ValueResult<()>
pub fn set_time(&mut self, value: NaiveTime) -> ValueResult<()>
Sourcepub fn set_datetime(&mut self, value: NaiveDateTime) -> ValueResult<()>
pub fn set_datetime(&mut self, value: NaiveDateTime) -> ValueResult<()>
Sourcepub fn set_instant(&mut self, value: DateTime<Utc>) -> ValueResult<()>
pub fn set_instant(&mut self, value: DateTime<Utc>) -> ValueResult<()>
Sourcepub fn set_biginteger(&mut self, value: BigInt) -> ValueResult<()>
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));Sourcepub fn set_bigdecimal(&mut self, value: BigDecimal) -> ValueResult<()>
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));Sourcepub fn get_intsize(&self) -> ValueResult<isize>
pub fn get_intsize(&self) -> ValueResult<isize>
Sourcepub fn get_uintsize(&self) -> ValueResult<usize>
pub fn get_uintsize(&self) -> ValueResult<usize>
Sourcepub fn get_duration(&self) -> ValueResult<Duration>
pub fn get_duration(&self) -> ValueResult<Duration>
Sourcepub fn get_url(&self) -> ValueResult<Url>
pub fn get_url(&self) -> ValueResult<Url>
Get Url reference
§Returns
If types match, returns a reference to the Url; otherwise returns an error
Sourcepub fn get_string_map(&self) -> ValueResult<HashMap<String, String>>
pub fn get_string_map(&self) -> ValueResult<HashMap<String, String>>
Get StringMap reference
§Returns
If types match, returns a reference to the
HashMap<String, String>; otherwise returns an error
Sourcepub fn get_json(&self) -> ValueResult<Value>
pub fn get_json(&self) -> ValueResult<Value>
Get Json value reference
§Returns
If types match, returns a reference to the serde_json::Value;
otherwise returns an error
Sourcepub fn set_intsize(&mut self, value: isize) -> ValueResult<()>
pub fn set_intsize(&mut self, value: isize) -> ValueResult<()>
Set isize value
Sourcepub fn set_uintsize(&mut self, value: usize) -> ValueResult<()>
pub fn set_uintsize(&mut self, value: usize) -> ValueResult<()>
Set usize value
Sourcepub fn set_duration(&mut self, value: Duration) -> ValueResult<()>
pub fn set_duration(&mut self, value: Duration) -> ValueResult<()>
Set Duration value
Sourcepub fn set_url(&mut self, value: Url) -> ValueResult<()>
pub fn set_url(&mut self, value: Url) -> ValueResult<()>
Set Url value
Sourcepub fn set_string_map(
&mut self,
value: HashMap<String, String>,
) -> ValueResult<()>
pub fn set_string_map( &mut self, value: HashMap<String, String>, ) -> ValueResult<()>
Set StringMap value
Sourcepub fn set_json(&mut self, value: Value) -> ValueResult<()>
pub fn set_json(&mut self, value: Value) -> ValueResult<()>
Set Json value
Sourcepub fn deserialize_json<T: DeserializeOwned>(&self) -> ValueResult<T>
pub fn deserialize_json<T: DeserializeOwned>(&self) -> ValueResult<T>
Trait Implementations§
Source§impl Clone for NamedValue
impl Clone for NamedValue
Source§fn clone(&self) -> NamedValue
fn clone(&self) -> NamedValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NamedValue
impl Debug for NamedValue
Source§impl Deref for NamedValue
impl Deref for NamedValue
Source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Dereference to the inner Value
Allows direct invocation of methods on Value, for example: named.to::<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.to::<bool>().unwrap(), true);Source§impl DerefMut for NamedValue
impl DerefMut for NamedValue
Source§impl<'de> Deserialize<'de> for NamedValue
impl<'de> Deserialize<'de> for NamedValue
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<NamedValue> for NamedMultiValues
impl From<NamedValue> for NamedMultiValues
Source§fn from(named: NamedValue) -> Self
fn from(named: NamedValue) -> Self
Construct NamedMultiValues from NamedValue
Reuses the name and promotes the single value to a MultiValues containing only one element.