yaml_peg/serde/mod.rs
1//! The implementation of serialization. The technique is come from [`serde`].
2//!
3//! Here is an example for converting YAML data into a custom structure.
4//!
5//! ```
6//! use serde::Deserialize;
7//! use yaml_peg::node;
8//!
9//! #[derive(Deserialize)]
10//! struct Member {
11//! name: String,
12//! married: bool,
13//! age: u8,
14//! }
15//!
16//! let n = node!({
17//! "name" => "Bob",
18//! "married" => true,
19//! "age" => 46,
20//! });
21//! let officer = Member::deserialize(n).unwrap();
22//! assert_eq!("Bob", officer.name);
23//! assert!(officer.married);
24//! assert_eq!(46, officer.age);
25//! ```
26//!
27//! At least you should enable the `serde/derive` and `serde/alloc` features to
28//! run the example. The `serde/derive` feature provides derive macro for the
29//! custom data, and if `serde/alloc` is not used, you cannot deserialize
30//! [`alloc::string::String`] or [`alloc::vec::Vec`] type.
31//!
32//! For converting custom data into YAML data, please see [`to_node`] and
33//! [`to_arc_node`], and if you went to parse / dump YAML document, use
34//! [`from_str`] and [`to_string`].
35//!
36//! # Anchors
37//!
38//! [`crate::Yaml::Alias`] is not support serialization.
39//! Please using direct parsing function [`crate::parse`] to avoid the alias
40//! node.
41//!
42//! Cyclic data should be handled manually.
43//!
44//! # Mixed String Type
45//!
46//! If the data needs to deserialized from any type into string, please see
47//! [`Stringify`] type.
48//!
49//! # Mixed Listed Map
50//!
51//! If the data supports listed items but allows single mapped item, please see
52//! [`InlineList`] type.
53//!
54//! # Error
55//!
56//! The error message will provide the position of the node.
57//!
58//! Please see [`SerdeError`] for more information.
59//!
60//! ```
61//! use serde::Deserialize;
62//! use yaml_peg::serde::from_str;
63//!
64//! #[derive(Deserialize)]
65//! struct Member {
66//! name: String,
67//! married: bool,
68//! age: u8,
69//! }
70//!
71//! let yaml = "
72//! name: Bob
73//! married: 84
74//! age: 46
75//! ";
76//! let err = from_str::<Member>(yaml).err().unwrap();
77//! assert_eq!("invalid type: integer `84`, expected a boolean", err.msg);
78//! assert_eq!(20, err.pos);
79//! ```
80pub use self::{de::*, error::*, inline_list::*, optional::*, ser::*, stringify::*};
81
82mod de;
83mod error;
84mod inline_list;
85mod optional;
86mod ser;
87mod ser_node;
88mod stringify;