pub struct Item<'de> { /* private fields */ }Expand description
A parsed TOML value with span information.
Use the as_* methods (as_str,
as_i64, as_table, etc.) to
extract the value, or call value /
value_mut to pattern match via the Value /
ValueMut enums.
Items support indexing with &str (table lookup) and usize (array
access). These operators return MaybeItem and never panic — missing
keys or out-of-bounds indices produce a None variant instead.
§Lookup performance
String-key lookups (item["key"], as_table +
Table::get) perform a linear scan over the table entries — O(n) in
the number of keys. For small tables or a handful of lookups, as is
typical in TOML, this is well fast enough.
For structured deserialization of larger tables, use
TableHelper with the Context
returned by parse.
§Examples
let arena = toml_spanner::Arena::new();
let table = toml_spanner::parse("x = 42", &arena)?;
assert_eq!(table["x"].as_i64(), Some(42));
assert_eq!(table["missing"].as_i64(), None);Implementations§
Source§impl<'de> Item<'de>
impl<'de> Item<'de>
Sourcepub fn expect_custom_string(
&self,
ctx: &mut Context<'de>,
expected: &'static str,
) -> Result<&'de str, Failed>
pub fn expect_custom_string( &self, ctx: &mut Context<'de>, expected: &'static str, ) -> Result<&'de str, Failed>
Returns a string, or records an error with a custom expected message.
Use this instead of expect_string when the
expected value is more specific than just “a string” — for example,
"an IPv4 address" or "a hex color".
Sourcepub fn expect_string(&self, ctx: &mut Context<'de>) -> Result<&'de str, Failed>
pub fn expect_string(&self, ctx: &mut Context<'de>) -> Result<&'de str, Failed>
Returns a string, or records an error if this is not a string.
Sourcepub fn expect_array(
&self,
ctx: &mut Context<'de>,
) -> Result<&Array<'de>, Failed>
pub fn expect_array( &self, ctx: &mut Context<'de>, ) -> Result<&Array<'de>, Failed>
Returns an array reference, or records an error if this is not an array.
Sourcepub fn expect_table(
&self,
ctx: &mut Context<'de>,
) -> Result<&Table<'de>, Failed>
pub fn expect_table( &self, ctx: &mut Context<'de>, ) -> Result<&Table<'de>, Failed>
Returns a table reference, or records an error if this is not a table.
Sourcepub fn table_helper<'ctx, 'item>(
&'item self,
ctx: &'ctx mut Context<'de>,
) -> Result<TableHelper<'ctx, 'item, 'de>, Failed>
pub fn table_helper<'ctx, 'item>( &'item self, ctx: &'ctx mut Context<'de>, ) -> Result<TableHelper<'ctx, 'item, 'de>, Failed>
Creates a TableHelper for this item, returning an error if it is not a table.
This is the typical entry point for implementing Deserialize.
Source§impl<'de> Item<'de>
impl<'de> Item<'de>
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns an f64 if this is a float or integer value.
Integer values are converted to f64 via as cast (lossy for large
values outside the 2^53 exact-integer range).
Sourcepub fn as_array(&self) -> Option<&Array<'de>>
pub fn as_array(&self) -> Option<&Array<'de>>
Returns a borrowed array if this is an array value.
Sourcepub fn as_table(&self) -> Option<&Table<'de>>
pub fn as_table(&self) -> Option<&Table<'de>>
Returns a borrowed table if this is a table value.
Sourcepub fn as_datetime(&self) -> Option<&DateTime>
pub fn as_datetime(&self) -> Option<&DateTime>
Returns a borrowed DateTime if this is a datetime value.
Sourcepub fn as_array_mut(&mut self) -> Option<&mut Array<'de>>
pub fn as_array_mut(&mut self) -> Option<&mut Array<'de>>
Returns a mutable array reference.
Sourcepub fn as_table_mut(&mut self) -> Option<&mut Table<'de>>
pub fn as_table_mut(&mut self) -> Option<&mut Table<'de>>
Returns a mutable table reference.