pub struct Document { /* private fields */ }Expand description
A parsed native file that can be edited without disturbing anything outside the edited entry.
Containers created by Document::upsert or Document::set_list_member
are removed again when a later Document::remove or
set_list_member(.., false) leaves them empty, so an edit that is undone
within one document returns the original bytes. Containers that were in
the file are never removed.
Implementations§
Source§impl Document
impl Document
Sourcepub fn parse(bytes: &[u8], format: Format) -> Result<Self, CoreError>
pub fn parse(bytes: &[u8], format: Format) -> Result<Self, CoreError>
Parses a native file. Empty (or whitespace-only) input is an empty
document, which is how a missing file is edited. Fails with
parse_error when the bytes are not UTF-8 or not valid format.
Sourcepub fn empty(format: Format) -> Self
pub fn empty(format: Format) -> Self
An empty document: the skeleton for a file that does not exist yet.
pub fn format(&self) -> Format
Sourcepub fn style(&self) -> TextStyle
pub fn style(&self) -> TextStyle
BOM, line endings and final newline detected at parse time and
restored by Document::to_bytes.
Sourcepub fn list(&self, container: &[&str]) -> Result<Vec<String>, CoreError>
pub fn list(&self, container: &[&str]) -> Result<Vec<String>, CoreError>
Entry keys of the container at container, in document order. A
missing container has no entries.
Sourcepub fn get(
&self,
container: &[&str],
key: &str,
) -> Result<Option<NativeValue>, CoreError>
pub fn get( &self, container: &[&str], key: &str, ) -> Result<Option<NativeValue>, CoreError>
The value of key in the container at container.
Sourcepub fn upsert(
&mut self,
container: &[&str],
key: &str,
value: &NativeValue,
) -> Result<bool, CoreError>
pub fn upsert( &mut self, container: &[&str], key: &str, value: &NativeValue, ) -> Result<bool, CoreError>
Makes key in the container at container equal to value,
creating missing containers. A new key is appended at the end of the
container. An existing key is updated in place: object members that
keep their value keep their text and comments, members missing from
value are removed and new members are appended. Members the caller
does not model survive only if value carries them (read the entry
with Document::get first). Returns whether the document changed;
on error it is unchanged (unexpected_shape for a container that is
not an object or table, invalid_value for a value the format cannot
hold).
Sourcepub fn remove(
&mut self,
container: &[&str],
key: &str,
) -> Result<bool, CoreError>
pub fn remove( &mut self, container: &[&str], key: &str, ) -> Result<bool, CoreError>
Removes key from the container at container, with the comment
lines directly above it (no blank line between; in JSONC only when
other members remain; in YAML only those indented like the entry);
comments above a blank line stay. Returns whether it was there.