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
|
//! Roundtrip serde Options module.
use std::io;
use serde::{de, ser, Deserialize, Serialize};
use crate::de::Deserializer;
use crate::error::{Result, SpannedResult};
use crate::extensions::Extensions;
use crate::ser::{PrettyConfig, Serializer};
/// Roundtrip serde options.
///
/// # Examples
///
/// ```
/// use ron::{Options, extensions::Extensions};
///
/// let ron = Options::default()
/// .with_default_extension(Extensions::IMPLICIT_SOME);
///
/// let de: Option<i32> = ron.from_str("42").unwrap();
/// let ser = ron.to_string(&de).unwrap();
///
/// assert_eq!(ser, "42");
/// ```
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct Options {
/// Extensions that are enabled by default during serialization and
/// deserialization.
/// During serialization, these extensions do NOT have to be explicitly
/// enabled in the parsed RON.
/// During deserialization, these extensions are used, but their explicit
/// activation is NOT included in the output RON.
/// No extensions are enabled by default.
pub default_extensions: Extensions,
}
impl Default for Options {
fn default() -> Self {
Self {
default_extensions: Extensions::empty(),
}
}
}
impl Options {
#[must_use]
/// Enable `default_extension` by default during serialization and deserialization.
pub fn with_default_extension(mut self, default_extension: Extensions) -> Self {
self.default_extensions |= default_extension;
self
}
#[must_use]
/// Do NOT enable `default_extension` by default during serialization and deserialization.
pub fn without_default_extension(mut self, default_extension: Extensions) -> Self {
self.default_extensions &= !default_extension;
self
}
}
impl Options {
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from a reader.
pub fn from_reader<R, T>(&self, mut rdr: R) -> SpannedResult<T>
where
R: io::Read,
T: de::DeserializeOwned,
{
let mut bytes = Vec::new();
rdr.read_to_end(&mut bytes)?;
self.from_bytes(&bytes)
}
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from a string.
pub fn from_str<'a, T>(&self, s: &'a str) -> SpannedResult<T>
where
T: de::Deserialize<'a>,
{
self.from_bytes(s.as_bytes())
}
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from bytes.
pub fn from_bytes<'a, T>(&self, s: &'a [u8]) -> SpannedResult<T>
where
T: de::Deserialize<'a>,
{
self.from_bytes_seed(s, std::marker::PhantomData)
}
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from a reader
/// and a seed.
pub fn from_reader_seed<R, S, T>(&self, mut rdr: R, seed: S) -> SpannedResult<T>
where
R: io::Read,
S: for<'a> de::DeserializeSeed<'a, Value = T>,
{
let mut bytes = Vec::new();
rdr.read_to_end(&mut bytes)?;
self.from_bytes_seed(&bytes, seed)
}
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from a string
/// and a seed.
pub fn from_str_seed<'a, S, T>(&self, s: &'a str, seed: S) -> SpannedResult<T>
where
S: de::DeserializeSeed<'a, Value = T>,
{
self.from_bytes_seed(s.as_bytes(), seed)
}
/// A convenience function for building a deserializer
/// and deserializing a value of type `T` from bytes
/// and a seed.
pub fn from_bytes_seed<'a, S, T>(&self, s: &'a [u8], seed: S) -> SpannedResult<T>
where
S: de::DeserializeSeed<'a, Value = T>,
{
let mut deserializer = Deserializer::from_bytes_with_options(s, self.clone())?;
let value = seed
.deserialize(&mut deserializer)
.map_err(|e| deserializer.span_error(e))?;
deserializer.end().map_err(|e| deserializer.span_error(e))?;
Ok(value)
}
/// Serializes `value` into `writer`
pub fn to_writer<W, T>(&self, writer: W, value: &T) -> Result<()>
where
W: io::Write,
T: ?Sized + ser::Serialize,
{
let mut s = Serializer::with_options(writer, None, self.clone())?;
value.serialize(&mut s)
}
/// Serializes `value` into `writer` in a pretty way.
pub fn to_writer_pretty<W, T>(&self, writer: W, value: &T, config: PrettyConfig) -> Result<()>
where
W: io::Write,
T: ?Sized + ser::Serialize,
{
let mut s = Serializer::with_options(writer, Some(config), self.clone())?;
value.serialize(&mut s)
}
/// Serializes `value` and returns it as string.
///
/// This function does not generate any newlines or nice formatting;
/// if you want that, you can use `to_string_pretty` instead.
pub fn to_string<T>(&self, value: &T) -> Result<String>
where
T: ?Sized + ser::Serialize,
{
let mut output = Vec::new();
let mut s = Serializer::with_options(&mut output, None, self.clone())?;
value.serialize(&mut s)?;
Ok(String::from_utf8(output).expect("Ron should be utf-8"))
}
/// Serializes `value` in the recommended RON layout in a pretty way.
pub fn to_string_pretty<T>(&self, value: &T, config: PrettyConfig) -> Result<String>
where
T: ?Sized + ser::Serialize,
{
let mut output = Vec::new();
let mut s = Serializer::with_options(&mut output, Some(config), self.clone())?;
value.serialize(&mut s)?;
Ok(String::from_utf8(output).expect("Ron should be utf-8"))
}
}
|