pub struct ScalarValue { /* private fields */ }Expand description
A scalar value with metadata about its style and content
Implementations§
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 parse_escape_sequences(text: &str) -> String
pub fn parse_escape_sequences(text: &str) -> String
Parse escape sequences in a double-quoted string
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 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.
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: true, false, yes, no, on, off (case-insensitive).
Sourcepub fn as_binary(&self) -> Option<Result<Vec<u8>, String>>
pub fn as_binary(&self) -> Option<Result<Vec<u8>, String>>
Extract binary data if this is a binary scalar
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 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
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.
Sourcepub fn to_yaml_string(&self) -> String
pub fn to_yaml_string(&self) -> String
Render the scalar as a YAML string with proper escaping
Sourcepub fn to_literal_with_indent(&self, indent: usize) -> String
pub fn to_literal_with_indent(&self, indent: usize) -> String
Convert to literal block scalar with specific indentation
Sourcepub fn to_folded_with_indent(&self, indent: usize) -> String
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
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§fn clone(&self) -> ScalarValue
fn clone(&self) -> ScalarValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more