1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! `serde_magnus` converts between Rust and Ruby data structures using [Serde] and [Magnus].
//!
//! [Serde]: https://github.com/serde-rs/serde
//! [Magnus]: https://github.com/matsadler/magnus
//!
//! The [`serialize`] function converts from a Rust type implementing the [`serde::Serialize`]
//! trait into a Ruby equivalent.
//!
//! ```
//! # let _cleanup = unsafe { magnus::embed::init() };
//! #
//! use serde::{Serialize, Deserialize};
//! use magnus::{eval, Value};
//! use serde_magnus::serialize;
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct Post {
//! title: String,
//! content: String,
//! author: Author,
//! tags: Vec<String>
//! }
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct Author {
//! name: String,
//! email_address: String
//! }
//!
//! let post = Post {
//! title: "Spring carnival planning update".into(),
//! content: "Here's what's new.".into(),
//! author: Author {
//! name: "Martha".into(),
//! email_address: "martha@example.com".into()
//! },
//! tags: vec![
//! "carnival".into(),
//! "update".into()
//! ]
//! };
//!
//! let post: Value = serialize(&post)?;
//!
//! assert!(eval!(
//! r#"
//! post == {
//! title: "Spring carnival planning update",
//! content: "Here's what's new.",
//! author: {
//! name: "Martha",
//! email_address: "martha@example.com"
//! },
//! tags: ["carnival", "update"]
//! }
//! "#,
//! post
//! )?);
//! #
//! # Ok::<(), magnus::Error>(())
//! ```
//!
//! [`deserialize`] converts from a Ruby value to a Rust type implementing [`serde::Deserialize`].
//!
//! ```
//! # use serde::Deserialize;
//! # use magnus::{eval, RHash};
//! #
//! # let _cleanup = unsafe { magnus::embed::init() };
//! #
//! # #[derive(Deserialize, PartialEq, Debug)]
//! # struct Post {
//! # title: String,
//! # content: String,
//! # author: Author,
//! # tags: Vec<String>
//! # }
//! #
//! # #[derive(Deserialize, PartialEq, Debug)]
//! # struct Author {
//! # name: String,
//! # email_address: String
//! # }
//! #
//! use serde_magnus::deserialize;
//!
//! let post: RHash = eval!(r#"
//! {
//! title: "Spring carnival planning update",
//! content: "Here's what's new.",
//! author: {
//! name: "Martha",
//! email_address: "martha@example.com"
//! },
//! tags: ["carnival", "update"]
//! }
//! "#)?;
//!
//! let post: Post = deserialize(post)?;
//!
//! assert_eq!(
//! Post {
//! title: "Spring carnival planning update".into(),
//! content: "Here's what's new.".into(),
//! author: Author {
//! name: "Martha".into(),
//! email_address: "martha@example.com".into()
//! },
//! tags: vec![
//! "carnival".into(),
//! "update".into()
//! ]
//! },
//! post
//! );
//! #
//! # Ok::<(), magnus::Error>(())
//! ```
pub use deserialize;
pub use serialize;