serde_double_tag/
lib.rs

1//! This crates provides derive macros for a double tagged enum representation for [`serde`].
2//! It is basically a combination of externally and adjecently tagged enums.
3//!
4//! If you enable the `schemars` feature,
5//! the crate also exposes a derive macro for the [`schemars::JsonSchema`] trait.
6//!
7//! For example, consider this enum:
8//! ```
9//! #[derive(serde_double_tag::Deserialize, serde_double_tag::Serialize)]
10//! #[serde(tag = "species")]
11//! #[serde(rename_all = "snake_case")]
12//! enum Friend {
13//!   Human {
14//!     name: String,
15//!     hobbies: Vec<String>,
16//!   },
17//!   Dog {
18//!     name: String,
19//!     color: String,
20//!   }
21//! }
22//! ```
23//!
24//! A `Friend::Human` will be serialized as:
25//! ```json
26//! {
27//!   "species": "human",
28//!   "human": {
29//!     "name": "Zohan",
30//!     "hobbies": ["hair dressing"],
31//!   }
32//! }
33//! ```
34//!
35//! Similarly, a `Friend::Dog` will be serialized as:
36//! ```json
37//! {
38//!   "species": "dog",
39//!   "dog": {
40//!     "name": "Scrappy",
41//!     "color": "white and gray",
42//!   }
43//! }
44//! ```
45//!
46//! This enum representation could be useful if you want data for the different variants to co-exist in a single file or in your database.
47//! Since each variant uses a different field name, they will never conflict.
48//! And since there is still a separate field for the enum tag, you can still known which variant is actually active.
49//!
50//! Currently supported `serde` attributes:
51//! * `#[serde(rename = "...")]
52//! * `#[serde(rename_all = "...")]
53//! * `#[serde(rename_all_fields = "...")]
54//! * `#[serde(deny_unknown_fields = "...")]
55
56#![deny(missing_docs)]
57#![deny(missing_debug_implementations)]
58
59#[doc(hidden)]
60#[allow(missing_debug_implementations)]
61pub mod internal__;
62
63/// Derive [`serde::Serialize`] for an enum using the double-tagged enum representation.
64///
65/// See the module documentation for details on the enum representation.
66pub use serde_double_tag_derive::Serialize;
67
68/// Derive [`serde::Deserialize`] for an enum using the double-tagged enum representation.
69///
70/// See the module documentation for details on the enum representation.
71pub use serde_double_tag_derive::Deserialize;
72
73/// Derive [`schemars::JsonSchema`] for an enum using the double-tagged enum representation.
74///
75/// See the module documentation for details on the enum representation.
76#[cfg(feature = "schemars")]
77#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "schemars")))]
78pub use serde_double_tag_derive::JsonSchema;