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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2024 Lily Lyons
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
pub use Serializer;
use crate::;
/// An enum representing any ruby value.
///
/// Similar to `serde_json::Value`, although much more nuanced.
/// Interpret a `Value` as an instance of type `T`.
///
/// # Example
///
/// ```
/// use alox_48::Deserialize;
///
/// #[derive(Deserialize, Debug, PartialEq)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
///
/// let mut object = alox_48::Object { class: "User".into(), ..Default::default() };
/// object.fields.insert("fingerprint".into(), alox_48::RbString::from("0xF9BA143B95FF6D82").into());
/// object.fields.insert("location".into(), alox_48::RbString::from("Menlo Park, CA").into());
/// let value = alox_48::Value::Object(object);
///
/// let u: User = alox_48::from_value(&value).unwrap();
/// assert_eq!(u, User { fingerprint: "0xF9BA143B95FF6D82".to_string(), location: "Menlo Park, CA".to_string() });
///
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the Value does not match the structure of `T`.
/// Convert a `T` into `Value`.
///
/// # Example
///
/// ```
/// use alox_48::Serialize;
///
/// #[derive(Serialize, Debug, PartialEq)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
///
/// let mut object = alox_48::Object { class: "User".into(), ..Default::default() };
/// object.fields.insert("@fingerprint".into(), alox_48::Instance::from("0xF9BA143B95FF6D82").into());
/// object.fields.insert("@location".into(), alox_48::Instance::from("Menlo Park, CA").into());
/// let original = alox_48::Value::Object(object);
///
/// let value = alox_48::to_value(User { fingerprint: "0xF9BA143B95FF6D82".to_string(), location: "Menlo Park, CA".to_string() }).unwrap();
/// assert_eq!(original, value);
///
/// ```
///
/// # Errors
///
/// This conversion can fail if `T`'s implementation of `Serialize` decides to fail, or uses an unsupported data type.