@@ -83,32 +83,131 @@ struct Pretty {
83
83
sequence_index : Vec < usize > ,
84
84
}
85
85
86
- /// Pretty serializer configuration
86
+ /// Pretty serializer configuration.
87
+ ///
88
+ /// # Examples
89
+ ///
90
+ /// ```
91
+ /// use ron::ser::PrettyConfig;
92
+ ///
93
+ /// let my_config = PrettyConfig::new()
94
+ /// .with_depth_limit(4)
95
+ /// // definitely superior (okay, just joking)
96
+ /// .with_indentor("\t".to_owned());
97
+ /// ```
87
98
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
88
99
pub struct PrettyConfig {
89
100
/// Limit the pretty-ness up to the given depth.
101
+ #[ serde( default = "default_depth_limit" ) ]
90
102
pub depth_limit : usize ,
91
103
/// New line string
104
+ #[ serde( default = "default_new_line" ) ]
92
105
pub new_line : String ,
93
106
/// Indentation string
107
+ #[ serde( default = "default_indentor" ) ]
94
108
pub indentor : String ,
95
109
/// Separate tuple members with indentation
110
+ #[ serde( default = "default_separate_tuple_members" ) ]
96
111
pub separate_tuple_members : bool ,
97
112
/// Enumerate array items in comments
113
+ #[ serde( default = "default_enumerate_arrays" ) ]
98
114
pub enumerate_arrays : bool ,
115
+ /// Private field to ensure adding a field is non-breaking.
116
+ #[ serde( skip) ]
117
+ _future_proof : ( ) ,
118
+ }
119
+
120
+ impl PrettyConfig {
121
+ /// Creates a default `PrettyConfig`.
122
+ pub fn new ( ) -> Self {
123
+ Default :: default ( )
124
+ }
125
+
126
+ /// Limits the pretty-formatting based on the number of indentations.
127
+ /// I.e., with a depth limit of 5, starting with an element of depth (indentation level) 6,
128
+ /// everything will be put into the same line, without pretty formatting.
129
+ ///
130
+ /// Default: [std::usize::MAX]
131
+ pub fn with_depth_limit ( mut self , depth_limit : usize ) -> Self {
132
+ self . depth_limit = depth_limit;
133
+
134
+ self
135
+ }
136
+
137
+ /// Configures the newlines used for serialization.
138
+ ///
139
+ /// Default: `\r\n` on Windows, `\n` otherwise
140
+ pub fn with_new_line ( mut self , new_line : String ) -> Self {
141
+ self . new_line = new_line;
142
+
143
+ self
144
+ }
145
+
146
+ /// Configures the string sequence used for indentation.
147
+ ///
148
+ /// Default: 4 spaces
149
+ pub fn with_indentor ( mut self , indentor : String ) -> Self {
150
+ self . indentor = indentor;
151
+
152
+ self
153
+ }
154
+
155
+ /// Configures whether tuples are single- or multi-line.
156
+ /// If set to `true`, tuples will have their fields indented and in new lines.
157
+ /// If set to `false`, tuples will be serialized without any newlines or indentations.
158
+ ///
159
+ /// Default: `false`
160
+ pub fn with_separate_tuple_members ( mut self , separate_tuple_members : bool ) -> Self {
161
+ self . separate_tuple_members = separate_tuple_members;
162
+
163
+ self
164
+ }
165
+
166
+ /// Configures whether a comment shall be added to every array element, indicating
167
+ /// the index.
168
+ ///
169
+ /// Default: `false`
170
+ pub fn with_enumerate_arrays ( mut self , enumerate_arrays : bool ) -> Self {
171
+ self . enumerate_arrays = enumerate_arrays;
172
+
173
+ self
174
+ }
175
+ }
176
+
177
+ fn default_depth_limit ( ) -> usize {
178
+ !0
179
+ }
180
+
181
+ fn default_new_line ( ) -> String {
182
+ #[ cfg( not( target_os = "windows" ) ) ]
183
+ let new_line = "\n " . to_string ( ) ;
184
+ #[ cfg( target_os = "windows" ) ]
185
+ let new_line = "\r \n " . to_string ( ) ;
186
+
187
+ new_line
188
+ }
189
+
190
+ fn default_indentor ( ) -> String {
191
+ " " . to_string ( )
192
+ }
193
+
194
+ fn default_separate_tuple_members ( ) -> bool {
195
+ false
196
+ }
197
+
198
+ fn default_enumerate_arrays ( ) -> bool {
199
+ false
99
200
}
100
201
101
202
impl Default for PrettyConfig {
102
203
fn default ( ) -> Self {
103
204
PrettyConfig {
104
- depth_limit : !0 ,
105
- #[ cfg( not( target_os = "windows" ) ) ]
106
- new_line : "\n " . to_string ( ) ,
107
- #[ cfg( target_os = "windows" ) ]
108
- new_line : "\r \n " . to_string ( ) ,
109
- indentor : " " . to_string ( ) ,
110
- separate_tuple_members : false ,
111
- enumerate_arrays : false ,
205
+ depth_limit : default_depth_limit ( ) ,
206
+ new_line : default_new_line ( ) ,
207
+ indentor : default_indentor ( ) ,
208
+ separate_tuple_members : default_separate_tuple_members ( ) ,
209
+ enumerate_arrays : default_enumerate_arrays ( ) ,
210
+ _future_proof : ( ) ,
112
211
}
113
212
}
114
213
}
0 commit comments