Skip to main content

DocumentFile

Struct DocumentFile 

Source
pub struct DocumentFile { /* private fields */ }
Expand description

A file-backed document: owns the path, format, original source text, and parsed value.

Reading (open) is always allowed. Mutation methods guard against unsafe targets (symlinks, hardlinked files) and write through a same-directory temp file that is atomically renamed over the original — see DocumentFile::save_atomic.

Implementations§

Source§

impl DocumentFile

Source

pub fn open( path: impl AsRef<Path>, format_override: Option<Format>, ) -> DocumentResult<DocumentFile>

Open and parse path.

format_override takes precedence; otherwise the format is detected from the file extension via Format::detect. Reading is always allowed — this does not run the mutation guard.

Source

pub fn open_capped( path: impl AsRef<Path>, format_override: Option<Format>, max_bytes: u64, ) -> DocumentResult<DocumentFile>

Open and parse path like DocumentFile::open, but first reject any non-regular file, or any file larger than max_bytes, without reading its contents.

Use this over open when reading untrusted or secret-bearing config, where an unbounded read of an arbitrary path is a denial-of-service risk.

Source

pub fn path(&self) -> &Path

The file path this document was opened from.

Source

pub fn value(&self) -> &Value

Borrow the currently parsed value (reflects the last successful edit, if any).

Source

pub fn value_at(&self, path: &str) -> DocumentResult<Value>

Resolve a dotted path against the parsed document and return the value at that address.

One-call read counterpart to set: equivalent to value followed by crate::document::get_path, for callers that only need to read one address out of a file.

Source

pub fn format(&self) -> Format

The format this file was opened as.

Source

pub fn source(&self) -> &str

Borrow the current source text (reflects the last successful edit, if any).

Source

pub fn ensure_mutable(&self, operation: &str) -> DocumentResult<()>

Preflight-check that this file is safe to mutate — not a symlink, and on unix not hardlinked — without performing any write.

Every mutation method (DocumentFile::set and friends) already runs this same guard itself before writing, so calling it directly is only useful when a caller must front-run a separate side effect with the same guarantee — for example, a CLI that reads a secret from stdin for set should refuse an unsafe target before consuming that input, not after.

Source

pub fn set(&mut self, key: &str, value: Value) -> DocumentResult<()>

Set key to the typed value, preserving the rest of the source document.

Source

pub fn add( &mut self, key: &str, slug: &str, slug_field: &str, fields: &[(String, Value)], ) -> DocumentResult<()>

Add a new element to the keyed list at key, identified by slug/slug_field, with the given fields. Preserves the rest of the source document.

Only JSON and YAML backends implement a source-preserving keyed-collection editor today; other formats return DocumentError::UnsupportedOperation.

Source

pub fn remove( &mut self, key: &str, slug: &str, slug_field: &str, ) -> DocumentResult<()>

Remove the element identified by slug/slug_field from the keyed list at key. Preserves the rest of the source document.

Only JSON and YAML backends implement a source-preserving keyed-collection editor today; other formats return DocumentError::UnsupportedOperation.

Source

pub fn unset(&mut self, key: &str) -> DocumentResult<()>

Remove the entry at key entirely. Preserves the rest of the source document.

Trait Implementations§

Source§

impl Debug for DocumentFile

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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