Skip to main content

Item

Struct Item 

Source
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.

§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>

Source

pub fn span(&self) -> Span

Returns the byte-offset span of this value in the source document.

Source

pub fn type_str(&self) -> &'static str

Returns the TOML type name (e.g. "string", "integer", "table").

Source§

impl<'de> Item<'de>

Source

pub fn value(&self) -> Value<'_, 'de>

Returns a borrowed view for pattern matching.

Source

pub fn value_mut(&mut self) -> ValueMut<'_, 'de>

Returns a mutable view for pattern matching.

Source§

impl<'de> Item<'de>

Source

pub fn as_str(&self) -> Option<&str>

Returns a borrowed string if this is a string value.

Source

pub fn as_i64(&self) -> Option<i64>

Returns an i64 if this is an integer value.

Source

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).

Source

pub fn as_bool(&self) -> Option<bool>

Returns a bool if this is a boolean value.

Source

pub fn as_array(&self) -> Option<&Array<'de>>

Returns a borrowed array if this is an array value.

Source

pub fn as_table(&self) -> Option<&Table<'de>>

Returns a borrowed table if this is a table value.

Source

pub fn expect_array(&mut self) -> Result<&mut Array<'de>, Error>

Returns a mutable array reference, or an error if this is not an array.

Source

pub fn expect_table(&mut self) -> Result<&mut Table<'de>, Error>

Returns a mutable table reference, or an error if this is not a table.

This is the typical entry point for implementing Deserialize.

Source

pub fn as_array_mut(&mut self) -> Option<&mut Array<'de>>

Returns a mutable array reference.

Source

pub fn as_table_mut(&mut self) -> Option<&mut Table<'de>>

Returns a mutable table reference.

Source

pub fn has_keys(&self) -> bool

Returns true if the value is a table and is non-empty.

Source

pub fn has_key(&self, key: &str) -> bool

Returns true if the value is a table and has the specified key.

Source§

impl<'de> Item<'de>

Source

pub fn expected(&self, expected: &'static str) -> Error

Creates an “expected X, found Y” error using this value’s type and span.

Source

pub fn parse<T, E>(&mut self) -> Result<T, Error>
where T: FromStr<Err = E>, E: Display,

Takes a string value and parses it via std::str::FromStr.

Returns an error if the value is not a string or parsing fails.

Source

pub fn take_string( &mut self, msg: Option<&'static str>, ) -> Result<&'de str, Error>

Takes the value as a string, returning an error if it is not a string.

Trait Implementations§

Source§

impl Debug for Item<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Index<&str> for Item<'de>

Source§

type Output = MaybeItem<'de>

The returned type after indexing.
Source§

fn index(&self, index: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'de> Index<usize> for Item<'de>

Source§

type Output = MaybeItem<'de>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<'de> Freeze for Item<'de>

§

impl<'de> RefUnwindSafe for Item<'de>

§

impl<'de> !Send for Item<'de>

§

impl<'de> !Sync for Item<'de>

§

impl<'de> Unpin for Item<'de>

§

impl<'de> UnsafeUnpin for Item<'de>

§

impl<'de> UnwindSafe for Item<'de>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.