pub struct Document { /* private fields */ }Expand description
A TOML document together with what its file looked like.
The tree is toml_edit’s, reached through Document::tree_mut for the
operations in this crate to work on. What this adds is the byte order
mark, the line ending and the final newline, which toml_edit does not
keep, and a baseline for saying whether anything has been edited.
Implementations§
Source§impl Document
impl Document
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
Sourcepub fn save_to(&mut self, path: &Path) -> Result<(), Error>
pub fn save_to(&mut self, path: &Path) -> Result<(), Error>
Write what Document::render gives to a file, and mark the
document saved.
Written beside the file and renamed over it, so that a failure at any
point leaves the original as it was and never a file half written.
The rename is what makes it one step, and it needs the two on one
file system, which a sibling is. A platform whose sandbox refuses a
sibling, which macOS’s does for a file a dialog granted, needs its
own arm here; PROMPT.md carries that under Phase 3.
A rename needs only the directory to be writable, so two things an in-place write would do for free are done here on purpose: a file marked read-only is refused rather than replaced, and the file keeps the permissions it had rather than the staged file’s. Both were found by hand on 2026-09-07, when a read-only fixture saved without a word and came back mode 0600.
§Errors
When the file is read-only, or the sibling cannot be created, written, or renamed over the file. The document is not marked saved then.
Sourcepub fn tree(&self) -> &DocumentMut
pub fn tree(&self) -> &DocumentMut
The tree, to read.
Sourcepub fn tree_mut(&mut self) -> &mut DocumentMut
pub fn tree_mut(&mut self) -> &mut DocumentMut
The tree, to edit.
Sourcepub fn render(&self) -> String
pub fn render(&self) -> String
What a save writes: the document, with the byte order mark, the line ending and the final newline the file had.
Sourcepub fn mark_saved(&mut self)
pub fn mark_saved(&mut self)
Record that what the document now holds is what is on disk.
Sourcepub fn record(&mut self, group: Option<&[String]>) -> bool
pub fn record(&mut self, group: Option<&[String]>) -> bool
Record whatever changed since the last call as a step, and say whether anything did.
group names the row the change was made in, where there is one.
A change in the same row as the step before joins that step rather
than starting another, so a word typed into a field undoes as a word;
a change with no row, or in another row, is a step of its own. Any
change empties the redo stack, since what was undone no longer
follows from what is there.
Sourcepub fn undo(&mut self) -> bool
pub fn undo(&mut self) -> bool
Put the document back as it was before the last step.
Anything changed since the last Document::record is recorded
first, so that it can be redone rather than lost.
Sourcepub fn lines_of(&self, path: &[String]) -> Option<Range<usize>>
pub fn lines_of(&self, path: &[String]) -> Option<Range<usize>>
The lines of the rendered text that the item at a path occupies,
counted from zero, the end exclusive: a row from its key to the end
of its value, a table or an array-of-tables element its header line.
None for a path that names nothing.
The tree keeps no positions once it can be edited, so this parses the rendered text again, which does. That is a full parse per call, and a caller asks only when the selection or the document has changed.