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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Encode a value through [`Display`] and decode it through [`FromStr`].
//!
//! This module can be used as a field adapter with
//! [`#[musli(with = musli::display)]`][with], which makes a field a string on
//! the wire. Its [`option`] submodule does the same for an `Option<T>` field.
//!
//! For a type which should always be a string on the wire, the derives also
//! accept [`#[musli(display)]`][display] on the type itself.
//!
//! Decoding fails with the [`FromStr`] error, formatted through its
//! [`Display`] implementation, if the string can not be parsed.
//!
//! [`Display`]: core::fmt::Display
//! [with]: https://docs.rs/musli/latest/musli/_help/derives/index.html#musliwith--path
//! [display]: https://docs.rs/musli/latest/musli/_help/derives/index.html#muslidisplay
//!
//! # Examples
//!
//! ```
//! use std::net::Ipv4Addr;
//!
//! use musli::{Decode, Encode};
//!
//! #[derive(Debug, PartialEq, Encode, Decode)]
//! struct Host {
//! #[musli(with = musli::display)]
//! address: Ipv4Addr,
//! #[musli(with = musli::display::option)]
//! gateway: Option<Ipv4Addr>,
//! }
//!
//! let host = Host {
//! address: Ipv4Addr::new(10, 0, 0, 2),
//! gateway: None,
//! };
//!
//! let json = musli::json::to_string(&host)?;
//! assert_eq!(json, r#"{"address":"10.0.0.2","gateway":null}"#);
//! assert_eq!(musli::json::from_str::<Host>(&json)?, host);
//!
//! let error = musli::json::from_str::<Host>(r#"{"address":"10.0.0","gateway":null}"#);
//! assert!(error.is_err());
//! # Ok::<_, musli::json::Error>(())
//! ```
use fmt;
use FromStr;
use crate::;
/// Encode `value` as a string through its [`Display`] implementation.
///
/// [`Display`]: core::fmt::Display
///
/// # Examples
///
/// ```
/// use std::net::Ipv4Addr;
///
/// use musli::Encode;
///
/// #[derive(Encode)]
/// struct Host {
/// #[musli(encode_with = musli::display::encode)]
/// address: Ipv4Addr,
/// }
///
/// let host = Host { address: Ipv4Addr::LOCALHOST };
/// assert_eq!(musli::json::to_string(&host)?, r#"{"address":"127.0.0.1"}"#);
/// # Ok::<_, musli::json::Error>(())
/// ```
/// Decode a string and parse it through [`FromStr`].
///
/// A parse error is reported through the decoder's context, formatted through
/// its [`Display`] implementation.
///
/// [`Display`]: core::fmt::Display
///
/// # Examples
///
/// ```
/// use std::net::Ipv4Addr;
///
/// use musli::Decode;
///
/// #[derive(Debug, PartialEq, Decode)]
/// struct Host {
/// #[musli(decode_with = musli::display::decode)]
/// address: Ipv4Addr,
/// }
///
/// let host: Host = musli::json::from_str(r#"{"address":"127.0.0.1"}"#)?;
/// assert_eq!(host, Host { address: Ipv4Addr::LOCALHOST });
/// # Ok::<_, musli::json::Error>(())
/// ```
/// Encode an `Option<T>` through [`Display`] and decode it through [`FromStr`].
///
/// The value is encoded as an optional string, so `None` is encoded the same
/// way as it is for any other `Option<T>`.
///
/// [`Display`]: core::fmt::Display
///
/// # Examples
///
/// ```
/// use std::net::Ipv4Addr;
///
/// use musli::{Decode, Encode};
///
/// #[derive(Debug, PartialEq, Encode, Decode)]
/// struct Host {
/// #[musli(with = musli::display::option)]
/// gateway: Option<Ipv4Addr>,
/// }
///
/// let host = Host { gateway: Some(Ipv4Addr::new(10, 0, 0, 1)) };
/// let json = musli::json::to_string(&host)?;
/// assert_eq!(json, r#"{"gateway":"10.0.0.1"}"#);
/// assert_eq!(musli::json::from_str::<Host>(&json)?, host);
/// # Ok::<_, musli::json::Error>(())
/// ```