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
extern crate serde_crate as serde;
use serde::{
de::{Deserialize, Deserializer},
ser::{Error as _, Serialize, Serializer},
};
use crate::JsOption;
impl<'de, T> Deserialize<'de> for JsOption<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Option::<T>::deserialize(deserializer).map(Self::from_option)
}
}
impl<T> Serialize for JsOption<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Some(val) => serializer.serialize_some(val),
Self::Null => serializer.serialize_none(),
Self::Undefined => Err(S::Error::custom("attempted to serialize `undefined`")),
}
}
}