Skip to content

Commit

Permalink
Merge pull request #786 from epage/table
Browse files Browse the repository at this point in the history
fix(edit): Allow creating Tables from Items
  • Loading branch information
epage committed Sep 17, 2024
2 parents 1bfb7d7 + f8e909c commit 8911cb1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions crates/toml_edit/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ impl FromStr for Item {
}
}

impl<'b> From<&'b Item> for Item {
fn from(s: &'b Item) -> Self {
s.clone()
}
}

impl<V: Into<Value>> From<V> for Item {
fn from(s: V) -> Self {
Item::Value(s.into())
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,17 @@ impl std::fmt::Display for Table {
}
}

impl<K: Into<Key>, V: Into<Value>> Extend<(K, V)> for Table {
impl<K: Into<Key>, V: Into<Item>> Extend<(K, V)> for Table {
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
for (key, value) in iter {
let key = key.into();
let value = Item::Value(value.into());
let value = value.into();
self.items.insert(key, value);
}
}
}

impl<K: Into<Key>, V: Into<Value>> FromIterator<(K, V)> for Table {
impl<K: Into<Key>, V: Into<Item>> FromIterator<(K, V)> for Table {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
Expand Down

0 comments on commit 8911cb1

Please sign in to comment.