Skip to main content

ScalarValue

Struct ScalarValue 

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

A scalar value with metadata about its style and content

Implementations§

Source§

impl ScalarValue

Source

pub fn binary(data: &[u8]) -> Self

Create a binary scalar from raw bytes

Source

pub fn as_binary(&self) -> Option<Result<Vec<u8>, String>>

Extract binary data if this is a binary scalar

Source

pub fn is_binary(&self) -> bool

Check if this is a binary scalar

Source§

impl ScalarValue

Source

pub fn parse_escape_sequences(text: &str) -> String

Decode the content of a double-quoted string: process escape sequences and fold line breaks as described in the YAML spec.

Source§

impl ScalarValue

Source

pub fn to_yaml_string(&self) -> String

Render the scalar as a YAML string with proper escaping.

A block scalar is laid out for a top-level entry, with its body indented two spaces. Use to_yaml_string_with_indent for a value nested deeper.

Source

pub fn to_yaml_string_with_indent( &self, parent_column: usize, indent: usize, ) -> String

As to_yaml_string, for a value of the collection whose entries sit at parent_column, with a block scalar’s body indent spaces further right.

parent_column is the column of the entry’s key, or of the - of a sequence entry, and 0 for a top-level entry or the document root. It matters because a block scalar’s indentation indicator (|2) counts from there: for a:\n k: <value> pass 2.

§Panics

For a block scalar, if indent is 0, or above 9 when the value needs an indentation indicator (its first non-empty line starts with a space).

Source

pub fn to_literal_with_indent( &self, parent_column: usize, indent: usize, ) -> String

Render as a literal block scalar (|) whose body sits indent spaces past parent_column; see to_yaml_string_with_indent. A value a block scalar can’t hold, such as one with a carriage return, is double-quoted instead.

§Panics

As to_yaml_string_with_indent.

Source

pub fn to_folded_with_indent( &self, parent_column: usize, indent: usize, ) -> String

Render as a folded block scalar (>) whose body sits indent spaces past parent_column; see to_yaml_string_with_indent. A value a block scalar can’t hold, such as one with a carriage return, is double-quoted instead.

§Panics

As to_yaml_string_with_indent.

Source§

impl ScalarValue

Source

pub fn classify_plain(value: &str) -> CoreScalarType

Classify a plain (unquoted) scalar’s text per the YAML 1.2 core schema tag-resolution rules.

This is the single source of truth for what a plain scalar resolves to; the lexer and from_scalar both delegate here so token-emit-time and CST-inspection-time classification cannot drift.

auto_detect_type builds on this and additionally recognises timestamps.

Source

pub fn auto_detect_type(value: &str) -> ScalarType

Auto-detect the most appropriate scalar type from a string value

This follows classify_plain, except that text the core schema reads as a string is checked for a timestamp.

Source§

impl ScalarValue

Source

pub fn string(value: impl Into<String>) -> Self

Create a scalar value explicitly treating it as a string (no type auto-detection)

This method always creates a String type scalar. The value will be properly quoted if needed when rendering to YAML, but no type detection is performed.

§Examples
use yaml_edit::ScalarValue;

let scalar = ScalarValue::string("123");
assert_eq!(scalar.scalar_type(), yaml_edit::ScalarType::String);
// Renders with quotes to distinguish from integer
assert_eq!(scalar.to_yaml_string(), "'123'");

let scalar = ScalarValue::string("true");
assert_eq!(scalar.scalar_type(), yaml_edit::ScalarType::String);
// Renders with quotes to distinguish from boolean
assert_eq!(scalar.to_yaml_string(), "'true'");

let scalar = ScalarValue::string("hello");
assert_eq!(scalar.scalar_type(), yaml_edit::ScalarType::String);
// Plain strings don't need quotes
assert_eq!(scalar.to_yaml_string(), "hello");

For YAML-style type detection (parsing “123” as Integer, “true” as Boolean), use ScalarValue::parse() instead.

Source

pub fn with_style(value: impl Into<String>, style: ScalarStyle) -> Self

Create a new scalar with a specific style

Source

pub fn plain(value: impl Into<String>) -> Self

Create a plain scalar

Source

pub fn single_quoted(value: impl Into<String>) -> Self

Create a single-quoted scalar

Source

pub fn double_quoted(value: impl Into<String>) -> Self

Create a double-quoted scalar

Source

pub fn literal(value: impl Into<String>) -> Self

Create a literal scalar

Source

pub fn folded(value: impl Into<String>) -> Self

Create a folded scalar

Source

pub fn null() -> Self

Create a null scalar

Source

pub fn timestamp(value: impl Into<String>) -> Self

Create a timestamp scalar

Source

pub fn regex(pattern: impl Into<String>) -> Self

Create a regex scalar

Source

pub fn with_tag(self, tag: impl Into<String>) -> Self

Return this scalar with an explicit tag, such as !!str or !local.

The tag is rendered in front of the content. It does not change the scalar’s ScalarType.

use yaml_edit::ScalarValue;

let scalar = ScalarValue::from(1.0).with_tag("!!float");
assert_eq!(scalar.tag(), Some("!!float"));
assert_eq!(scalar.to_yaml_string(), "!!float 1.0");
Source

pub fn tag(&self) -> Option<&str>

The explicit tag on this scalar, if any.

Source

pub fn value(&self) -> &str

Get the raw value

Source

pub fn style(&self) -> ScalarStyle

Get the style

Source

pub fn scalar_type(&self) -> ScalarType

Get the scalar type

Source

pub fn to_i64(&self) -> Option<i64>

Try to parse this scalar as an i64.

Returns None if the scalar type is not Integer or the value does not fit in an i64. Integers are read as the YAML 1.2 core schema defines them, so 0755 is seven hundred and fifty five; only 0o755 is octal.

Source

pub fn to_i64_yaml_1_2(&self) -> Option<i64>

👎Deprecated since 0.3.3:

to_i64 follows YAML 1.2; use it instead

Same as to_i64, which now follows YAML 1.2.

Source

pub fn to_f64(&self) -> Option<f64>

Try to parse this scalar as an f64.

Returns None if the scalar type is not Float.

Source

pub fn to_bool(&self) -> Option<bool>

Try to parse this scalar as a bool.

Returns None if the scalar type is not Boolean. Recognizes the YAML 1.2 core schema spellings: true, True, TRUE, false, False and FALSE.

Source

pub fn is_timestamp(&self) -> bool

Check if this is a timestamp scalar

Source

pub fn is_regex(&self) -> bool

Check if this is a regex scalar

Source

pub fn coerce_to_type(&self, target_type: ScalarType) -> Option<ScalarValue>

Try to coerce this scalar to the specified type

Source

pub fn parse(value: impl Into<String>) -> Self

Parse a YAML scalar value with automatic type detection

This method automatically detects the YAML type based on the content:

  • “123” → Integer
  • “3.14” → Float
  • “true” / “false” → Boolean
  • “null” / “~” → Null
  • etc.
§Examples
use yaml_edit::{ScalarValue, ScalarType};

let scalar = ScalarValue::parse("123");
assert_eq!(scalar.scalar_type(), ScalarType::Integer);
assert_eq!(scalar.to_yaml_string(), "123");

let scalar = ScalarValue::parse("true");
assert_eq!(scalar.scalar_type(), ScalarType::Boolean);
assert_eq!(scalar.to_yaml_string(), "true");

let scalar = ScalarValue::parse("hello");
assert_eq!(scalar.scalar_type(), ScalarType::String);
assert_eq!(scalar.to_yaml_string(), "hello");

To create a String-type scalar without auto-detection (e.g., to represent the string “123” rather than the integer 123), use ScalarValue::string() instead.

Source

pub fn from_scalar(scalar: &Scalar) -> Self

Create a ScalarValue from a Scalar syntax node, preserving the type from the lexer

This extracts type information directly from the token kind (INT, BOOL, FLOAT, etc.) rather than guessing based on heuristics. This is the correct way to convert parsed YAML into ScalarValue.

Trait Implementations§

Source§

impl AsYaml for ScalarValue

Source§

fn as_node(&self) -> Option<&SyntaxNode<Lang>>

Returns a reference to the underlying SyntaxNode if one exists. Read more
Source§

fn kind(&self) -> YamlKind

Returns the kind of YAML value this represents.
Source§

fn build_content( &self, builder: &mut GreenNodeBuilder<'_>, indent: usize, flow_context: bool, ) -> bool

Serialize this value into a GreenNodeBuilder. Read more
Source§

fn is_inline(&self) -> bool

Returns whether this value should be rendered on the same line as its key. Read more
Source§

impl Clone for ScalarValue

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 ScalarValue

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for ScalarValue

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for ScalarValue

Source§

impl From<&str> for ScalarValue

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Scalar> for ScalarValue

Source§

fn from(scalar: Scalar) -> Self

Converts to this type from the input type.
Source§

impl From<ScalarValue> for YamlValue

Source§

fn from(value: ScalarValue) -> Self

Converts to this type from the input type.
Source§

impl From<String> for ScalarValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for ScalarValue

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for ScalarValue

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for ScalarValue

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for ScalarValue

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for ScalarValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ScalarValue

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ScalarValue

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
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> 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 = !

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.