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
//! This crate provides a set of Enums that help you to define data structures
//! that accept either strings, structs or arrays.
//!
//! # Example
//! ```rust
//! use serde::{Serialize, Deserialize};
//! use serde_either::StringOrStruct;
//! use serde_json;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Authors {
//! first_name: String,
//! last_name: String
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct Book {
//! pub authors: StringOrStruct<Authors>
//! }
//!
//! // And StringOrStruct is just a normal enum
//!
//! impl Book {
//! fn get_author_name(&self) -> String {
//! match &self.authors {
//! StringOrStruct::String(s) => s.to_owned(),
//! StringOrStruct::Struct(author) => format!("{} {}", &author.first_name, &author.last_name)
//! }
//! }
//! }
//!
//! let books = r#"[
//! {
//! "authors": {
//! "first_name": "John",
//! "last_name": "Smith"
//! }
//! },
//! {
//! "authors": "Michael J. Smith"
//! }
//! ]"#;
//!
//! let res: Vec<Book> = serde_json::from_str(books).unwrap();
//! assert_eq!(res[0].get_author_name(), "John Smith");
//! assert_eq!(res[1].get_author_name(), "Michael J. Smith");
//!
//! ```
//!
pub use *;