Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Add multi-doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 2, 2021
1 parent cfe758b commit 5b91abf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = "1.0.69"
yaml-rust = "0.4.5"

[dev-dependencies]
anyhow = "1.0"
indoc = "1.0"
serde_derive = "1.0"

Expand Down
37 changes: 37 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ use yaml_rust::parser::{Event as YamlEvent, MarkedEventReceiver, Parser};
use yaml_rust::scanner::{Marker, TScalarStyle, TokenType};

/// A structure that deserializes YAML into Rust values.
///
/// # Examples
///
/// Deserializing a single document:
///
/// ```
/// use anyhow::Result;
/// use serde::Deserialize;
/// use serde_yaml::Value;
///
/// fn main() -> Result<()> {
/// let input = "---\nk: 107\n";
/// let de = serde_yaml::Deserializer::from_str(input);
/// let value = Value::deserialize(de)?;
/// println!("{:?}", value);
/// Ok(())
/// }
/// ```
///
/// Deserializing multi-doc YAML:
///
/// ```
/// use anyhow::Result;
/// use serde::Deserialize;
/// use serde_yaml::Value;
///
/// fn main() -> Result<()> {
/// let input = "---\nk: 107\n...\n---\nj: 106\n";
///
/// for document in serde_yaml::Deserializer::from_str(input) {
/// let value = Value::deserialize(document)?;
/// println!("{:?}", value);
/// }
///
/// Ok(())
/// }
/// ```
pub struct Deserializer<'a> {
input: Input<'a>,
}
Expand Down
23 changes: 23 additions & 0 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ use std::{fmt, io, num, str};
use yaml_rust::{yaml, Yaml, YamlEmitter};

/// A structure for serializing Rust values into YAML.
///
/// # Example
///
/// ```
/// use anyhow::Result;
/// use serde::Serialize;
/// use std::collections::BTreeMap;
///
/// fn main() -> Result<()> {
/// let mut buffer = Vec::new();
/// let mut ser = serde_yaml::Serializer::new(&mut buffer);
///
/// let mut object = BTreeMap::new();
/// object.insert("k", 107);
/// object.serialize(&mut ser)?;
///
/// object.insert("j", 106);
/// object.serialize(&mut ser)?;
///
/// assert_eq!(buffer, b"---\nk: 107\n...\n---\nj: 106\nk: 107\n");
/// Ok(())
/// }
/// ```
pub struct Serializer<W> {
documents: usize,
writer: W,
Expand Down

0 comments on commit 5b91abf

Please sign in to comment.