Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Preserve dotted-key ordering #808

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 5 additions & 61 deletions crates/toml_edit/src/inline_table.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, because of this, we could remove the ordering of the IndexMap completely now, and instead rely on the position value, thereby reducing the chances of future confusion.

Table and InlineTable have the semantics of an IndexMap. We need to preserve the order of content as it gets inserted. We could set a position on insertion.

Also, I'm unsure about allowing key positions to be moved between tables because the position is dependent on the other values within the same table and you can't easily translate that from one table to the next.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could set a position on insertion.

I think I missed this when I was initially reading your feedback, I did not investigate this as an option yet, I'll take a look at this in the next week or so when I can.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::iter::FromIterator;
use crate::key::Key;
use crate::repr::Decor;
use crate::table::{Iter, IterMut, KeyValuePairs, TableLike};
use crate::internal_table::{GetTableValues, SortTable, SortTableBy};
use crate::{InternalString, Item, KeyMut, RawString, Table, Value};

/// Type representing a TOML inline table,
Expand Down Expand Up @@ -51,30 +52,7 @@ impl InlineTable {
///
/// For example, this will return dotted keys
pub fn get_values(&self) -> Vec<(Vec<&Key>, &Value)> {
let mut values = Vec::new();
let root = Vec::new();
self.append_values(&root, &mut values);
values
}

pub(crate) fn append_values<'s>(
&'s self,
parent: &[&'s Key],
values: &mut Vec<(Vec<&'s Key>, &'s Value)>,
) {
for (key, value) in self.items.iter() {
let mut path = parent.to_vec();
path.push(key);
match value {
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
table.append_values(&path, values);
}
Item::Value(value) => {
values.push((path, value));
}
_ => {}
}
}
GetTableValues::get_values(self)
}

/// Auto formats the table.
Expand All @@ -84,52 +62,18 @@ impl InlineTable {

/// Sorts the key/value pairs by key.
pub fn sort_values(&mut self) {
// Assuming standard tables have their position set and this won't negatively impact them
self.items.sort_keys();
for value in self.items.values_mut() {
match value {
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
table.sort_values();
}
_ => {}
}
}
SortTable::sort_values(self)
}

/// Sort Key/Value Pairs of the table using the using the comparison function `compare`.
///
/// The comparison function receives two key and value pairs to compare (you can sort by keys or
/// values or their combination as needed).
pub fn sort_values_by<F>(&mut self, mut compare: F)
pub fn sort_values_by<F>(&mut self, compare: F)
where
F: FnMut(&Key, &Value, &Key, &Value) -> std::cmp::Ordering,
{
self.sort_values_by_internal(&mut compare);
}

fn sort_values_by_internal<F>(&mut self, compare: &mut F)
where
F: FnMut(&Key, &Value, &Key, &Value) -> std::cmp::Ordering,
{
let modified_cmp =
|key1: &Key, val1: &Item, key2: &Key, val2: &Item| -> std::cmp::Ordering {
match (val1.as_value(), val2.as_value()) {
(Some(v1), Some(v2)) => compare(key1, v1, key2, v2),
(Some(_), None) => std::cmp::Ordering::Greater,
(None, Some(_)) => std::cmp::Ordering::Less,
(None, None) => std::cmp::Ordering::Equal,
}
};

self.items.sort_by(modified_cmp);
for value in self.items.values_mut() {
match value {
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
table.sort_values_by_internal(compare);
}
_ => {}
}
}
SortTableBy::<Value>::sort_values_by(self, compare)
}

/// If a table has no key/value pairs and implicit, it will not be displayed.
Expand Down
179 changes: 179 additions & 0 deletions crates/toml_edit/src/internal_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use crate::{InlineTable, Item, Key, Table, Value};
/// a place for helper methods supporting the table-like impls

use crate::table::KeyValuePairs;

/// GetTableValues provides the logic for displaying a table's items in their parsed order
pub(crate) trait GetTableValues {
Comment on lines +4 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is a roundabout way to share code for manipulating KeyValuePairs. Seems like the functionality should just be writtenm for KeyValuePairs and shared

imo if I were to do this, I would

  • Move KeyValuePairs into here
  • Either rename the file to be about KeyValuePairs or rename the type (though not a fan of InternalTable though InnerTable might work)
  • Either make get_values and sort_values either non-member functions that take KeyValuePairs or make KeyValuePairs a newtype (which might add its own complexity)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to split this refactor out into its own commit. It'll likely make it easier to review and get merged which can speed up reviewing both PRs.

Also, please consider if there are intermediate steps to the above that should be broken out into their own commit (e.g. adding a newtype of we go that route)

fn items(&self) -> &KeyValuePairs;

fn get_values(&self) -> Vec<(Vec<&Key>, &Value)> {
let mut values = Vec::new();
let root = Vec::new();
self.append_values(&root, &mut values);
values
}

fn append_values<'s>(
&'s self,
parent: &[&'s Key],
values: &mut Vec<(Vec<&'s Key>, &'s Value)>,
) {
for (key, item) in self.items().iter() {
let mut path = parent.to_vec();
path.push(key);
match item {
Item::Table(table) if table.is_dotted() => {
GetTableValues::append_values(table, &path, values)
}
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
GetTableValues::append_values(table, &path, values)
}
Item::Value(value) => {
values.push((path, value))
}
_ => {}
}
}
}
}

/// SortTable provides the logic for sorting a table's items by its keys
pub(crate) trait SortTable {
fn items_mut(&mut self) -> &mut KeyValuePairs;

fn sort_values(&mut self) {
// Assuming standard tables have their doc_position set and this won't negatively impact them
self.items_mut().sort_keys();
assign_sequential_key_positions(self.items_mut(), |item| {
match item {
Item::Table(table) if table.is_dotted() => {
SortTable::sort_values(table)
}
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
SortTable::sort_values(table)
}
_ => {}
}
});
}
}

/// SortTableBy provides the logic for sorting a table by a custom comparison
pub(crate) trait SortTableBy<It> : SortTable
where
It: for<'a> TryFrom<&'a Item>
{
fn sort_values_by<F>(&mut self, compare: F)
where
F: FnMut(&Key, &It, &Key, &It) -> std::cmp::Ordering,
{
// intended for `InlineTable`s, where some `Item`s might not be `Value`s,
// in the case of dotted keys mostly I expect.
// but for `Table`s the `(Some,Some)` will be the only one used.
self.sort_vals_by_direct(
&mut Self::generalize(compare)
)
}

/// no modification to the comparing Fn in this one,
/// allows for slightly improved recursion that does not continuously
/// re-modify the comparison function.
fn sort_vals_by_direct<F>(&mut self, compare: &mut F)
where
F: FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering
{
self.items_mut().sort_by(|key1, val1, key2, val2| {
compare(key1, val1, key2, val2)
});

assign_sequential_key_positions(self.items_mut(), |value| {
match value {
Item::Table(table) if table.is_dotted() => {
SortTableBy::<Item>::sort_values_by(
table,
|k1, i1, k2, i2| {
compare(k1, i1, k2, i2)
}
)
},
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
SortTableBy::<Value>::sort_values_by(
table,
|k1, i1, k2, i2| {
let s1 = &Item::from(i1);
let s2 = &Item::from(i2);
compare(k1, s1, k2, s2)
}
)
},
_ => {}
};
});
}

fn generalize<'a, F>(mut compare: F) -> Box<dyn FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering + 'a>
where
F: FnMut(&Key, &It, &Key, &It) -> std::cmp::Ordering + 'a,
{
Box::new(move |key1, s1, key2, s2| {
match (It::try_from(s1).ok(), It::try_from(s2).ok()) {
(Some(v1), Some(v2)) => compare(key1, &v1, key2, &v2),
(Some(_), None) => std::cmp::Ordering::Greater,
(None, Some(_)) => std::cmp::Ordering::Less,
(None, None) => std::cmp::Ordering::Equal,
}
})
}
}

fn assign_sequential_key_positions<F>(items: &mut KeyValuePairs, mut recursive_step: F)
where
F: FnMut(&mut Item),
{
use indexmap::map::MutableKeys;
for (pos, (key, value)) in items.iter_mut2().enumerate() {
key.set_position(Some(pos));
recursive_step(value);
}
}

impl TryFrom<&Item> for Value {
type Error = String;

fn try_from(value: &Item) -> Result<Self, Self::Error> {
let err = "cannot extract Value from Non-Value Item:";
match value {
Item::Value(v) => Ok((*v).clone()),
it => it.as_value().map(|v| v.clone()).ok_or(
format!("{err}: {it:?}")
),
}
}

}

impl GetTableValues for Table {
fn items(&self) -> &KeyValuePairs {
&self.items
}
}
impl GetTableValues for InlineTable {
fn items(&self) -> &KeyValuePairs {
&self.items
}
}

impl SortTable for Table {
fn items_mut(&mut self) -> &mut KeyValuePairs {
&mut self.items
}
}
impl SortTable for InlineTable {
fn items_mut(&mut self) -> &mut KeyValuePairs {
&mut self.items
}
}

impl SortTableBy<Item> for Table {}
impl SortTableBy<Value> for InlineTable {}
1 change: 1 addition & 0 deletions crates/toml_edit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod error;
mod index;
mod inline_table;
mod internal_string;
mod internal_table;
mod item;
mod key;
#[cfg(feature = "parse")]
Expand Down
70 changes: 5 additions & 65 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use indexmap::map::IndexMap;
use crate::key::Key;
use crate::repr::Decor;
use crate::value::DEFAULT_VALUE_DECOR;
use crate::internal_table::{GetTableValues, SortTable, SortTableBy};
use crate::{InlineTable, InternalString, Item, KeyMut, Value};

/// Type representing a TOML non-inline table
Expand Down Expand Up @@ -64,38 +65,7 @@ impl Table {
///
/// For example, this will return dotted keys
pub fn get_values(&self) -> Vec<(Vec<&Key>, &Value)> {
let mut values = Vec::new();
let root = Vec::new();
self.append_values(&root, &mut values);
values
}

fn append_values<'s>(
&'s self,
parent: &[&'s Key],
values: &mut Vec<(Vec<&'s Key>, &'s Value)>,
) {
for (key, value) in self.items.iter() {
let mut path = parent.to_vec();
path.push(key);
match value {
Item::Table(table) if table.is_dotted() => {
table.append_values(&path, values);
}
Item::Value(value) => {
if let Some(table) = value.as_inline_table() {
if table.is_dotted() {
table.append_values(&path, values);
} else {
values.push((path, value));
}
} else {
values.push((path, value));
}
}
_ => {}
}
}
GetTableValues::get_values(self)
}

/// Auto formats the table.
Expand All @@ -107,48 +77,18 @@ impl Table {
///
/// Doesn't affect subtables or subarrays.
pub fn sort_values(&mut self) {
// Assuming standard tables have their doc_position set and this won't negatively impact them
self.items.sort_keys();
for value in self.items.values_mut() {
match value {
Item::Table(table) if table.is_dotted() => {
table.sort_values();
}
_ => {}
}
}
SortTable::sort_values(self)
}

/// Sort Key/Value Pairs of the table using the using the comparison function `compare`.
///
/// The comparison function receives two key and value pairs to compare (you can sort by keys or
/// values or their combination as needed).
pub fn sort_values_by<F>(&mut self, mut compare: F)
pub fn sort_values_by<F>(&mut self, compare: F)
where
F: FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering,
{
self.sort_values_by_internal(&mut compare);
}

fn sort_values_by_internal<F>(&mut self, compare: &mut F)
where
F: FnMut(&Key, &Item, &Key, &Item) -> std::cmp::Ordering,
{
let modified_cmp =
|key1: &Key, val1: &Item, key2: &Key, val2: &Item| -> std::cmp::Ordering {
compare(key1, val1, key2, val2)
};

self.items.sort_by(modified_cmp);

for value in self.items.values_mut() {
match value {
Item::Table(table) if table.is_dotted() => {
table.sort_values_by_internal(compare);
}
_ => {}
}
}
SortTableBy::<Item>::sort_values_by(self, compare)
}

/// If a table has no key/value pairs and implicit, it will not be displayed.
Expand Down
6 changes: 4 additions & 2 deletions crates/toml_edit/tests/testsuite/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ struct Test {

fn given(input: &str) -> Test {
let doc = input.parse::<DocumentMut>();
assert!(doc.is_ok());
Test { doc: doc.unwrap() }
match doc {
Err(e) => panic!("{}", e),
_ => Test { doc: doc.unwrap() }
}
}

impl Test {
Expand Down