pub struct FormatResult { /* private fields */ }Expand description
The verified output of Document::format or Document::sort, with the
text edits that produce it.
Before it is returned, the output was parsed again with the document’s
dialect and checked. Use as_str or
into_string for the new text, and
edits or into_edits to apply the
change to a buffer that still holds the original source.
is_empty tells whether anything changed.
§Examples
use adotenv::FormatOptions;
let doc = adotenv::parse_strict("PORT=8080 \nHOST =localhost\n")?;
let formatted = doc.format(&FormatOptions::normalize())?;
assert_eq!(formatted.as_str(), "PORT = 8080\nHOST = localhost\n");
let mut text = doc.source().to_string();
for edit in formatted.edits().iter().rev() {
text.replace_range(edit.range.clone(), &edit.replacement);
}
assert_eq!(text, formatted.as_str());
// Formatting the output again changes nothing.
let again = adotenv::parse_strict(formatted.as_str())?;
assert!(again.format(&FormatOptions::normalize())?.is_empty());Implementations§
Source§impl FormatResult
impl FormatResult
Sourcepub fn edits(&self) -> &[TextEdit]
pub fn edits(&self) -> &[TextEdit]
Returns the text edits that turn the original source into the output.
The edits are sorted by start offset and do not overlap. Their ranges
refer to the original Document::source. Apply them in reverse order
of this slice, from the last element to the first; see TextEdit.
Document::sort returns one edit for each reordered group.
Sourcepub fn into_edits(self) -> Vec<TextEdit>
pub fn into_edits(self) -> Vec<TextEdit>
Returns the text edits, consuming the result.
The edits are in the same order as edits returns them.
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Returns the complete verified output, consuming the result.