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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Deserialize EnCom data to a Rust data structure.
pub
// use self::wrapper::InitDeserializer;
use crate;
use cratelexical;
use crateNumber;
use iter;
use result;
use FromStr;
use de;
use crateNumberDeserializer;
pub use Deserializer;
pub use IoRead;
pub use ;
pub use InitDeserializer;
/// Deserialize an instance of type `T` from an IO stream of EnCom.
///
/// The content of the IO stream is deserialized directly from the stream
/// without being buffered in memory by serde_encom.
///
/// When reading from a source against which short reads are not efficient, such
/// as a [`File`], you will want to apply your own buffering because serde_encom
/// will not buffer the input. See [`std::io::BufReader`].
///
/// It is expected that the input stream ends after the deserialized object.
/// If the stream does not end, such as in the case of a persistent socket connection,
/// this function will not return. It is possible instead to deserialize from a prefix of an input
/// stream without looking for EOF by managing your own [`Deserializer`].
///
/// Note that counter to intuition, this function is usually slower than
/// reading a file completely into memory and then applying [`from_str`]
/// or [`from_slice`] on it. See [issue #160].
///
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
/// [`from_str`]: ./fn.from_str.html
/// [`from_slice`]: ./fn.from_slice.html
/// [issue #160]: https://github.com/serde-rs/json/issues/160
///
/// # Example
///
/// Reading the contents of a file.
///
/// ```
/// use serde::Deserialize;
///
/// use std::error::Error;
/// use std::fs::File;
/// use std::io::BufReader;
/// use std::path::Path;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<dyn Error>> {
/// // Open the file in read-only mode with buffer.
/// let file = File::open(path)?;
/// let reader = BufReader::new(file);
///
/// // Read the EnCom contents of the file as an instance of `User`.
/// let u = serde_encom::from_reader(reader)?;
///
/// // Return the `User`.
/// Ok(u)
/// }
///
/// fn main() {
/// # }
/// # fn fake_main() {
/// let u = read_user_from_file("test.encom").unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// Reading from a persistent socket connection.
///
/// ```
/// use serde::Deserialize;
///
/// use std::error::Error;
/// use std::net::{TcpListener, TcpStream};
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
/// let mut de = serde_encom::Deserializer::from_reader(tcp_stream);
/// let u = User::deserialize(&mut de)?;
///
/// Ok(u)
/// }
///
/// fn main() {
/// # }
/// # fn fake_main() {
/// let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
///
/// for stream in listener.incoming() {
/// println!("{:#?}", read_user_from_stream(stream.unwrap()));
/// }
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than an EnCom map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the EnCom map or some number is too big to fit in the expected primitive
/// type.
/// Deserialize an instance of type `T` from bytes of EnCom text.
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn main() {
/// // The type of `j` is `&[u8]`
/// let j = b"
/// fingerprint:18=0xF9BA143B95FF6D82
/// location:14=Menlo Park, CA
/// ";
///
/// let u: User = serde_encom::from_slice(j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than an EnCom map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the EnCom map or some number is too big to fit in the expected primitive
/// type.
/// Deserialize an instance of type `T` from a string of EnCom text.
///
/// # Example
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
/// fingerprint: String,
/// location: String,
/// }
///
/// fn main() {
/// // The type of `j` is `&str`
/// let j = "
/// fingerprint:18=0xF9BA143B95FF6D82
/// location:14=Menlo Park, CA
/// ";
///
/// let u: User = serde_encom::from_str(j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the input does not match the
/// structure expected by `T`, for example if `T` is a struct type but the input
/// contains something other than an EnCom map. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the EnCom map or some number is too big to fit in the expected primitive
/// type.