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
|
//! [![github]](https://github.com/dtolnay/serde-repr) [![crates-io]](https://crates.io/crates/serde_repr) [![docs-rs]](https://docs.rs/serde_repr)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! <br>
//!
//! Derive `Serialize` and `Deserialize` that delegates to the underlying repr
//! of a C-like enum.
//!
//! # Examples
//!
//! ```
//! use serde_repr::{Serialize_repr, Deserialize_repr};
//!
//! #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
//! #[repr(u8)]
//! enum SmallPrime {
//! Two = 2,
//! Three = 3,
//! Five = 5,
//! Seven = 7,
//! }
//!
//! fn main() -> serde_json::Result<()> {
//! let j = serde_json::to_string(&SmallPrime::Seven)?;
//! assert_eq!(j, "7");
//!
//! let p: SmallPrime = serde_json::from_str("2")?;
//! assert_eq!(p, SmallPrime::Two);
//!
//! Ok(())
//! }
//! ```
#![allow(clippy::single_match_else)]
extern crate proc_macro;
mod parse;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
use crate::parse::Input;
#[proc_macro_derive(Serialize_repr)]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Input);
let ident = input.ident;
let repr = input.repr;
let match_variants = input.variants.iter().map(|variant| {
let variant = &variant.ident;
quote! {
#ident::#variant => #ident::#variant as #repr,
}
});
TokenStream::from(quote! {
impl serde::Serialize for #ident {
#[allow(clippy::use_self)]
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer
{
let value: #repr = match *self {
#(#match_variants)*
};
serde::Serialize::serialize(&value, serializer)
}
}
})
}
#[proc_macro_derive(Deserialize_repr, attributes(serde))]
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Input);
let ident = input.ident;
let repr = input.repr;
let variants = input.variants.iter().map(|variant| &variant.ident);
let declare_discriminants = input.variants.iter().map(|variant| {
let variant = &variant.ident;
quote! {
const #variant: #repr = #ident::#variant as #repr;
}
});
let match_discriminants = input.variants.iter().map(|variant| {
let variant = &variant.ident;
quote! {
discriminant::#variant => core::result::Result::Ok(#ident::#variant),
}
});
let error_format = match input.variants.len() {
1 => "invalid value: {}, expected {}".to_owned(),
2 => "invalid value: {}, expected {} or {}".to_owned(),
n => "invalid value: {}, expected one of: {}".to_owned() + &", {}".repeat(n - 1),
};
let other_arm = match input.default_variant {
Some(variant) => {
let variant = &variant.ident;
quote! {
core::result::Result::Ok(#ident::#variant)
}
}
None => quote! {
core::result::Result::Err(serde::de::Error::custom(
format_args!(#error_format, other #(, discriminant::#variants)*)
))
},
};
TokenStream::from(quote! {
impl<'de> serde::Deserialize<'de> for #ident {
#[allow(clippy::use_self)]
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct discriminant;
#[allow(non_upper_case_globals)]
impl discriminant {
#(#declare_discriminants)*
}
match <#repr as serde::Deserialize>::deserialize(deserializer)? {
#(#match_discriminants)*
other => #other_arm,
}
}
}
})
}
|