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
pub use crate::perf_context::PerfLevel;
macro_rules! numeric_enum_mod {
($name:ident $enum:ident { $($variant:ident = $value:expr, )* }) => {
pub mod $name {
use std::fmt;
use serde::{Serializer, Deserializer};
use serde::de::{self, Unexpected, Visitor};
use crate::$enum;
pub fn serialize<S>(mode: &$enum, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
serializer.serialize_i64(*mode as i64)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<$enum, D::Error>
where D: Deserializer<'de>
{
struct EnumVisitor;
impl<'de> Visitor<'de> for EnumVisitor {
type Value = $enum;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, concat!("valid ", stringify!($enum)))
}
fn visit_i64<E>(self, value: i64) -> Result<$enum, E>
where E: de::Error
{
match value {
$( $value => Ok($enum::$variant), )*
_ => Err(E::invalid_value(Unexpected::Signed(value), &self))
}
}
}
deserializer.deserialize_i64(EnumVisitor)
}
#[cfg(test)]
mod tests {
use toml;
use crate::$enum;
#[test]
fn test_serde() {
#[derive(Serialize, Deserialize, PartialEq)]
struct EnumHolder {
#[serde(with = "super")]
e: $enum,
}
let cases = vec![
$(($enum::$variant, $value), )*
];
for (e, v) in cases {
let holder = EnumHolder { e };
let res = toml::to_string(&holder).unwrap();
let exp = format!("e = {}\n", v);
assert_eq!(res, exp);
let h: EnumHolder = toml::from_str(&exp).unwrap();
assert!(h == holder);
}
}
}
}
}
}
numeric_enum_mod! {perf_level_serde PerfLevel {
Uninitialized = 0,
Disable = 1,
EnableCount = 2,
EnableTimeExceptForMutex = 3,
EnableTimeAndCPUTimeExceptForMutex = 4,
EnableTime = 5,
OutOfBounds = 6,
}}