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).unwrap();
// 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>
impl<'de> MaybeItem<'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.