Skip to main content

Document

Struct Document 

Source
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

Source

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.

Source

pub fn empty(format: Format) -> Self

An empty document: the skeleton for a file that does not exist yet.

Source

pub fn format(&self) -> Format

Source

pub fn style(&self) -> TextStyle

BOM, line endings and final newline detected at parse time and restored by Document::to_bytes.

Source

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.

Source

pub fn get( &self, container: &[&str], key: &str, ) -> Result<Option<NativeValue>, CoreError>

The value of key in the container at container.

Source

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).

Source

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.

Source

pub fn set_list_member( &mut self, list: &[&str], member: &str, present: bool, ) -> Result<bool, CoreError>

Adds the string member to the array at list (creating it) when present, or removes every occurrence of it otherwise. A new member is appended. Returns whether the document changed.

Source

pub fn to_bytes(&self) -> Vec<u8> ⓘ

The document text with the original BOM, line endings and final newline.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Self

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§

§

impl Forget for Document

§

impl Freeze for Document

§

impl Move for Document

§

impl RefUnwindSafe for Document

§

impl Send for Document
where RawVecInner: Send, Arc<OnceLock<Result<Option<Node>, CoreError>>>: Send, RawTable<usize>: Send, Arc<Mutex<Option<(String, Arc<Tree>)>>>: Send,

§

impl Sync for Document
where RawVecInner: Sync, Arc<OnceLock<Result<Option<Node>, CoreError>>>: Sync, RawTable<usize>: Sync, Arc<Mutex<Option<(String, Arc<Tree>)>>>: Sync,

§

impl Unpin for Document
where *const u8 is !null: Unpin, Arc<OnceLock<Result<Option<Node>, CoreError>>>: Unpin, Arc<Mutex<Option<(String, Arc<Tree>)>>>: Unpin,

§

impl UnsafeUnpin for Document

§

impl UnwindSafe for Document

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more