Skip to content

Commit

Permalink
feat(edit): Add key accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 6, 2024
1 parent 7f3e276 commit eb9992c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ impl InlineTable {
&self.decor
}

/// Returns an accessor to a key's formatting
fn key(&mut self, key: &str) -> Option<&'_ Key> {
self.items.get(key).map(|kv| &kv.key)
}

/// Returns an accessor to a key's formatting
pub fn key_mut(&mut self, key: &str) -> Option<KeyMut<'_>> {
self.items.get_mut(key).map(|kv| kv.key.as_mut())
Expand Down Expand Up @@ -573,6 +578,9 @@ impl TableLike for InlineTable {
self.is_dotted()
}

fn key(&mut self, key: &str) -> Option<&'_ Key> {
self.key(key)
}
fn key_mut(&mut self, key: &str) -> Option<KeyMut<'_>> {
self.key_mut(key)
}
Expand Down
10 changes: 10 additions & 0 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ impl Table {
&self.decor
}

/// Returns an accessor to a key's formatting
fn key(&mut self, key: &str) -> Option<&'_ Key> {
self.items.get(key).map(|kv| &kv.key)
}

/// Returns an accessor to a key's formatting
pub fn key_mut(&mut self, key: &str) -> Option<KeyMut<'_>> {
self.items.get_mut(key).map(|kv| kv.key.as_mut())
Expand Down Expand Up @@ -571,6 +576,8 @@ pub trait TableLike: crate::private::Sealed {
/// Check if this is a wrapper for dotted keys, rather than a standard table
fn is_dotted(&self) -> bool;

/// Returns an accessor to a key's formatting
fn key(&mut self, key: &str) -> Option<&'_ Key>;
/// Returns an accessor to a key's formatting
fn key_mut(&mut self, key: &str) -> Option<KeyMut<'_>>;
/// Returns the decor associated with a given key of the table.
Expand Down Expand Up @@ -635,6 +642,9 @@ impl TableLike for Table {
self.set_dotted(yes)
}

fn key(&mut self, key: &str) -> Option<&'_ Key> {
self.key(key)
}
fn key_mut(&mut self, key: &str) -> Option<KeyMut<'_>> {
self.key_mut(key)
}
Expand Down

0 comments on commit eb9992c

Please sign in to comment.