pub struct ScalarValue { /* private fields */ }Expand description
A scalar value with metadata about its style and content
Implementations§
Source§impl ScalarValue
impl ScalarValue
Source§impl ScalarValue
impl ScalarValue
Sourcepub fn parse_escape_sequences(text: &str) -> String
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
impl ScalarValue
Sourcepub fn to_yaml_string(&self) -> String
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.
Sourcepub fn to_yaml_string_with_indent(
&self,
parent_column: usize,
indent: usize,
) -> String
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).
Sourcepub fn to_literal_with_indent(
&self,
parent_column: usize,
indent: usize,
) -> String
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
Sourcepub fn to_folded_with_indent(
&self,
parent_column: usize,
indent: usize,
) -> String
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
Source§impl ScalarValue
impl ScalarValue
Sourcepub fn classify_plain(value: &str) -> CoreScalarType
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.
Sourcepub fn auto_detect_type(value: &str) -> ScalarType
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
impl ScalarValue
Sourcepub fn string(value: impl Into<String>) -> Self
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.
Sourcepub fn with_style(value: impl Into<String>, style: ScalarStyle) -> Self
pub fn with_style(value: impl Into<String>, style: ScalarStyle) -> Self
Create a new scalar with a specific style
Sourcepub fn single_quoted(value: impl Into<String>) -> Self
pub fn single_quoted(value: impl Into<String>) -> Self
Create a single-quoted scalar
Sourcepub fn double_quoted(value: impl Into<String>) -> Self
pub fn double_quoted(value: impl Into<String>) -> Self
Create a double-quoted scalar
Sourcepub fn with_tag(self, tag: impl Into<String>) -> Self
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");Sourcepub fn style(&self) -> ScalarStyle
pub fn style(&self) -> ScalarStyle
Get the style
Sourcepub fn scalar_type(&self) -> ScalarType
pub fn scalar_type(&self) -> ScalarType
Get the scalar type
Sourcepub fn to_i64(&self) -> Option<i64>
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.
Sourcepub fn to_i64_yaml_1_2(&self) -> Option<i64>
👎Deprecated since 0.3.3: to_i64 follows YAML 1.2; use it instead
pub fn to_i64_yaml_1_2(&self) -> Option<i64>
to_i64 follows YAML 1.2; use it instead
Same as to_i64, which now follows YAML 1.2.
Sourcepub fn to_f64(&self) -> Option<f64>
pub fn to_f64(&self) -> Option<f64>
Try to parse this scalar as an f64.
Returns None if the scalar type is not Float.
Sourcepub fn to_bool(&self) -> Option<bool>
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.
Sourcepub fn is_timestamp(&self) -> bool
pub fn is_timestamp(&self) -> bool
Check if this is a timestamp scalar
Sourcepub fn coerce_to_type(&self, target_type: ScalarType) -> Option<ScalarValue>
pub fn coerce_to_type(&self, target_type: ScalarType) -> Option<ScalarValue>
Try to coerce this scalar to the specified type
Sourcepub fn parse(value: impl Into<String>) -> Self
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.
Sourcepub fn from_scalar(scalar: &Scalar) -> Self
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
impl AsYaml for ScalarValue
Source§fn as_node(&self) -> Option<&SyntaxNode<Lang>>
fn as_node(&self) -> Option<&SyntaxNode<Lang>>
SyntaxNode if one exists. Read moreSource§fn build_content(
&self,
builder: &mut GreenNodeBuilder<'_>,
indent: usize,
flow_context: bool,
) -> bool
fn build_content( &self, builder: &mut GreenNodeBuilder<'_>, indent: usize, flow_context: bool, ) -> bool
GreenNodeBuilder. Read moreSource§impl Clone for ScalarValue
impl Clone for ScalarValue
Source§impl Debug for ScalarValue
impl Debug for ScalarValue
Source§impl Display for ScalarValue
impl Display for ScalarValue
impl Eq for ScalarValue
Source§impl From<&str> for ScalarValue
impl From<&str> for ScalarValue
Source§impl From<Scalar> for ScalarValue
Same as ScalarValue::from_scalar.
impl From<Scalar> for ScalarValue
Same as ScalarValue::from_scalar.