use serde_yml::libyml::emitter::{
Emitter, Event, Mapping, Scalar, ScalarStyle, Sequence,
};
use std::io::Cursor;
pub(crate) fn main() {
println!("\n❯ Executing examples/libyml/emitter_examples.rs");
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted stream start and end: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted document with scalar: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted sequence: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted mapping: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.flush().unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted and flushed: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: Some("!mytag".to_string()),
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted scalar with tag: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence {
tag: Some("!mytag".to_string()),
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted sequence with tag: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping {
tag: Some("!mytag".to_string()),
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted mapping with tag: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted empty sequence: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted empty mapping: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted nested sequence: {}", output);
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested_key",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested_value",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted nested mapping: {}", output);
}