Expand description
§Serde YML (a fork of Serde YAML)
Serde YML is a Rust library for using the Serde serialization framework with data in YAML file format.
§Features
- Serialization and deserialization of Rust data structures to/from YAML format
- Support for custom structs and enums using Serde’s derive macros
- Handling of YAML’s
!tag
syntax for representing enum variants - Direct access to YAML values through the
Value
type and related types likeMapping
andSequence
- Comprehensive error handling with
Error
,Location
, andResult
types - Serialization to YAML using
to_string
andto_writer
functions - Deserialization from YAML using
from_str
,from_slice
, andfrom_reader
functions - Customizable serialization and deserialization behavior using Serde’s
#[serde(with = ...)]
attribute - Support for serializing/deserializing enums using a YAML map with a single key-value pair through the
singleton_map
module - Recursive application of
singleton_map
serialization/deserialization to all enums within a data structure using thesingleton_map_recursive
module - Serialization and deserialization of optional enum fields using the
singleton_map_optional
module - Handling of nested enum structures with optional inner enums using the
singleton_map_recursive
module - Customization of serialization and deserialization logic for enums using the
singleton_map_with
module and custom helper functions
§Installation
Add this to your Cargo.toml
:
[dependencies]
serde = "1.0"
serde_yml = "0.0.12"
§Usage
Here’s a quick example on how to use Serde YML to serialize and deserialize a struct to and from YAML:
use serde::{Serialize, Deserialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
fn main() -> Result<(), serde_yml::Error> {
let point = Point { x: 1.0, y: 2.0 };
// Serialize to YAML
let yaml = serde_yml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");
// Deserialize from YAML
let deserialized_point: Point = serde_yml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Ok(())
}
§Documentation
For full API documentation, please visit https://docs.rs/serde-yml.
§Rust Version Compatibility
Compiler support: requires rustc 1.56.0+
§Examples
Serde YML provides a set of comprehensive examples to demonstrate its usage and capabilities. You can find them in the examples
directory of the project.
To run the examples, clone the repository and execute the following command in your terminal from the project root directory:
cargo run --example example
The examples cover various scenarios, including serializing and deserializing structs, enums, optional fields, custom structs, and more.
Re-exports§
pub use crate::de::from_reader;
pub use crate::de::from_slice;
pub use crate::de::from_str;
pub use crate::de::Deserializer;
pub use crate::modules::error::Error;
pub use crate::modules::error::Location;
pub use crate::modules::error::Result;
pub use crate::ser::to_string;
pub use crate::ser::to_writer;
pub use crate::ser::Serializer;
pub use crate::ser::State;
Modules§
- The
de
module contains the library’s YAML deserializer. - The
libyml
module contains the library’s YAML parser and emitter. - The
loader
module contains theLoader
type for YAML loading. - The
mapping
module contains theMapping
type for YAML mappings. A YAML mapping and its iterator types. - The
modules
module contains the library’s modules. - The
number
module contains theNumber
type for YAML numbers. - The
ser
module contains the library’s YAML serializer. YAML Serialization - The
value
module contains theValue
type for YAML values. The Value enum, a loosely typed way of representing any valid YAML value. - The
with
module contains theWith
type for YAML values. Customizations to use with Serde’s#[serde(with = …)]
attribute.
Structs§
- A YAML mapping in which the keys and values are both
serde_yml::Value
. - Represents a YAML number, whether integer or floating point.
Enums§
- Represents any valid YAML value.
Traits§
- A type that can be used to index into a
serde_yml::Value
. See theget
andget_mut
methods ofValue
.
Functions§
- Interpret a
serde_yml::Value
as an instance of typeT
. - Converts a serializable value into a
serde_yml::Value
.
Type Aliases§
- A YAML sequence in which the elements are
serde_yml::Value
.