pub enum Value {
Object(HashMap<String, Value>),
Array(Vec<Value>),
Boolean(bool),
Null,
String(String),
Number(Number),
}
Variants§
Implementations§
Source§impl Value
impl Value
pub fn object(obj: HashMap<String, Value>) -> Value
pub fn object_from_iter<I>(iter: I) -> Value
pub fn array(values: Vec<Value>) -> Value
pub fn array_from_iter<I>(iter: I) -> Valuewhere
I: IntoIterator<Item = Value>,
pub fn boolean(boolean: bool) -> Value
pub fn null() -> Value
pub fn new_string(string: impl Into<String>) -> Value
Source§impl Value
impl Value
pub fn as_object(&self) -> Option<&HashMap<String, Value>>
pub fn as_array(&self) -> Option<&Vec<Value>>
Sourcepub fn as_array_numerically(&self) -> Option<Vec<&Value>>
pub fn as_array_numerically(&self) -> Option<Vec<&Value>>
Attempts to interpret the current Value
as an array by applying
HOCON’s “numerically-indexed object to array” conversion rule.
§Behavior
-
If the value is already an array (
Value::Array
), this simply returns a reference to its elements as aVec<&Value>
. -
If the value is an object (
Value::Object
) whose keys are strings representing integers (e.g."0"
,"1"
,"2"
), it is converted into an array:- Keys are filtered to include only those that can be parsed as
usize
. - The key–value pairs are sorted by their numeric key.
- The values are collected into a
Vec<&Value>
in ascending key order.
- Keys are filtered to include only those that can be parsed as
-
For any other kind of value, the function returns
None
.
§Example
{
"0": "first",
"2": "third",
"1": "second"
}
Will be interpreted as:
[ "first", "second", "third" ]
Sourcepub fn as_boolean(&self) -> Option<bool>
pub fn as_boolean(&self) -> Option<bool>
Attempts to interpret the current Value
as a boolean, following
HOCON’s relaxed truthy/falsey rules.
§Behavior
-
If the value is a
Value::Boolean
, returns the innerbool
. -
If the value is a
Value::String
, accepts several textual representations:"true"
,"on"
,"yes"
→Some(true)
"false"
,"off"
,"no"
→Some(false)
-
For all other values (numbers, arrays, objects, or strings that don’t match the above), returns
None
.
§Notes
- The matching is case-sensitive (
"True"
will not be recognized). - This conversion is specific to HOCON and goes beyond JSON’s strict boolean representation.
pub fn as_str(&self) -> Option<&str>
pub fn as_f64(&mut self) -> Option<f64>
pub fn as_i64(&self) -> Option<i64>
pub fn as_i128(&self) -> Option<i128>
pub fn as_u128(&self) -> Option<u128>
pub fn as_u64(&self) -> Option<u64>
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Checks whether the current Value
represents null
in HOCON.
§Behavior
- Returns
true
if the value is explicitlyValue::Null
. - Returns
true
if the value is aValue::String
equal to"null"
. - Otherwise, returns
false
.
§Notes
- The check for
"null"
is case-sensitive."Null"
or"NULL"
will not be considered null. - This deviates from strict JSON, where only a literal
null
is valid. HOCON allows the string"null"
to be treated as a null value.
pub fn ty(&self) -> &'static str
pub fn into_object(self) -> Option<HashMap<String, Value>>
pub fn into_array(self) -> Option<Vec<Value>>
pub fn into_boolean(self) -> Option<bool>
pub fn into_string(self) -> Option<String>
pub fn into_number(self) -> Option<Number>
Sourcepub fn get_by_path<'a>(&self, paths: impl AsRef<[&'a str]>) -> Option<&Value>
pub fn get_by_path<'a>(&self, paths: impl AsRef<[&'a str]>) -> Option<&Value>
Retrieves a value from a nested Value::Object
by following a HOCON-style path.
§Arguments
paths
- A sequence of keys representing the path to the desired value. The path should already be split by.
(dot).
§Returns
Some(&Value)
if the full path exists in the object tree.None
if any key in the path does not exist or if a non-object value is encountered before reaching the end of the path.
§Example
// Assuming the following HOCON-like structure:
// {
// database: {
// connection: {
// timeout: 30
// }
// }
// }
let val = root.get_by_path(&["database", "connection", "timeout"]);
assert_eq!(val, Some(&hocon_rs::Value::Number(30.into())));
pub fn get_by_path_mut<'a>( &mut self, paths: impl AsRef<[&'a str]>, ) -> Option<&mut Value>
Sourcepub fn with_fallback(self, fallback: Value) -> Value
pub fn with_fallback(self, fallback: Value) -> Value
Merge this Value
with a fallback Value
, following HOCON’s withFallback
semantics.
- If both
self
andfallback
areObject
s, they are merged key by key:- If a key exists in both objects:
- If both values are objects, merge them recursively.
- Otherwise, keep the value from
self
(ignore the fallback).
- If a key exists only in the fallback, insert it into
self
.
- If a key exists in both objects:
- For all other cases (non-object values),
self
takes precedence and the fallback is ignored.
Source§impl Value
impl Value
pub fn as_bytes(&self) -> Option<BigUint>
pub fn as_duration(&self) -> Option<Duration>
pub fn as_nanos(&self) -> Option<u128>
pub fn as_millis(&self) -> Option<u128>
pub fn as_secs(&self) -> Option<u64>
pub fn as_secs_f32(&self) -> Option<f32>
pub fn as_secs_f64(&self) -> Option<f64>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
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<'de> Deserializer<'de> for Value
impl<'de> Deserializer<'de> for Value
Source§type Error = Error
type Error = Error
Source§fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserializer
to figure out how to drive the visitor based
on what data type is in the input. Read moreSource§fn deserialize_bool<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_bool<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a bool
value.Source§fn deserialize_i8<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_i8<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an i8
value.Source§fn deserialize_i16<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_i16<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an i16
value.Source§fn deserialize_i32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_i32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an i32
value.Source§fn deserialize_i64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_i64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an i64
value.Source§fn deserialize_i128<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_i128<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Source§fn deserialize_u8<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_u8<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a u8
value.Source§fn deserialize_u16<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_u16<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a u16
value.Source§fn deserialize_u32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_u32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a u32
value.Source§fn deserialize_u64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_u64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a u64
value.Source§fn deserialize_u128<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_u128<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Source§fn deserialize_f32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_f32<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a f32
value.Source§fn deserialize_f64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_f64<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a f64
value.Source§fn deserialize_char<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_char<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a char
value.Source§fn deserialize_str<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_str<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_string<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_string<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_bytes<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_bytes<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a byte array and does not
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_byte_buf<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_byte_buf<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_option<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_option<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an optional value. Read moreSource§fn deserialize_unit<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_unit<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a unit value.Source§fn deserialize_unit_struct<W>(
self,
name: &'static str,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_unit_struct<W>(
self,
name: &'static str,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a unit struct with a
particular name.Source§fn deserialize_newtype_struct<W>(
self,
name: &'static str,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_newtype_struct<W>(
self,
name: &'static str,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a newtype struct with a
particular name.Source§fn deserialize_seq<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_seq<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a sequence of values.Source§fn deserialize_tuple<W>(
self,
len: usize,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_tuple<W>(
self,
len: usize,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data.Source§fn deserialize_tuple_struct<W>(
self,
name: &'static str,
len: usize,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_tuple_struct<W>(
self,
name: &'static str,
len: usize,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a tuple struct with a
particular name and number of fields.Source§fn deserialize_map<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_map<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a map of key-value pairs.Source§fn deserialize_struct<W>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_struct<W>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting a struct with a particular
name and fields.Source§fn deserialize_enum<W>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_enum<W>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting an enum value with a
particular name and possible variants.Source§fn deserialize_identifier<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_identifier<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant.Source§fn deserialize_ignored_any<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
fn deserialize_ignored_any<W>(
self,
visitor: W,
) -> Result<W::Value, <Self as Deserializer<'de>>::Error>where
W: Visitor<'de>,
Deserialize
type needs to deserialize a value whose type
doesn’t matter because it is ignored. Read moreSource§fn is_human_readable(&self) -> bool
fn is_human_readable(&self) -> bool
Deserialize
implementations should expect to
deserialize their human-readable form. Read more