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 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 parse_escape_sequences(text: &str) -> String

Parse escape sequences in a double-quoted string

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 binary(data: &[u8]) -> Self

Create a binary scalar from raw bytes

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

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: true, false, yes, no, on, off (case-insensitive).

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

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 auto_detect_type(value: &str) -> ScalarType

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

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.

Source

pub fn to_yaml_string(&self) -> String

Render the scalar as a YAML string with proper escaping

Source

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

Convert to literal block scalar with specific indentation

Source

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

Convert to folded block scalar with specific indentation

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

Returns a duplicate of the value. Read more
1.0.0 · 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 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<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: &ScalarValue) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ScalarValue

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