pub struct Entry<'a> { /* private fields */ }Expand description
A path-shaped mutable handle to a node in a Document.
Created by Document::entry. Every method routes through the
same CST splice the functional API uses, so edits are byte-faithful
outside the touched span.
§Examples
Single-level edit, equivalent to Document::set:
use noyalib::cst::parse_document;
let mut doc = parse_document("port: 8080\n").unwrap();
doc.entry("port").set("9090").unwrap();
assert_eq!(doc.to_string(), "port: 9090\n");Nested edit by chained drill-down — no string concatenation:
use noyalib::cst::parse_document;
let mut doc = parse_document(
"server:\n host: localhost\n port: 8080\n",
).unwrap();
doc.entry("server").entry("port").set("9090").unwrap();
assert!(doc.to_string().contains("port: 9090"));Insert into a mapping by key without manual path strings:
use noyalib::cst::parse_document;
let mut doc = parse_document(
"metadata:\n labels:\n app: noyalib\n",
).unwrap();
doc.entry("metadata.labels").insert("env", "prod").unwrap();
let out = doc.to_string();
assert!(out.contains("app: noyalib"));
assert!(out.contains("env: prod"));Implementations§
Source§impl<'a> Entry<'a>
impl<'a> Entry<'a>
Sourcepub fn get(&self) -> Option<&str>
pub fn get(&self) -> Option<&str>
The current source slice at this path, or None if the path
does not resolve. Forwards to Document::get.
Sourcepub fn span_at(&self) -> Option<(usize, usize)>
pub fn span_at(&self) -> Option<(usize, usize)>
The byte range (start, end) at this path, or None if the
path does not resolve. Forwards to Document::span_at.
Sourcepub fn comments(&self) -> CommentBundle
pub fn comments(&self) -> CommentBundle
Comments that decorate this node. Forwards to
Document::comments_at.
Sourcepub fn set(self, fragment: &str) -> Result<()>
pub fn set(self, fragment: &str) -> Result<()>
Replace the value at this path with fragment. Forwards to
Document::set.
Consumes the entry. Returns the Document’s reference back
via the borrow expiring; chain a fresh doc.entry(...) for
the next edit.
§Errors
Same as Document::set — invalid path, parse-after-splice
failure, etc.
Sourcepub fn set_value(self, value: &Value) -> Result<()>
pub fn set_value(self, value: &Value) -> Result<()>
Replace the value at this path with the YAML emission of
value. Forwards to Document::set_value.
§Errors
Same as Document::set_value.
Sourcepub fn insert(self, key: &str, fragment: &str) -> Result<()>
pub fn insert(self, key: &str, fragment: &str) -> Result<()>
Insert a key: value pair into the mapping at this path.
If the key already exists, the value is replaced losslessly;
if it is new, a sibling line is spliced after the last
existing entry with matching indent. Forwards to
Document::insert_entry.
§Errors
- The mapping at
self.pathdoes not exist. - The mapping is empty (no anchor for indent — use
Self::setwith a fragment to give the mapping its first entry). - The same parse-after-splice errors as the underlying
replace_span.
Sourcepub fn insert_value(self, key: &str, value: &Value) -> Result<()>
pub fn insert_value(self, key: &str, value: &Value) -> Result<()>
Insert a key: value pair where the value is a typed
Value, auto-formatting both halves.
Forwards to Document::insert_entry_value: the value is
emitted so it re-parses to exactly value — quoted when the
plain spelling would not — the key is quoted when it needs to
be, indentation follows the file’s detected unit, and the
splice is held to a re-parse plus typed-value check that rolls
back on any mismatch.
§Errors
Sourcepub fn push_back_value(self, value: &Value) -> Result<()>
pub fn push_back_value(self, value: &Value) -> Result<()>
Append a typed Value to the sequence at this path.
Forwards to Document::push_back_value.
§Errors
As Document::push_back_value — the path must point to a
non-empty block sequence.
Sourcepub fn insert_after_value(self, value: &Value) -> Result<()>
pub fn insert_after_value(self, value: &Value) -> Result<()>
Insert a typed Value as a new sequence item immediately
after the item at this path. Forwards to
Document::insert_after_value.
§Errors
As Document::insert_after_value — the path must end in an
index and resolve to an item of a block sequence.
Sourcepub fn push_back(self, fragment: &str) -> Result<()>
pub fn push_back(self, fragment: &str) -> Result<()>
Append fragment as a new item to the sequence at this path.
Forwards to Document::push_back.
§Errors
Same as Document::push_back — the path must point to a
non-empty block sequence.
Sourcepub fn insert_after(self, fragment: &str) -> Result<()>
pub fn insert_after(self, fragment: &str) -> Result<()>
Insert fragment as a new sequence item immediately after
the item at this path. Forwards to Document::insert_after.
§Errors
Same as Document::insert_after — the path must end in an
index and resolve to a sequence item in a block sequence.
Sourcepub fn entry(self, child: &str) -> Self
pub fn entry(self, child: &str) -> Self
Drill down to a child path. The returned Entry represents
self.path + "." + child, holding the same mutable borrow. A
child that begins with [ — an index, or the quoted segment
crate::path::quote_key spells for a key holding ., [,
], or * — is appended without the separator.
§Examples
use noyalib::cst::parse_document;
let mut doc = parse_document(
"a:\n b:\n c: 1\n",
).unwrap();
doc.entry("a").entry("b").entry("c").set("2").unwrap();
assert!(doc.to_string().contains("c: 2"));Sourcepub fn or_insert(self, default: &str) -> Result<bool>
pub fn or_insert(self, default: &str) -> Result<bool>
Ensure a value exists at this path: if the entry already
resolves, leave it untouched; if it does not, splice
default as the new value.
Mirrors the std::collections::hash_map::Entry::or_insert
pattern adapted to the CST: the path is resolved through the
green tree first, then the splice runs only on the vacant
branch. The resolution is byte-faithful and skips the typed
Value cache, so the call is cheap on already-occupied paths.
Returns Ok(true) when the splice ran (the entry was
vacant), Ok(false) when the entry was already occupied
(no-op).
§Errors
- For mapping inserts: as
Self::insert— the parent mapping must exist at the dotted-path prefix and must not be empty. - For top-level inserts (no
.in path): rejected with a clear message; useDocument::setwith a fresh document for that case.
§Examples
use noyalib::cst::parse_document;
let mut doc = parse_document(
"metadata:\n labels:\n app: noyalib\n",
).unwrap();
// First call: the path was vacant, so the splice ran.
let inserted = doc.entry("metadata.labels.env")
.or_insert("prod")
.unwrap();
assert!(inserted);
assert!(doc.to_string().contains("env: prod"));
// Second call on the same path: the entry is now occupied,
// so it returns `false` and leaves the value untouched.
let inserted = doc.entry("metadata.labels.env")
.or_insert("staging")
.unwrap();
assert!(!inserted);
assert!(doc.to_string().contains("env: prod"));Sourcepub fn or_insert_with<F>(self, default: F) -> Result<bool>
pub fn or_insert_with<F>(self, default: F) -> Result<bool>
Like Self::or_insert but constructs the default lazily —
the closure runs only on the vacant branch.
§Errors
As Self::or_insert.
§Examples
use noyalib::cst::parse_document;
let mut doc = parse_document(
"metadata:\n labels:\n app: noyalib\n",
).unwrap();
let _ = doc.entry("metadata.labels.env")
.or_insert_with(|| "prod".to_owned())
.unwrap();
assert!(doc.to_string().contains("env: prod"));Sourcepub fn or_insert_value(self, default: &Value) -> Result<bool>
pub fn or_insert_value(self, default: &Value) -> Result<bool>
Like Self::or_insert but takes a typed Value. Honours
the file’s detected indent unit
(Document::indent_unit) when emitting the value.
§Errors
As Self::or_insert, plus serializer errors when the value
cannot be emitted as YAML.
§Examples
use noyalib::cst::parse_document;
use noyalib::Value;
let mut doc = parse_document(
"metadata:\n labels:\n app: noyalib\n",
).unwrap();
let _ = doc.entry("metadata.labels.replicas")
.or_insert_value(&Value::Number(noyalib::Number::Integer(3)))
.unwrap();
assert!(doc.to_string().contains("replicas: 3"));Sourcepub fn and_modify<F>(self, f: F) -> Self
pub fn and_modify<F>(self, f: F) -> Self
If the entry is occupied, run f and let it apply
arbitrary mutations through a fresh sub-borrow of the
document. No-op when the entry is vacant. The closure
receives a Document reference so it can touch any path,
not just self.path — useful for “if this exists, also
update its sibling” patterns.
Returns self for further chaining (typically with
Self::or_insert) — mirrors
std::collections::hash_map::Entry::and_modify.
§Examples
use noyalib::cst::parse_document;
let mut doc = parse_document(
"service:\n port: 8080\n",
).unwrap();
// Increment-style update: read, mutate, write — all under
// the same Entry handle. The closure only runs because the
// path is occupied.
let _ = doc.entry("service.port")
.and_modify(|d| {
let _ = d.set("service.port", "9090");
})
.or_insert("8080") // no-op now: path exists
.unwrap();
assert!(doc.to_string().contains("port: 9090"));Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Entry<'a>
impl<'a> !Sync for Entry<'a>
impl<'a> !UnwindSafe for Entry<'a>
impl<'a> Freeze for Entry<'a>
impl<'a> Send for Entry<'a>
impl<'a> Unpin for Entry<'a>
impl<'a> UnsafeUnpin for Entry<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreimpl<T> MaybeSendSync for T
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);