Crate serde_yaml
source ·Expand description
This crate is a Rust library for using the Serde serialization framework with data in YAML file format.
This library does not reimplement a YAML parser; it uses yaml-rust which is a pure Rust YAML 1.2 implementation.
Examples
use std::collections::BTreeMap;
fn main() -> Result<(), serde_yaml::Error> {
// You have some type.
let mut map = BTreeMap::new();
map.insert("x".to_string(), 1.0);
map.insert("y".to_string(), 2.0);
// Serialize it to a YAML string.
let s = serde_yaml::to_string(&map)?;
assert_eq!(s, "---\nx: 1.0\ny: 2.0\n");
// Deserialize it back to a Rust type.
let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&s)?;
assert_eq!(map, deserialized_map);
Ok(())
}
Using Serde derive
It can also be used with Serde’s serialization code generator serde_derive
to
handle structs and enums defined in your own program.
use serde::{Serialize, Deserialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
fn main() -> Result<(), serde_yaml::Error> {
let point = Point { x: 1.0, y: 2.0 };
let s = serde_yaml::to_string(&point)?;
assert_eq!(s, "---\nx: 1.0\ny: 2.0\n");
let deserialized_point: Point = serde_yaml::from_str(&s)?;
assert_eq!(point, deserialized_point);
Ok(())
}
Modules
- A YAML mapping and its iterator types.
- Entry points for deserializing with pre-existing state.
Structs
- A structure that deserializes YAML into Rust values.
- An error that happened serializing or deserializing YAML data.
- The input location that an error occured.
- A YAML mapping in which the keys and values are both
serde_yaml::Value
. - Represents a YAML number, whether integer or floating point.
- A structure for serializing Rust values into YAML.
Enums
- Represents any valid YAML value.
Traits
- A type that can be used to index into a
serde_yaml::Value
. See theget
andget_mut
methods ofValue
.
Functions
- Deserialize an instance of type
T
from an IO stream of YAML. - Deserialize an instance of type
T
from bytes of YAML text. - Deserialize an instance of type
T
from a string of YAML text. - Interpret a
serde_yaml::Value
as an instance of typeT
. - Serialize the given data structure as a String of YAML.
- Convert a
T
intoserde_yaml::Value
which is an enum that can represent any valid YAML data. - Serialize the given data structure as a YAML byte vector.
- Serialize the given data structure as YAML into the IO stream.
Type Definitions
- Alias for a
Result
with the error typeserde_yaml::Error
. - A YAML sequence in which the elements are
serde_yaml::Value
.