Skip to main content

MaybeItem

Struct MaybeItem 

Source
pub struct MaybeItem<'de> { /* private fields */ }
Expand description

A nullable reference to a parsed TOML value.

MaybeItem is returned by the index operators ([]) on Item, Table, Array, and MaybeItem itself. It acts like an Option<&Item> that can be further indexed without panicking — chained lookups on missing keys simply propagate the None state.

Use the as_* accessors to extract a value, or call item to get back an Option<&Item>.

§Examples

use toml_spanner::Arena;

let arena = Arena::new();
let table = toml_spanner::parse(r#"
[server]
host = "localhost"
port = 8080
"#, &arena)?;

// Successful nested lookup.
assert_eq!(table["server"]["host"].as_str(), Some("localhost"));
assert_eq!(table["server"]["port"].as_i64(), Some(8080));

// Missing keys propagate through chained indexing without panicking.
assert_eq!(table["server"]["missing"].as_str(), None);
assert_eq!(table["nonexistent"]["deep"]["path"].as_str(), None);

// Convert back to an Option<&Item> when needed.
assert!(table["server"]["host"].item().is_some());
assert!(table["nope"].item().is_none());

Implementations§

Source§

impl<'de> MaybeItem<'de>

Source

pub fn from_ref<'a>(item: &'a Item<'de>) -> &'a Self

Views an Item reference as a MaybeItem.

Source

pub fn item(&self) -> Option<&Item<'de>>

Returns the underlying Item, or None if this is a missing value.

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 span(&self) -> Option<Span>

Returns the source span, or None if this is a missing value.

Source

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

Returns a borrowed Value for pattern matching, or None if missing.

Trait Implementations§

Source§

impl<'de> Index<&str> for MaybeItem<'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 MaybeItem<'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
Source§

impl Sync for MaybeItem<'_>

Auto Trait Implementations§

§

impl<'de> Freeze for MaybeItem<'de>

§

impl<'de> RefUnwindSafe for MaybeItem<'de>

§

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

§

impl<'de> Unpin for MaybeItem<'de>

§

impl<'de> UnsafeUnpin for MaybeItem<'de>

§

impl<'de> UnwindSafe for MaybeItem<'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.