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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use IBig;
use ;
pub use ;
/// Helper for `serde` to deserialize a `BigInt` to
/// any type which implements `TryFrom<dashu_int::IBig>`.
///
/// To be used in conjunction with [`As`].
///
/// ## Example
///
/// ```rust
/// use std::collections::HashMap;
///
/// use dashu_int::IBig;
/// use serde::Deserialize;
///
/// use itf::Trace;
/// use itf::de::{As, Integer};
///
/// let json = serde_json::json!([
/// {
/// "_foo": {"#map": [[{"#bigint": "1"}, {"#bigint": "2"}]]},
/// "typ": "Foo",
/// },
/// {
/// "_bar": [[[{"#bigint": "1"}, {"#bigint": "2"}]]],
/// "typ": "Bar",
/// }
/// ]);
///
/// // Deserialize as `dashu_int::IBig`
/// #[derive(Deserialize, Debug)]
/// #[serde(tag = "typ")]
/// enum FooBarBigInt {
/// Foo { _foo: HashMap<IBig, IBig> },
/// Bar { _bar: Vec<Vec<(IBig, IBig)>> },
/// }
/// itf::from_value::<Vec<FooBarBigInt>>(json.clone()).unwrap();
///
/// // Deserialize as `i64`
/// #[derive(Deserialize, Debug)]
/// #[serde(tag = "typ")]
/// enum FooBarInt {
/// // try to deserialize _foo as i64, instead of BigInt
/// Foo {
/// #[serde(with = "As::<HashMap<Integer, Integer>>")]
/// _foo: HashMap<i64, i64>,
/// },
/// Bar {
/// #[serde(with = "As::<Vec<Vec<(Integer, Integer)>>>")]
/// _bar: Vec<Vec<(i64, i64)>>,
/// },
/// }
/// itf::from_value::<Vec<FooBarInt>>(json.clone()).unwrap();
///
/// // Deserialize as a mix
/// #[derive(Deserialize, Debug)]
/// #[serde(tag = "typ")]
/// enum FooBarMixInt {
/// // try to deserialize _foo as i64, instead of BigInt
/// Foo {
/// #[serde(with = "As::<HashMap<Integer, Integer>>")]
/// _foo: HashMap<i64, IBig>,
/// },
/// Bar {
/// #[serde(with = "As::<Vec<Vec<(Integer, Integer)>>>")]
/// _bar: Vec<Vec<(IBig, u64)>>,
/// },
/// }
/// itf::from_value::<Vec<FooBarMixInt>>(json.clone()).unwrap();
/// ```
pub type Integer = TryFromInto;
/// Helper for `serde` to deserialize types isomorphic to [`std::option::Option`].
///
/// To be used in conjunction with [`As`].
///
/// ## Example
///
/// ```rust
/// use serde::Deserialize;
/// use serde_json::json;
///
/// use itf::de::{self, As};
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// struct FooOption {
/// #[serde(with = "As::<de::Option::<_>>")]
/// foo: Option<u64>,
/// }
///
/// let some_itf = json!({
/// "foo": {
/// "tag": "Some",
/// "value": 42,
/// }
/// });
///
/// let some_foo = itf::from_value::<FooOption>(some_itf).unwrap();
/// assert_eq!(some_foo, FooOption { foo: Some(42) });
///
/// let none_itf = json!({
/// "foo": {
/// "tag": "None",
/// "value": {},
/// }
/// });
///
/// let none_foo = itf::from_value::<FooOption>(none_itf).unwrap();
/// assert_eq!(none_foo, FooOption { foo: None });
/// ```
pub type Option<T> = FromInto;
/// A type isomorphic to [`std::option::Option`].
///
/// Can either be used directly or with [`As`] together with [`Option`].
/// Helper for `serde` to deserialize types isomorphic to [`std::result::Result`].
///
/// To be used in conjunction with [`As`].
///
/// ## Example
///
/// ```rust
/// use serde::Deserialize;
/// use serde_json::json;
///
/// use itf::de::{self, As};
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// struct FooResult {
/// #[serde(with = "As::<de::Result::<_, _>>")]
/// foo: Result<u64, u64>,
/// }
///
/// let ok_itf = json!({
/// "foo": {
/// "tag": "Ok",
/// "value": 42,
/// }
/// });
///
/// let ok = itf::from_value::<FooResult>(ok_itf).unwrap();
/// assert_eq!(ok.foo, Ok(42));
///
/// let err_itf = json!({
/// "foo": {
/// "tag": "Err",
/// "value": 42,
/// }
/// });
///
/// let err = itf::from_value::<FooResult>(err_itf).unwrap();
/// assert_eq!(err.foo, Err(42));
/// ```
pub type Result<T, E> = FromInto;
/// A type isomorphic to [`std::result::Result`].
///
/// Can either be used directly or with [`As`] together with [`Result`].