Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Available on crate feature std only.
Expand description

A YAML document with byte-faithful source preservation, typed data access, and path-targeted edits.

Document carries three coordinated views of the same input: an immutable green tree that reproduces the source byte-for-byte, a typed Value for data access, and an internal span tree that maps any Value-shaped path back to a byte range. Edits flow through Document::replace_span (the primitive) and Document::set (the path-shaped wrapper); untouched bytes — indentation, comments, blank lines, sibling entries — are preserved verbatim.

§Examples

Read-only round-trip:

use noyalib::cst::parse_document;

let src = "name: noyalib  # the project\nversion: 0.0.1\n";
let doc = parse_document(src).unwrap();
assert_eq!(doc.to_string(), src);

Path-targeted edit:

use noyalib::cst::parse_document;

let mut doc = parse_document("name: foo\nversion: 0.0.1\n").unwrap();
doc.set("version", "0.0.2").unwrap();
assert_eq!(doc.to_string(), "name: foo\nversion: 0.0.2\n");

Implementations§

Source§

impl Document

Source

pub fn anchors(&self) -> Vec<AnchorInfo>

Every &name declaration in this document, in source order.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document(
    "defaults: &cfg\n  port: 8080\nserver:\n  <<: *cfg\n",
).unwrap();
let anchors = doc.anchors();
assert_eq!(anchors.len(), 1);
assert_eq!(anchors[0].name, "cfg");
Source

pub fn aliases(&self) -> Vec<AliasInfo>

Every *name reference in this document, in source order.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document("a: &x 1\nb: *x\nc: *x\n").unwrap();
let aliases = doc.aliases();
assert_eq!(aliases.len(), 2);
Source

pub fn aliases_of(&self, name: &str) -> Vec<AliasInfo>

Aliases whose name matches name, in source order.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document("a: &x 1\nb: &y 2\nc: *x\nd: *y\n").unwrap();
let xs = doc.aliases_of("x");
assert_eq!(xs.len(), 1);
assert_eq!(xs[0].name, "x");
Source

pub fn materialise_alias_at(&mut self, position: usize) -> Result<()>

Replace the *name alias whose mark begins at byte position with the source text of the matching &name’s scalar value.

After the splice, the alias’s site holds an independent copy of the anchored scalar — subsequent edits to the anchored value do not propagate to it.

§Errors
  • position does not start an *name token.
  • The named anchor is not declared earlier in source order.
  • The anchored value is not a scalar (multi-line block collections require manual handling — read the anchor’s span via Self::anchors and splice with Self::replace_span).
  • The same parse-after-edit errors as Self::replace_span.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("a: &x 7\nb: *x\n").unwrap();
let alias = doc.aliases()[0].clone();
doc.materialise_alias_at(alias.mark_span.0).unwrap();
assert!(!doc.to_string().contains("*x"));
assert!(doc.to_string().contains("b: 7"));
Source

pub fn materialise_aliases_of(&mut self, name: &str) -> Result<usize>

Materialise every alias whose name matches name. Returns the count of aliases replaced.

Aliases are processed in reverse source order so each splice’s offsets stay valid for later (earlier in source) aliases.

§Errors

As Self::materialise_alias_at. The first failing alias aborts the batch — already-materialised aliases stay materialised, the rest are unchanged.

§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("a: &x 7\nb: *x\nc: *x\n").unwrap();
let n = doc.materialise_aliases_of("x").unwrap();
assert_eq!(n, 2);
assert!(!doc.to_string().contains('*'));
Source

pub fn rename_anchor(&mut self, old: &str, new: &str) -> Result<usize>

Rename every &old anchor declaration and every *old alias reference to new in one atomic pass. Returns the total number of touched sites (anchors + aliases).

Splices run in reverse source order so each successive splice’s offsets stay valid for earlier sites. The whole rename is byte-faithful outside the touched marks — comments, blank lines, and sibling formatting survive verbatim.

§Errors
  • new is empty or contains characters that would not be accepted as a YAML anchor name (any of the flow indicators ,[]{} or whitespace per YAML 1.2 §6.9.2).
  • old does not match any anchor or alias in the document (so the call is a no-op the user probably did not intend) — surfaced as an error rather than a silent zero-count.
  • The same parse-after-edit errors as crate::cst::Document::replace_span for any individual splice. The first failing splice aborts the batch; already-renamed sites stay renamed, so callers should treat a partial-failure error as a recoverable state and inspect the document.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document(
    "defaults: &cfg\n  port: 8080\nservice:\n  <<: *cfg\nbackup: *cfg\n",
).unwrap();

// Rename `cfg` → `defaults`. The single `&cfg` declaration
// and both `*cfg` references are updated in one call.
let n = doc.rename_anchor("cfg", "defaults").unwrap();
assert_eq!(n, 3); // 1 anchor + 2 aliases
let out = doc.to_string();
assert!(!out.contains("&cfg"));
assert!(!out.contains("*cfg"));
assert!(out.contains("&defaults"));
assert!(out.contains("*defaults"));
Source§

impl Document

Source

pub fn comments_at(&self, path: &str) -> CommentBundle

Comments decorating the node at path, classified by position.

Returns an empty CommentBundle when path does not resolve. Path syntax matches Document::span_atfoo.bar, items[0], items[0].name. Wildcard / recursive-descent segments are not supported (a non-singular span has no canonical “above” line).

§Examples
use noyalib::cst::parse_document;

let src = "# A multi-line\n# leading block\nport: 8080  # inline\n";
let doc = parse_document(src).unwrap();

let b = doc.comments_at("port");
assert_eq!(b.before.len(), 2);
assert_eq!(b.inline.as_ref().unwrap().text, " inline");
Source§

impl Document

Source

pub fn syntax(&self) -> &GreenNode

Borrow the root GreenNode.

§Examples
use noyalib::cst::{parse_document, SyntaxKind};

let doc = parse_document("foo: 1\n").unwrap();
assert_eq!(doc.syntax().kind(), SyntaxKind::Document);
Source

pub fn as_value(&self) -> Ref<'_, Value>

Borrow the typed Value view of the document.

On the first call after an edit (or a fresh parse), this triggers a one-shot parse of the current source into the internal Value / SpanTree cache. Subsequent calls on the same document are O(1) until the next edit invalidates the cache. Code that batches many edits without reading the typed view in between never pays the typed-tree cost.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document("name: noyalib\n").unwrap();
assert_eq!(doc.as_value()["name"].as_str(), Some("noyalib"));
Source

pub fn source(&self) -> &str

The original source bytes for this document. After an edit reflects the current source.

§Examples
use noyalib::cst::parse_document;

let src = "key: 1\n";
let doc = parse_document(src).unwrap();
assert_eq!(doc.source(), src);
Source

pub fn span_at(&self, path: &str) -> Option<(usize, usize)>

Resolve a path to the byte range of the value at that path, if any.

Path syntax matches the rest of the crate (foo.bar, items[0], items[0].name). Wildcard / recursive-descent segments are not supported here — they have no single span.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document("name: noyalib\nversion: 0.0.1\n").unwrap();
let (s, e) = doc.span_at("version").unwrap();
assert_eq!(&doc.source()[s..e], "0.0.1");
Source

pub fn validate(&self) -> Result<()>

Verify that the current source re-parses cleanly.

Document::set (and the rest of the path-shaped edit API) uses a localised-repair fast path that gates each splice on the fragment’s own scanner-level validation but commits optimistically: a structurally invalid splice across the whole document — for example, a value like [ that opens a flow collection never closed at end-of-input — passes the fragment check and only surfaces when the typed view is next read. as_value, span_at, get, and any path-shaped API panic on first access in that state.

validate is the non-panicking eager check: call it after an edit (or before handing the document to a downstream consumer) to surface any document-level parse error as a regular Result. On success, the typed cache is populated as a side-effect so a subsequent as_value call is free.

§Errors

Returns the underlying parse error if the source no longer parses as a single YAML document.

§Examples

Eagerly validate after an edit that may not be safe:

use noyalib::cst::parse_document;

let mut doc = parse_document("name: foo\n").unwrap();
// `[` opens a flow seq that is never closed — the local
// repair commits optimistically, but the document is now
// structurally broken. `validate` surfaces that as an
// error rather than waiting for the next typed-view read.
doc.set("name", "[").unwrap();
assert!(doc.validate().is_err());

Validate a freshly-parsed document — always succeeds:

use noyalib::cst::parse_document;

let doc = parse_document("name: foo\n").unwrap();
assert!(doc.validate().is_ok());
Source

pub fn get(&self, path: &str) -> Option<&str>

Return the source slice of the value at path.

§Examples
use noyalib::cst::parse_document;

let doc = parse_document("items:\n  - one\n  - two\n").unwrap();
assert_eq!(doc.get("items[1]"), Some("two"));
Source

pub fn replace_span( &mut self, start: usize, end: usize, replacement: &str, ) -> Result<()>

Replace the bytes in start..end with replacement and re-parse. The caller is responsible for replacement being a syntactically valid fragment in that position; if the spliced source fails to parse, the original document is left unchanged and the parse error is returned.

§Errors
  • Error::Parse if the resulting source is not valid YAML.
  • Error::Parse if start..end is out of bounds or not a character boundary.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("a: 1\n").unwrap();
let (s, e) = doc.span_at("a").unwrap();
doc.replace_span(s, e, "42").unwrap();
assert_eq!(doc.to_string(), "a: 42\n");
Source

pub fn last_repair_scope(&self) -> Option<RepairScope>

Last successful repair scope, if any. Useful for tests and instrumentation; returns None for a freshly-parsed document or when the most recent edit fell back to a full re-parse.

Source

pub fn set(&mut self, path: &str, fragment: &str) -> Result<()>

Replace the value at path with fragment.

fragment is spliced verbatim into the source — the caller supplies the YAML representation. This deliberately matches no scalar style automatically; choose double-quoted, plain, or block style to suit. Auto-formatting (the Emit trait from the design doc) is a follow-up.

§Errors
  • Error::Parse(...) with “path not found” if path does not resolve in the current document.
  • The same errors as Document::replace_span otherwise.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("name: foo\nversion: 0.0.1\n").unwrap();
doc.set("version", "0.0.2").unwrap();
assert_eq!(doc.to_string(), "name: foo\nversion: 0.0.2\n");
Source

pub fn set_value(&mut self, path: &str, value: &Value) -> Result<()>

Replace the value at path with a typed Value, formatting the YAML fragment to match the existing scalar style at the target site.

Style matching:

  • PlainScalar — emit plain when safe, double-quoted otherwise.
  • SingleQuotedScalar — wrap in '…' (only string values).
  • DoubleQuotedScalar — wrap in "…" with standard escapes (only string values).
  • LiteralScalar / FoldedScalar — currently rejected; block scalar formatting is a follow-up.

Non-string values (numbers, booleans, null) are emitted plain regardless of the existing style — quoting them would change the parsed type round-trip.

§Errors
  • Path not found.
  • Target is a collection or block scalar.
  • Caller passed a Sequence / Mapping (use set with a pre-formatted fragment for those — set_value is scalar-only for now).
  • The same errors as Document::replace_span otherwise.
§Examples
use noyalib::cst::parse_document;
use noyalib::Value;

let mut doc = parse_document("name: noyalib\nversion: 0.0.1\n").unwrap();
doc.set_value("version", &Value::String("0.0.2".into())).unwrap();
assert_eq!(doc.to_string(), "name: noyalib\nversion: 0.0.2\n");
Source

pub fn remove(&mut self, path: &str) -> Result<()>

Remove the value at path along with its surrounding entry (key + colon for mappings, - indicator for sequences). Trailing whitespace and the line break are removed too so the surrounding entries close up with no orphan blank line.

Restrictions in this phase:

  • Block context only — flow-collection entry removal ([a, b, c][a, c]) is a follow-up.
  • The value must end on the line where its key / - indicator appears (single-line scalars). Multi-line values and nested collections are deferred to the same follow-up that handles block-scalar replacement in set_value.
  • Removing the only entry of a block mapping or sequence is rejected — the result would parse differently (an empty block becomes null), and the caller needs to express that intent explicitly.
§Errors
  • Path not found.
  • Restrictions above.
  • The same parse-after-edit errors as Document::replace_span; on failure the document is left unchanged.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("a: 1\nb: 2\nc: 3\n").unwrap();
doc.remove("b").unwrap();
assert_eq!(doc.to_string(), "a: 1\nc: 3\n");
Source

pub fn push_back(&mut self, path: &str, fragment: &str) -> Result<()>

Append a new item to the block sequence at path.

fragment is the YAML representation of the value — the - indicator and the surrounding indentation are synthesized from the existing items so the new line matches the file’s shape. Block sequences only in this phase; flow sequences ([…]) and empty sequences are rejected.

§Errors
  • path does not resolve to a sequence.
  • The sequence is a flow collection ([…]).
  • The sequence has no existing items to anchor indentation on.
  • The same parse-after-edit errors as Document::replace_span.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("items:\n  - one\n  - two\n").unwrap();
doc.push_back("items", "three").unwrap();
assert_eq!(doc.to_string(), "items:\n  - one\n  - two\n  - three\n");
Source

pub fn indent_unit(&self) -> usize

Detect the indentation unit (in spaces) used by this document.

Walks the source line-by-line, looks for any pair of consecutive non-empty/non-comment lines where the second is more deeply indented than the first, and returns the smallest such delta — that is the file’s “indent step”, typically 2 or 4 spaces. A document with no nested structure (or only top-level keys) has no detectable step; the default 2 is returned in that case.

Used internally by the crate::cst::Entry insertion paths to keep the inserted YAML’s inner indentation consistent with what the rest of the file already uses (2-space file → 2-space inserts; 4-space file → 4-space inserts). Exposed publicly so callers building their own emission paths can match the same convention.

§Examples
use noyalib::cst::parse_document;

let two_space = parse_document(
    "metadata:\n  labels:\n    app: noyalib\n",
).unwrap();
assert_eq!(two_space.indent_unit(), 2);

let four_space = parse_document(
    "metadata:\n    labels:\n        app: noyalib\n",
).unwrap();
assert_eq!(four_space.indent_unit(), 4);

// No nested structure — defaults to 2.
let flat = parse_document("a: 1\nb: 2\n").unwrap();
assert_eq!(flat.indent_unit(), 2);
Source

pub fn dominant_quote_style(&self) -> ScalarStyle

Inspect the document and return the dominant scalar quote style — Plain, SingleQuoted, or DoubleQuoted. Used by the crate::cst::Entry insert helpers to make new scalars adopt the file’s existing convention rather than the serializer’s hard-coded default.

The detection scans every plain / single-quoted / double-quoted scalar leaf in the green tree, picks the majority, and breaks ties in favour of the simpler form (Plain > SingleQuoted > DoubleQuoted). Empty documents and documents with no string-shaped scalars default to Plain.

§Examples
use noyalib::cst::parse_document;
use noyalib::ScalarStyle;

let single = parse_document("a: 'one'\nb: 'two'\n").unwrap();
assert_eq!(single.dominant_quote_style(), ScalarStyle::SingleQuoted);

let double = parse_document("a: \"one\"\nb: \"two\"\n").unwrap();
assert_eq!(double.dominant_quote_style(), ScalarStyle::DoubleQuoted);

let plain = parse_document("a: one\nb: two\n").unwrap();
assert_eq!(plain.dominant_quote_style(), ScalarStyle::Plain);
Source

pub fn dominant_flow_style(&self) -> FlowStyle

Inspect the document and return the dominant collection style — FlowStyle::Block or FlowStyle::Auto (equivalent to “flow”). Used by Entry::insert_value to decide whether a typed mapping / sequence emission should use block or flow form.

The detection counts top-level BlockMapping / BlockSequence vs FlowMapping / FlowSequence leaves and picks the majority. Empty / scalar-only documents default to Block.

§Examples
use noyalib::cst::parse_document;
use noyalib::FlowStyle;

let block = parse_document("a:\n  - 1\n  - 2\n").unwrap();
assert_eq!(block.dominant_flow_style(), FlowStyle::Block);

let flow = parse_document("a: [1, 2, 3]\nb: [4, 5]\n").unwrap();
assert_eq!(flow.dominant_flow_style(), FlowStyle::Auto);
Source

pub fn insert_entry( &mut self, mapping_path: &str, key: &str, fragment: &str, ) -> Result<()>

Insert a new key: fragment entry into the block mapping at mapping_path. The mapping-side analogue of Document::push_back.

Behaves like set when the key already exists (the value is replaced losslessly). When the key is new, a sibling line is spliced after the last existing entry, with the indent matched to the last entry’s key column so the file stays canonical. Block mappings only in this phase; flow mappings ({…}) and empty mappings are rejected.

§Errors
  • mapping_path does not resolve to a mapping.
  • The mapping is empty (no anchor for indentation; use set with a fragment instead).
  • The same parse-after-edit errors as Document::replace_span.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document(
    "metadata:\n  labels:\n    app: noyalib\n",
).unwrap();
doc.insert_entry("metadata.labels", "env", "prod").unwrap();
let out = doc.to_string();
assert!(out.contains("app: noyalib"));
assert!(out.contains("env: prod"));
Source

pub fn insert_after(&mut self, item_path: &str, fragment: &str) -> Result<()>

Insert a new sequence item immediately after the item at item_path (e.g. "items[1]").

fragment is the YAML representation of the value; the - indicator and indentation are derived from the item at item_path.

§Errors
  • item_path does not end in an index.
  • The path does not resolve to a sequence item in a block sequence.
  • The same parse-after-edit errors as Document::replace_span.
§Examples
use noyalib::cst::parse_document;

let mut doc = parse_document("items:\n  - one\n  - three\n").unwrap();
doc.insert_after("items[0]", "two").unwrap();
assert_eq!(
    doc.to_string(),
    "items:\n  - one\n  - two\n  - three\n",
);
Source§

impl Document

Source

pub fn entry<'a>(&'a mut self, path: &str) -> Entry<'a>

Return a path-shaped mutable handle to the node at path.

The handle is the “pro” mutation interface — chainable, composable, ergonomic for nested edits — that complements the functional Document::set / Document::remove / Document::push_back / Document::insert_after methods (all of which remain available for direct one-shot operations).

entry itself is infallible — the path is recorded but not resolved at this point. Operations on the returned entry surface path-resolution and splice-failure errors via their own Result.

§Examples
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"));

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
Source§

impl Display for Document

Source§

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

Re-emit the document. For any input that parses successfully, the result equals the original bytes verbatim. Display drives Document::to_string() via the standard ToString blanket impl.

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

Source§

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

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Fmt for T
where T: Display,

Source§

fn fg<C>(self, color: C) -> Foreground<Self>
where C: Into<Option<Color>>, Self: Display,

Give this value the specified foreground colour.
Source§

fn bg<C>(self, color: C) -> Background<Self>
where C: Into<Option<Color>>, Self: Display,

Give this value the specified background colour.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> MaybeSendSync for T

Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

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

Source§

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();
Source§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
Source§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
Source§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
Source§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
Source§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
Source§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
Source§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
Source§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
Source§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
Source§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
Source§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
Source§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
Source§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
Source§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
Source§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
Source§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
Source§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
Source§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
Source§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
Source§

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();
Source§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
Source§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
Source§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
Source§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
Source§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
Source§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
Source§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
Source§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
Source§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
Source§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
Source§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
Source§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
Source§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
Source§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
Source§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
Source§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
Source§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
Source§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
Source§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
Source§

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();
Source§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
Source§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
Source§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
Source§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
Source§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
Source§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
Source§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
Source§

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();
Source§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
Source§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
Source§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
Source§

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.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
Source§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
Source§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
Source§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
Source§

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);
Source§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new Painted with a default Style. Read more
Source§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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> ValidateIp for T
where T: ToString,

Source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
Source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
Source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP