Skip to main content

Document

Struct Document 

Source
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

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>

Parse a file’s bytes.

§Errors

When the bytes are not UTF-8, or the text is not TOML.

Source

pub fn parse(text: &str) -> Result<Self, Error>

Parse text, which may begin with a byte order mark.

§Errors

When the text is not TOML.

Source

pub fn from_path(path: &Path) -> Result<Self, Error>

Read and parse a file.

§Errors

When the file cannot be read, is not UTF-8, or is not TOML.

Source

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.

Source

pub fn tree(&self) -> &DocumentMut

The tree, to read.

Source

pub fn tree_mut(&mut self) -> &mut DocumentMut

The tree, to edit.

Source

pub fn newline(&self) -> Newline

The line ending the file was written with.

Source

pub fn has_bom(&self) -> bool

Whether the file began with a byte order mark.

Source

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.

Source

pub fn edited(&self) -> bool

Whether the document differs from what was parsed or last saved.

Source

pub fn mark_saved(&mut self)

Record that what the document now holds is what is on disk.

Source

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.

Source

pub fn can_undo(&self) -> bool

Whether there is a step to undo.

Source

pub fn can_redo(&self) -> bool

Whether there is a step to redo.

Source

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.

Source

pub fn redo(&mut self) -> bool

Put back the last step undone.

Source

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.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.