Skip to content

Commit c9e5a94

Browse files
committed
improve RawString handling
1 parent 09b43a1 commit c9e5a94

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/formatting/cargo_toml.rs

+44-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use itertools::Itertools;
22
use std::cmp::Ordering;
33
use toml_edit::{
4-
visit_mut::*, Decor, Document, Formatted, Item, KeyMut, Table, TableLike, TomlError, Value,
4+
visit_mut::*, Decor, Document, Formatted, Item, KeyMut, RawString, Table, TableLike, TomlError,
5+
Value,
56
};
67

78
use crate::{Config, ErrorKind};
@@ -190,10 +191,12 @@ impl BlankLine {
190191
}
191192

192193
fn trim_decor_blank_lines(decor: &mut Decor) {
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()));
194+
if let Some(prefix) = decor.prefix().map(raw_string_as_str) {
195+
decor.set_prefix(Self::trim_blank_lines(prefix));
196+
}
197+
if let Some(suffix) = decor.suffix().map(raw_string_as_str) {
198+
decor.set_suffix(Self::trim_blank_lines(suffix));
199+
}
197200
}
198201
}
199202

@@ -221,8 +224,10 @@ impl VisitMut for BlankLine {
221224
});
222225
} else {
223226
let decor = table.decor_mut();
224-
let prefix = decor.prefix().cloned().unwrap_or_default();
225-
decor.set_prefix(format!("\n{}", prefix.as_str().unwrap_or_default()));
227+
decor.set_prefix(format!(
228+
"\n{}",
229+
decor.prefix().map(raw_string_as_str).unwrap_or_default()
230+
));
226231
}
227232
}
228233
}
@@ -237,7 +242,11 @@ impl KeyValue {
237242

238243
impl VisitMut for KeyValue {
239244
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, value: &mut Item) {
240-
let prefix = key.decor().prefix().cloned().unwrap_or_default();
245+
let original_prefix = key
246+
.decor()
247+
.prefix()
248+
.map(raw_string_as_str)
249+
.map(String::from);
241250
if Self::can_be_bare_key(key.get()) {
242251
// will remove decors and set the key to the bare key
243252
key.fmt();
@@ -246,7 +255,7 @@ impl VisitMut for KeyValue {
246255
key.decor_mut().set_suffix(" ");
247256
}
248257
// start all key names at the start of a line, but preserve comments
249-
if let Some(prefix) = prefix.as_str() {
258+
if let Some(prefix) = original_prefix {
250259
key.decor_mut()
251260
.set_prefix(prefix.trim_end_matches(|c: char| c.is_whitespace() && c != '\n'));
252261
}
@@ -404,19 +413,14 @@ impl VisitMut for TrimSpaces {
404413
self.visit_table_mut(node);
405414

406415
let set_prefix = |decor: &mut Decor, i: usize| {
407-
let prefix = format!(
408-
"{}{}",
409-
if i == 0 { "" } else { "\n" },
410-
Self::trim_block(
411-
decor
412-
.prefix()
413-
.cloned()
414-
.unwrap_or_default()
415-
.as_str()
416-
.unwrap_or_default()
417-
)
418-
);
419-
decor.set_prefix(prefix);
416+
if let Some(prefix) = decor.prefix().map(raw_string_as_str) {
417+
let prefix = format!(
418+
"{}{}",
419+
if i == 0 { "" } else { "\n" },
420+
Self::trim_block(prefix)
421+
);
422+
decor.set_prefix(prefix);
423+
}
420424
};
421425
let table = node.as_table_mut();
422426
for (i, (_, item)) in table.iter_mut().enumerate() {
@@ -429,9 +433,9 @@ impl VisitMut for TrimSpaces {
429433
}
430434
}
431435

432-
let trailing = node.trailing().as_str().unwrap_or_default();
436+
let trailing = raw_string_as_str(node.trailing());
433437
if !trailing.trim().is_empty() {
434-
let trailing: String = Self::trim_block(trailing);
438+
let trailing = Self::trim_block(trailing);
435439
node.set_trailing(&format!("\n{trailing}"));
436440
} else {
437441
node.set_trailing("");
@@ -440,33 +444,33 @@ impl VisitMut for TrimSpaces {
440444

441445
fn visit_table_mut(&mut self, node: &mut Table) {
442446
let decor = node.decor_mut();
443-
if let Some(prefix) = decor.prefix() {
444-
if let Some(prefix) = prefix.as_str() {
445-
decor.set_prefix(format!("\n{}", Self::trim_block(prefix)));
446-
}
447+
if let Some(prefix) = decor.prefix().map(raw_string_as_str) {
448+
decor.set_prefix(format!("\n{}", Self::trim_block(prefix)));
449+
}
450+
if let Some(suffix) = decor.suffix().map(raw_string_as_str) {
451+
decor.set_suffix(Self::trim_suffix(suffix));
447452
}
448-
trim_suffix(decor);
449453
self.visit_table_like_mut(node);
450454
}
451455

452456
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, value: &mut Item) {
453457
let decor = key.decor_mut();
454-
if let Some(prefix) = decor.prefix() {
455-
if let Some(prefix) = prefix.as_str() {
456-
decor.set_prefix(format!("{}", Self::trim_block(prefix)));
457-
}
458+
if let Some(prefix) = decor.prefix().map(raw_string_as_str) {
459+
decor.set_prefix(format!("{}", Self::trim_block(prefix)));
458460
}
461+
459462
if let Some(value) = value.as_value_mut() {
460-
trim_suffix(value.decor_mut());
463+
let decor = value.decor_mut();
464+
if let Some(suffix) = decor.suffix().map(raw_string_as_str) {
465+
decor.set_suffix(Self::trim_suffix(suffix));
466+
}
461467
}
462468
self.visit_item_mut(value);
463469
}
464470
}
465471

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+
/// Note: in `Document::from_str`, the document is despanned, so we can safely unwrap `as_str`
473+
/// when handling `RawString`.
474+
fn raw_string_as_str(raw_string: &RawString) -> &str {
475+
raw_string.as_str().expect("should already be despanded")
472476
}

0 commit comments

Comments
 (0)