Skip to content

Commit 4724d3e

Browse files
committed
Migrate to latest toml_edit crate
1 parent e3b05e1 commit 4724d3e

File tree

3 files changed

+47
-89
lines changed

3 files changed

+47
-89
lines changed

Cargo.lock

Lines changed: 6 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ thiserror = "1.0.40"
5252
toml = "0.7.4"
5353
tracing = "0.1.37"
5454
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
55-
toml_edit = "0.15"
55+
toml_edit = "0.19.14"
5656
unicode-segmentation = "1.9"
5757
unicode-width = "0.1"
5858
unicode_categories = "0.1"

src/formatting/cargo_toml.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{Config, ErrorKind};
1010
///
1111
/// [the Style Guide]: https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/cargo.md
1212
pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<String, ErrorKind> {
13-
let mut doc = content.parse::<toml_edit::Document>()?;
13+
let mut doc = content.parse::<Document>()?;
1414
let rules = [
1515
&mut SortSection {
1616
current_position: 0,
@@ -31,7 +31,7 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
3131
for rule in rules.into_iter() {
3232
rule.visit_document_mut(&mut doc);
3333
}
34-
// Special handling for falliable rules.
34+
// Special handling for fallible rules.
3535
let mut rule = SortKey { error: None };
3636
rule.visit_document_mut(&mut doc);
3737
if let Some(e) = rule.error {
@@ -190,10 +190,10 @@ impl BlankLine {
190190
}
191191

192192
fn trim_decor_blank_lines(decor: &mut Decor) {
193-
let prefix = decor.prefix().unwrap_or("").to_owned();
194-
let suffix = decor.suffix().unwrap_or("").to_owned();
195-
decor.set_prefix(Self::trim_blank_lines(prefix.as_str()));
196-
decor.set_suffix(Self::trim_blank_lines(suffix.as_str()));
193+
let prefix = decor.prefix().cloned().unwrap_or_default();
194+
let suffix = decor.suffix().cloned().unwrap_or_default();
195+
decor.set_prefix(Self::trim_blank_lines(prefix.as_str().unwrap_or_default()));
196+
decor.set_suffix(Self::trim_blank_lines(suffix.as_str().unwrap_or_default()));
197197
}
198198
}
199199

@@ -221,8 +221,8 @@ impl VisitMut for BlankLine {
221221
});
222222
} else {
223223
let decor = table.decor_mut();
224-
let prefix = decor.prefix().unwrap_or("").to_owned();
225-
decor.set_prefix("\n".to_owned() + &prefix);
224+
let prefix = decor.prefix().cloned().unwrap_or_default();
225+
decor.set_prefix(format!("\n{}", prefix.as_str().unwrap_or_default()));
226226
}
227227
}
228228
}
@@ -237,7 +237,7 @@ impl KeyValue {
237237

238238
impl VisitMut for KeyValue {
239239
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, value: &mut Item) {
240-
let prefix = key.decor().prefix().unwrap_or("").to_owned();
240+
let prefix = key.decor().prefix().cloned().unwrap_or_default();
241241
if Self::can_be_bare_key(key.get()) {
242242
// will remove decors and set the key to the bare key
243243
key.fmt();
@@ -246,8 +246,10 @@ impl VisitMut for KeyValue {
246246
key.decor_mut().set_suffix(" ");
247247
}
248248
// start all key names at the start of a line, but preserve comments
249-
key.decor_mut()
250-
.set_prefix(prefix.trim_end_matches(|c: char| c.is_whitespace() && c != '\n'));
249+
if let Some(prefix) = prefix.as_str() {
250+
key.decor_mut()
251+
.set_prefix(prefix.trim_end_matches(|c: char| c.is_whitespace() && c != '\n'));
252+
}
251253

252254
if let Some(v) = value.as_value_mut() {
253255
v.decor_mut().set_prefix(" ");
@@ -405,7 +407,14 @@ impl VisitMut for TrimSpaces {
405407
let prefix = format!(
406408
"{}{}",
407409
if i == 0 { "" } else { "\n" },
408-
Self::trim_block(decor.prefix().unwrap_or_default())
410+
Self::trim_block(
411+
decor
412+
.prefix()
413+
.cloned()
414+
.unwrap_or_default()
415+
.as_str()
416+
.unwrap_or_default()
417+
)
409418
);
410419
decor.set_prefix(prefix);
411420
};
@@ -420,8 +429,9 @@ impl VisitMut for TrimSpaces {
420429
}
421430
}
422431

423-
if !node.trailing().trim().is_empty() {
424-
let trailing: String = Self::trim_block(node.trailing());
432+
let trailing = node.trailing().as_str().unwrap_or_default();
433+
if !trailing.trim().is_empty() {
434+
let trailing: String = Self::trim_block(trailing);
425435
node.set_trailing(&format!("\n{trailing}"));
426436
} else {
427437
node.set_trailing("");
@@ -431,28 +441,32 @@ impl VisitMut for TrimSpaces {
431441
fn visit_table_mut(&mut self, node: &mut Table) {
432442
let decor = node.decor_mut();
433443
if let Some(prefix) = decor.prefix() {
434-
decor.set_prefix(format!("\n{}", Self::trim_block(prefix)));
435-
}
436-
if let Some(suffix) = decor.suffix() {
437-
decor.set_suffix(Self::trim_suffix(suffix));
444+
if let Some(prefix) = prefix.as_str() {
445+
decor.set_prefix(format!("\n{}", Self::trim_block(prefix)));
446+
}
438447
}
439-
448+
trim_suffix(decor);
440449
self.visit_table_like_mut(node);
441450
}
442451

443452
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, value: &mut Item) {
444453
let decor = key.decor_mut();
445454
if let Some(prefix) = decor.prefix() {
446-
decor.set_prefix(format!("{}", Self::trim_block(prefix)));
455+
if let Some(prefix) = prefix.as_str() {
456+
decor.set_prefix(format!("{}", Self::trim_block(prefix)));
457+
}
447458
}
448-
449459
if let Some(value) = value.as_value_mut() {
450-
let decor = value.decor_mut();
451-
if let Some(suffix) = decor.suffix() {
452-
decor.set_suffix(Self::trim_suffix(suffix));
453-
}
460+
trim_suffix(value.decor_mut());
454461
}
455-
456462
self.visit_item_mut(value);
457463
}
458464
}
465+
466+
fn trim_suffix(decor: &mut Decor) {
467+
if let Some(suffix) = decor.suffix() {
468+
if let Some(suffix) = suffix.as_str() {
469+
decor.set_suffix(TrimSpaces::trim_suffix(suffix));
470+
}
471+
}
472+
}

0 commit comments

Comments
 (0)