pub struct Item<'de> { /* private fields */ }Expand description
A parsed TOML value with span information.
Extract values with the as_* methods (as_str,
as_i64, as_table, etc.) or
pattern match via value and value_mut.
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 fast enough.
For structured conversion of larger tables, use
TableHelper with the Context
from parse, which internally uses an index for O(1) lookups.
§Examples
let arena = toml_spanner::Arena::new();
let table = toml_spanner::parse("x = 42", &arena).unwrap();
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 require_custom_string(
&self,
ctx: &mut Context<'de>,
expected: &'static &'static str,
) -> Result<&'de str, Failed>
Available on crate feature from-toml only.
pub fn require_custom_string( &self, ctx: &mut Context<'de>, expected: &'static &'static str, ) -> Result<&'de str, Failed>
from-toml only.Returns a string, or records an error with a custom expected message.
Use instead of require_string when the
expected value is more specific than “a string”, for example
"an IPv4 address" or "a hex color".
Sourcepub fn require_string(&self, ctx: &mut Context<'de>) -> Result<&'de str, Failed>
Available on crate feature from-toml only.
pub fn require_string(&self, ctx: &mut Context<'de>) -> Result<&'de str, Failed>
from-toml only.Returns a string, or records an error if this is not a string.
Sourcepub fn require_array(
&self,
ctx: &mut Context<'de>,
) -> Result<&Array<'de>, Failed>
Available on crate feature from-toml only.
pub fn require_array( &self, ctx: &mut Context<'de>, ) -> Result<&Array<'de>, Failed>
from-toml only.Returns an array reference, or records an error if this is not an array.
Sourcepub fn require_table(
&self,
ctx: &mut Context<'de>,
) -> Result<&Table<'de>, Failed>
Available on crate feature from-toml only.
pub fn require_table( &self, ctx: &mut Context<'de>, ) -> Result<&Table<'de>, Failed>
from-toml only.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>
Available on crate feature from-toml only.
pub fn table_helper<'ctx, 'item>( &'item self, ctx: &'ctx mut Context<'de>, ) -> Result<TableHelper<'ctx, 'item, 'de>, Failed>
from-toml only.Creates a TableHelper for this item, returning an error if it is
not a table.
Typical entry point for implementing FromToml.
Source§impl<'de> Item<'de>
impl<'de> Item<'de>
Sourcepub fn set_ignore_source_formatting_recursively(&mut self)
Available on crate feature to-toml only.
pub fn set_ignore_source_formatting_recursively(&mut self)
to-toml only.Prevents this item and its entire subtree from using source formatting during format-preserving emission. The item will be emitted with clean formatted output even when the rest of the document preserves source formatting via reprojection.
Sourcepub fn ignore_source_formatting_recursively(&self) -> bool
Available on crate feature to-toml only.
pub fn ignore_source_formatting_recursively(&self) -> bool
to-toml only.Returns true if this item will skip source formatting during emission.
Source§impl<'de> Item<'de>
impl<'de> Item<'de>
Sourcepub fn flag(&self) -> u32
pub fn flag(&self) -> u32
Returns the raw 3-bit flag encoding the container sub-kind.
Prefer Table::style or Array::style for a typed alternative.
Source§impl<'de> Item<'de>
impl<'de> Item<'de>
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
Returns an i64 if this is an integer value that fits in the i64 range.
Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
Returns a u64 if this is an integer value that fits in the u64 range.
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 into_table(self) -> Option<Table<'de>>
pub fn into_table(self) -> Option<Table<'de>>
Consumes this item, returning the table if it is one.
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.
Sourcepub fn clone_in(&self, arena: &'de Arena) -> Item<'de>
pub fn clone_in(&self, arena: &'de Arena) -> Item<'de>
Clones this item into arena, sharing existing strings.
Scalar values are copied directly. Tables and arrays are recursively cloned with new arena-allocated storage. String values and table key names continue to reference their original memory, so the source arena (or input string) must remain alive.
Trait Implementations§
Source§impl<'de> FromFlattened<'de> for Item<'de>
Available on crate feature from-toml only.
impl<'de> FromFlattened<'de> for Item<'de>
from-toml only.Source§impl ToFlattened for Item<'_>
Available on crate feature to-toml only.
impl ToFlattened for Item<'_>
to-toml only.Source§fn to_flattened<'a>(
&'a self,
arena: &'a Arena,
table: &mut Table<'a>,
) -> Result<(), ToTomlError>
fn to_flattened<'a>( &'a self, arena: &'a Arena, table: &mut Table<'a>, ) -> Result<(), ToTomlError>
Source§impl ToToml for Item<'_>
Available on crate feature to-toml only.
impl ToToml for Item<'_>
to-toml only.