Skip to content

Commit

Permalink
Merge pull request #560 from epage/despan
Browse files Browse the repository at this point in the history
fix(edit): Ensure Key::from_str doesnt rely on Document
  • Loading branch information
epage committed May 23, 2023
2 parents 0945943 + c9b2192 commit 17ba2c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,17 @@ impl Key {
}

fn try_parse_simple(s: &str) -> Result<Key, crate::TomlError> {
parser::parse_key(s)
let mut key = parser::parse_key(s)?;
key.despan(s);
Ok(key)
}

fn try_parse_path(s: &str) -> Result<Vec<Key>, crate::TomlError> {
parser::parse_key_path(s)
let mut keys = parser::parse_key_path(s)?;
for key in &mut keys {
key.despan(s);
}
Ok(keys)
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/toml_edit/tests/testsuite/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,3 +1475,16 @@ p.a=4
let actual = document.to_string();
assert_eq(input, actual);
}

#[test]
fn despan_keys() {
let mut doc = r#"aaaaaa = 1"#.parse::<Document>().unwrap();
let key = "bbb".parse::<Key>().unwrap();
let table = doc.as_table_mut();
table.insert_formatted(
&key,
toml_edit::Item::Value(Value::Integer(toml_edit::Formatted::new(2))),
);

assert_eq!(doc.to_string(), "aaaaaa = 1\nbbb = 2\n");
}

0 comments on commit 17ba2c0

Please sign in to comment.