pub enum Entry<'a> {
Occupied(OccupiedEntry<'a>),
Vacant(VacantEntry<'a>),
}
Expand description
A view into a single location in a map, which may be vacant or occupied.
Variants§
Implementations§
source§impl<'a> Entry<'a>
impl<'a> Entry<'a>
sourcepub fn key(&self) -> &str
pub fn key(&self) -> &str
Returns the entry key
Examples
use toml_edit::Table;
let mut map = Table::new();
assert_eq!("hello", map.entry("hello").key());
sourcepub fn or_insert(self, default: Item) -> &'a mut Item
pub fn or_insert(self, default: Item) -> &'a mut Item
Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
Examples found in repository?
src/index.rs (line 62)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
fn index_mut<'v>(&self, v: &'v mut Item) -> Option<&'v mut Item> {
if let Item::None = *v {
let mut t = InlineTable::default();
t.items.insert(
InternalString::from(self),
TableKeyValue::new(Key::new(self), Item::None),
);
*v = value(Value::InlineTable(t));
}
match *v {
Item::Table(ref mut t) => Some(t.entry(self).or_insert(Item::None)),
Item::Value(ref mut v) => v.as_inline_table_mut().map(|t| {
&mut t
.items
.entry(InternalString::from(self))
.or_insert_with(|| TableKeyValue::new(Key::new(self), Item::None))
.value
}),
_ => None,
}
}
}
impl Index for String {
fn index<'v>(&self, v: &'v Item) -> Option<&'v Item> {
self[..].index(v)
}
fn index_mut<'v>(&self, v: &'v mut Item) -> Option<&'v mut Item> {
self[..].index_mut(v)
}
}
impl<'a, T: ?Sized> Index for &'a T
where
T: Index,
{
fn index<'v>(&self, v: &'v Item) -> Option<&'v Item> {
(**self).index(v)
}
fn index_mut<'v>(&self, v: &'v mut Item) -> Option<&'v mut Item> {
(**self).index_mut(v)
}
}
impl<I> ops::Index<I> for Item
where
I: Index,
{
type Output = Item;
fn index(&self, index: I) -> &Item {
index.index(self).expect("index not found")
}
}
impl<I> ops::IndexMut<I> for Item
where
I: Index,
{
fn index_mut(&mut self, index: I) -> &mut Item {
index.index_mut(self).expect("index not found")
}
}
impl<'s> ops::Index<&'s str> for Table {
type Output = Item;
fn index(&self, key: &'s str) -> &Item {
self.get(key).expect("index not found")
}
}
impl<'s> ops::IndexMut<&'s str> for Table {
fn index_mut(&mut self, key: &'s str) -> &mut Item {
self.entry(key).or_insert(Item::None)
}
More examples
src/parser/state.rs (line 93)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
pub(crate) fn start_aray_table(
&mut self,
path: Vec<Key>,
decor: Decor,
) -> Result<(), CustomError> {
debug_assert!(!path.is_empty());
debug_assert!(self.current_table.is_empty());
debug_assert!(self.current_table_path.is_empty());
// Look up the table on start to ensure the duplicate_key error points to the right line
let root = self.document.as_table_mut();
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
let entry = parent_table
.entry_format(key)
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
entry
.as_array_of_tables()
.ok_or_else(|| CustomError::duplicate_key(&path, path.len() - 1))?;
self.current_table_position += 1;
self.current_table.decor = decor;
self.current_table.set_position(self.current_table_position);
self.current_is_array = true;
self.current_table_path = path;
Ok(())
}
pub(crate) fn start_table(&mut self, path: Vec<Key>, decor: Decor) -> Result<(), CustomError> {
debug_assert!(!path.is_empty());
debug_assert!(self.current_table.is_empty());
debug_assert!(self.current_table_path.is_empty());
// 1. Look up the table on start to ensure the duplicate_key error points to the right line
// 2. Ensure any child tables from an implicit table are preserved
let root = self.document.as_table_mut();
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
if let Some(entry) = parent_table.remove(key.get()) {
match entry {
Item::Table(t) if t.implicit => {
self.current_table = t;
}
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}
self.current_table_position += 1;
self.current_table.decor = decor;
self.current_table.set_position(self.current_table_position);
self.current_is_array = false;
self.current_table_path = path;
Ok(())
}
pub(crate) fn finalize_table(&mut self) -> Result<(), CustomError> {
let mut table = std::mem::take(&mut self.current_table);
let path = std::mem::take(&mut self.current_table_path);
let root = self.document.as_table_mut();
if path.is_empty() {
assert!(root.is_empty());
std::mem::swap(&mut table, root);
} else if self.current_is_array {
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
let entry = parent_table
.entry_format(key)
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
let array = entry
.as_array_of_tables_mut()
.ok_or_else(|| CustomError::duplicate_key(&path, path.len() - 1))?;
array.push(table);
} else {
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
let entry = parent_table.entry_format(key);
match entry {
crate::Entry::Occupied(entry) => {
match entry.into_mut() {
// if [a.b.c] header preceded [a.b]
Item::Table(ref mut t) if t.implicit => {
std::mem::swap(t, &mut table);
}
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}
crate::Entry::Vacant(entry) => {
let item = Item::Table(table);
entry.insert(item);
}
}
}
Ok(())
}
sourcepub fn or_insert_with<F: FnOnce() -> Item>(self, default: F) -> &'a mut Item
pub fn or_insert_with<F: FnOnce() -> Item>(self, default: F) -> &'a mut Item
Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
Examples found in repository?
src/parser/state.rs (lines 185-191)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
pub(crate) fn descend_path<'t, 'k>(
mut table: &'t mut Table,
path: &'k [Key],
dotted: bool,
) -> Result<&'t mut Table, CustomError> {
for (i, key) in path.iter().enumerate() {
let entry = table.entry_format(key).or_insert_with(|| {
let mut new_table = Table::new();
new_table.set_implicit(true);
new_table.set_dotted(dotted);
Item::Table(new_table)
});
match *entry {
Item::Value(ref v) => {
return Err(CustomError::extend_wrong_type(path, i, v.type_name()));
}
Item::ArrayOfTables(ref mut array) => {
debug_assert!(!array.is_empty());
let index = array.len() - 1;
let last_child = array.get_mut(index).unwrap();
table = last_child;
}
Item::Table(ref mut sweet_child_of_mine) => {
table = sweet_child_of_mine;
}
_ => unreachable!(),
}
}
Ok(table)
}