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
//! A decoder library for your types.
//!
//! When using [`serde`], your types become entangled with serialization logic due to the [`Serialize`] and [`Deserialize`] traits.
//!
//! This crate lets you decouple serialization logic by leveraging simple functions, at some performance cost:
//!
//! ```rust,no_run
//! use decoder::{Map, Result, Value};
//!
//! struct Person {
//! name: String,
//! projects: Vec<Project>
//! }
//!
//! struct Project {
//! name: String,
//! url: String,
//! }
//!
//! impl Person {
//! fn decode(value: Value) -> Result<Self> {
//! use decoder::decode::{map, sequence, string};
//!
//! let mut person = map(value)?;
//!
//! Ok(Self {
//! name: person.required("name", string)?,
//! projects: person.required("projects", sequence(Project::decode))?,
//! })
//! }
//!
//! fn encode(&self) -> Value {
//! use decoder::encode::{map, sequence, string};
//!
//! map([
//! ("name", string(&self.name)),
//! ("projects", sequence(Project::encode, &self.projects)),
//! ])
//! .into()
//! }
//! }
//!
//! impl Project {
//! fn decode(value: Value) -> Result<Self> {
//! use decoder::decode::{map, string};
//!
//! let mut project = map(value)?;
//!
//! Ok(Project {
//! name: project.required("name", string)?,
//! url: project.required("url", string)?
//! })
//! }
//!
//! fn encode(&self) -> Value {
//! use decoder::encode::{map, string};
//!
//! map([
//! ("name", string(&self.name)),
//! ("url", string(&self.url)),
//! ])
//! .into()
//! }
//! }
//!
//! let person =
//! decoder::run(serde_json::from_str, Person::decode, "{ ... }").expect("Decode person");
//!
//! let _ = serde_json::to_string(&person.encode());
//! ```
//!
//! You can try this crate if the [`serde`] way™ has become painful or it does not resonate with you.
//!
//! [`serde`]: https://serde.rs
//! [`Serialize`]: https://docs.rs/serde/latest/serde/trait.Serialize.html
//! [`Deserialize`]: https://docs.rs/serde/latest/serde/trait.Deserialize.html
pub use Error;
pub use ;
/// A decoding result.
pub type Result<T> = Result;
/// Some logic that turns a [`Value`] into some [`Output`](Self::Output).
/// Runs a [`Decoder`] using the given function to deserialize a [`Value`]
/// from the given input.