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
//! # `serenum`
//!
//! ## Example usage
//!
//! Source code:
//!
//! ```rust
//! #[derive(SerEnum)]
//! pub enum Order {
//! #[serenum(text = "full")]
//! Full,
//! #[serenum(text = "short")]
//! Short,
//! }
//! assert_eq!(Order::Full.text(), "full");
//! assert_eq!(Order::Short.text(), "short");
//! ```
//!
//! Expanded:
//!
//! ```rust
//! impl ::serde::Serialize for Order {
//! fn serialize<S>(&self, serializer: S) -> ::core::result::Result<S::Ok, S::Error>
//! where
//! S: ::serde::Serializer,
//! {
//! serializer.serialize_str(self.text())
//! }
//! }
//! impl<'de> ::serde::Deserialize<'de> for Order {
//! fn deserialize<D>(deserializer: D) -> ::core::result::Result<Self, D::Error>
//! where
//! D: ::serde::Deserializer<'de>,
//! {
//! #[allow(non_camel_case_types)]
//! #[derive(::serde::Deserialize)]
//! enum __impl_Order {
//! #[serde(rename = "full")]
//! Full,
//! #[serde(rename = "short")]
//! Short,
//! }
//! let result = <__impl_Order as ::serde::Deserialize<'de>>::deserialize(deserializer)?;
//! Ok(match result {
//! __impl_Order::Full => Order::Full,
//! __impl_Order::Short => Order::Short,
//! })
//! }
//! }
//! impl Order {
//! pub const FULL: &'static str = "full";
//! pub const SHORT: &'static str = "short";
//! pub fn from_text(text: &impl ::core::convert::AsRef<str>) -> ::core::option::Option<Self> {
//! match <_ as ::core::convert::AsRef<str>>::as_ref(text) {
//! Self::FULL => ::core::option::Option::Some(Self::Full),
//! Self::SHORT => ::core::option::Option::Some(Self::Short),
//! _ => ::core::option::Option::None,
//! }
//! }
//! pub const fn text(&self) -> &str {
//! match self {
//! Self::Full => Self::FULL,
//! Self::Short => Self::SHORT,
//! }
//! }
//! }
//! ```
//!
//! ## Features
//!
//! | name | default or not | description |
//! | ------- | -------------- | -------------------------------------------------------------------- |
//! | `serde` | default | Generate `serde::Serialize` and `serde::Deserialize` implementation. |
//!
//! ## Macro configuration
//!
//! ### Enum level
//!
//! | name | syntax | required or not | description |
//! | ------ | ----------------------------- | -------------------- | ----------------------------------- |
//! | `from` | `from = "some_function_name"` | default: `from_text` | The generated string parser's name. |
//! | `to` | `to = "some_function_name"` | default: `text` | The generated text getter's name. |
//!
//! ### Variant level
//!
//! | name | syntax | required or not | description |
//! | ------------ | --------------------------- | ------------------------------------------------- | ------------------------------------------------ |
//! | `text` | `text = "variant_repr"` | required | You string representation of the variant. |
//! | `const_name` | `const_name = "CONST_NAME"` | default: UPPER_SNAKE_CASE of your variant's name. | The associated const item's name of the variant. |
use Config;
use FromDeriveInput;
use TokenStream;
use quote;
use ;