#[non_exhaustive]pub enum Value {
Show 20 variants
Null,
Bool(bool),
Int(i64),
Uint(u64),
Int128(i128),
Uint128(u128),
Float(f64),
String(String),
DateTime(String),
Date(String),
Time(String),
Ipv4(String),
Ipv6(String),
Inet(String),
Cidr(String),
MacAddr(String),
MacAddr8(String),
Uuid(String),
Array(Vec<Value>),
Object(BTreeMap<String, Value>),
}Expand description
Dynamic config value.
Non-exhaustive: new variants may come without a breaking release, so
a match needs a _ arm.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Null
Bool(bool)
Int(i64)
Uint(u64)
Int128(i128)
128-bit signed — only from the table i128 type (never auto,
which stops at i64/u64/f64). Serializes as a plain number
when it fits i64 or u64, otherwise as its decimal string
(JSON has no wider integer); format_id still reports
i128.
Uint128(u128)
128-bit unsigned — only from the table u128 type. Serializes as
a plain number when it fits u64, otherwise as its decimal
string; format_id still reports u128.
Float(f64)
String(String)
DateTime(String)
An RFC 3339-style date or datetime, kept as its text. Produced by
toml datetime literals, the table dt type and the auto guess —
all gated by the datetime feature. Serializes as the string itself (so
a serialize→deserialize round trip through a format without a
datetime type yields a String).
Date(String)
YYYY-MM-DD, kept as its text (table date type, date feature).
Time(String)
hh:mm:ss[.fraction], kept as its text (table time type, time
feature).
Ipv4(String)
An IPv4 address, kept as its validated canonical text (table
ipv4 type, ipv4 feature) — text like every typed value, so
as_str works; as_ip gives the
address. The inet type (inet feature) also produces this or
Value::Ipv6, whichever family parses.
Ipv6(String)
An IPv6 address, kept as its validated canonical text
(2001:0DB8::1 → 2001:db8::1; table ipv6 type, ipv6 feature).
Inet(String)
A host address with a netmask (10.0.0.1/24), kept as its
validated text — PostgreSQL inet semantics, host bits below the
mask may be set (table inet type, inet feature; a bare inet
address maps onto Value::Ipv4/Value::Ipv6 instead).
Cidr(String)
A network — optional netmask defaulting to the full address
length, host bits below the mask must be zero — kept as its
validated text; PostgreSQL cidr semantics (table cidr type,
cidr feature).
MacAddr(String)
6-byte MAC address, kept as its validated text (table macaddr
type, macaddr feature).
MacAddr8(String)
8-byte MAC address, kept as its validated text (table macaddr8
type, macaddr8 feature).
Uuid(String)
Hyphenated 8-4-4-4-12 UUID, kept as its validated text (table
uuid type, uuid feature).
Array(Vec<Value>)
Object(BTreeMap<String, Value>)
Implementations§
Source§impl Value
impl Value
Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Object member by key; None when this is not an object or the key
is absent.
Sourcepub fn get_index(&self, index: usize) -> Option<&Value>
pub fn get_index(&self, index: usize) -> Option<&Value>
Array element by index; None when this is not an array or the
index is out of bounds.
pub fn is_null(&self) -> bool
pub fn as_bool(&self) -> Option<bool>
pub fn as_i64(&self) -> Option<i64>
pub fn as_u64(&self) -> Option<u64>
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
The text of a string — or of any typed value (datetime, IP, MAC, uuid, …), which all keep their text form.
Sourcepub fn into_typed<T: DeserializeOwned>(self) -> Result<T>
pub fn into_typed<T: DeserializeOwned>(self) -> Result<T>
Convert this value into any serde type, with the same rules as
Loader::load: strings coerce into numbers
and bools (and back), and an error names the key path. It consumes
the value; take one section of a loaded config with .clone():
#[derive(serde::Deserialize)]
struct Db {
port: u16,
}
let config = c4::Value::from(std::collections::BTreeMap::from([(
"db".to_owned(),
c4::Value::from(std::collections::BTreeMap::from([(
"port".to_owned(),
c4::Value::from("5432"),
)])),
)]));
let db: Db = config["db"].clone().into_typed()?;
assert_eq!(db.port, 5432);Sourcepub fn format_id(&self) -> Cow<'static, str>
pub fn format_id(&self) -> Cow<'static, str>
The format id of this value — which parser produced it: null,
bool, i64, u64, i128, u128, f64, str, the typed ids (dt, date,
time, ipv4, ipv6, cidr, macaddr, macaddr8, uuid),
arr:<t> for homogeneous scalar arrays, arr for other arrays,
object for objects. Integer widths normalize to the stored type
(an i8 cell stores an i64, so it reports i64), and inet
reports as the family it parsed into. Shown by trace() as the
format field of every serialized leaf.
pub fn as_array(&self) -> Option<&Vec<Value>>
pub fn as_object(&self) -> Option<&BTreeMap<String, Value>>
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 Display for Value
Compact JSON: typed values print as their text, non-finite floats as
null (like any JSON serializer). Use Value::as_str for a string’s
raw text.
impl Display for Value
Compact JSON: typed values print as their text, non-finite floats as
null (like any JSON serializer). Use Value::as_str for a string’s
raw text.
Source§impl From<TracedValue> for Value
Drop the provenance: the plain merged value, as load() returns it.
impl From<TracedValue> for Value
Drop the provenance: the plain merged value, as load() returns it.