1
use alloc::{borrow::Cow, string::String};
2
use core::fmt;
3

            
4
use serde::{ser, ser::Serialize};
5
use serde_derive::{Deserialize, Serialize};
6
use unicode_ident::is_xid_continue;
7

            
8
use crate::{
9
    error::{Error, Result},
10
    extensions::Extensions,
11
    options::Options,
12
    parse::{is_ident_first_char, is_ident_raw_char, is_whitespace_char, LargeSInt, LargeUInt},
13
};
14

            
15
pub mod path_meta;
16

            
17
mod raw;
18
#[cfg(test)]
19
mod tests;
20
mod value;
21

            
22
/// Serializes `value` into `writer`.
23
///
24
/// This function does not generate any newlines or nice formatting;
25
/// if you want that, you can use [`to_writer_pretty`] instead.
26
///
27
/// `writer` is required to implement [`core::fmt::Write`]. To use [`std::io::Write`] instead,
28
/// see [`Options::to_io_writer`](crate::options::Options::to_io_writer).
29
360
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<()>
30
360
where
31
360
    W: fmt::Write,
32
360
    T: ?Sized + Serialize,
33
{
34
360
    Options::default().to_writer(writer, value)
35
360
}
36

            
37
/// Serializes `value` into `writer` in a pretty way.
38
///
39
/// `writer` is required to implement [`core::fmt::Write`]. To use [`std::io::Write`] instead,
40
/// see [`Options::to_io_writer_pretty`](crate::options::Options::to_io_writer_pretty).
41
360
pub fn to_writer_pretty<W, T>(writer: W, value: &T, config: PrettyConfig) -> Result<()>
42
360
where
43
360
    W: fmt::Write,
44
360
    T: ?Sized + Serialize,
45
{
46
360
    Options::default().to_writer_pretty(writer, value, config)
47
360
}
48

            
49
/// Serializes `value` and returns it as string.
50
///
51
/// This function does not generate any newlines or nice formatting;
52
/// if you want that, you can use [`to_string_pretty`] instead.
53
1628
pub fn to_string<T>(value: &T) -> Result<String>
54
1628
where
55
1628
    T: ?Sized + Serialize,
56
{
57
1628
    Options::default().to_string(value)
58
1628
}
59

            
60
/// Serializes `value` in the recommended RON layout in a pretty way.
61
1404
pub fn to_string_pretty<T>(value: &T, config: PrettyConfig) -> Result<String>
62
1404
where
63
1404
    T: ?Sized + Serialize,
64
{
65
1404
    Options::default().to_string_pretty(value, config)
66
1404
}
67

            
68
/// Pretty serializer state
69
struct Pretty {
70
    indent: usize,
71
}
72

            
73
/// Pretty serializer configuration.
74
///
75
/// # Examples
76
///
77
/// ```
78
/// use ron::ser::PrettyConfig;
79
///
80
/// let my_config = PrettyConfig::new()
81
///     .depth_limit(4)
82
///     // definitely superior (okay, just joking)
83
///     .indentor("\t");
84
/// ```
85
#[allow(clippy::struct_excessive_bools)]
86
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
87
#[serde(default)]
88
#[non_exhaustive]
89
pub struct PrettyConfig {
90
    /// Limit the pretty-ness up to the given depth.
91
    pub depth_limit: usize,
92
    /// New line string
93
    pub new_line: Cow<'static, str>,
94
    /// Indentation string
95
    pub indentor: Cow<'static, str>,
96
    /// Separator string
97
    pub separator: Cow<'static, str>,
98
    // Whether to emit struct names
99
    pub struct_names: bool,
100
    /// Separate tuple members with indentation
101
    pub separate_tuple_members: bool,
102
    /// Enumerate array items in comments
103
    pub enumerate_arrays: bool,
104
    /// Enable extensions. Only configures `implicit_some`,
105
    ///  `unwrap_newtypes`, and `unwrap_variant_newtypes` for now.
106
    pub extensions: Extensions,
107
    /// Enable compact arrays, which do not insert new lines and indentation
108
    ///  between the elements of an array
109
    pub compact_arrays: bool,
110
    /// Whether to serialize strings as escaped strings,
111
    ///  or fall back onto raw strings if necessary.
112
    pub escape_strings: bool,
113
    /// Enable compact structs, which do not insert new lines and indentation
114
    ///  between the fields of a struct
115
    pub compact_structs: bool,
116
    /// Enable compact maps, which do not insert new lines and indentation
117
    ///  between the entries of a struct
118
    pub compact_maps: bool,
119
    /// Enable explicit number type suffixes like `1u16`
120
    pub number_suffixes: bool,
121
    /// Additional path-based field metadata to serialize
122
    pub path_meta: Option<path_meta::Field>,
123
    /// Enable compact range syntax, e.g. `0..5` instead of `(start: 0, end: 5)`.
124
    pub compact_ranges: bool,
125
}
126

            
127
impl PrettyConfig {
128
    /// Creates a default [`PrettyConfig`].
129
    #[must_use]
130
5148
    pub fn new() -> Self {
131
5148
        Self::default()
132
5148
    }
133

            
134
    /// Limits the pretty-formatting based on the number of indentations.
135
    /// I.e., with a depth limit of 5, starting with an element of depth
136
    /// (indentation level) 6, everything will be put into the same line,
137
    /// without pretty formatting.
138
    ///
139
    /// Default: [`usize::MAX`]
140
    #[must_use]
141
858
    pub fn depth_limit(mut self, depth_limit: usize) -> Self {
142
858
        self.depth_limit = depth_limit;
143

            
144
858
        self
145
858
    }
146

            
147
    /// Configures the newlines used for serialization.
148
    ///
149
    /// Default: `\r\n` on Windows, `\n` otherwise
150
    #[must_use]
151
48
    pub fn new_line(mut self, new_line: impl Into<Cow<'static, str>>) -> Self {
152
48
        self.new_line = new_line.into();
153

            
154
48
        self
155
48
    }
156

            
157
    /// Configures the string sequence used for indentation.
158
    ///
159
    /// Default: 4 spaces
160
    #[must_use]
161
16
    pub fn indentor(mut self, indentor: impl Into<Cow<'static, str>>) -> Self {
162
16
        self.indentor = indentor.into();
163

            
164
16
        self
165
16
    }
166

            
167
    /// Configures the string sequence used to separate items inline.
168
    ///
169
    /// Default: 1 space
170
    #[must_use]
171
20
    pub fn separator(mut self, separator: impl Into<Cow<'static, str>>) -> Self {
172
20
        self.separator = separator.into();
173

            
174
20
        self
175
20
    }
176

            
177
    /// Configures whether to emit struct names.
178
    ///
179
    /// See also [`Extensions::EXPLICIT_STRUCT_NAMES`] for the extension equivalent.
180
    ///
181
    /// Default: `false`
182
    #[must_use]
183
4438
    pub fn struct_names(mut self, struct_names: bool) -> Self {
184
4438
        self.struct_names = struct_names;
185

            
186
4438
        self
187
4438
    }
188

            
189
    /// Configures whether tuples are single- or multi-line.
190
    /// If set to `true`, tuples will have their fields indented and in new
191
    /// lines. If set to `false`, tuples will be serialized without any
192
    /// newlines or indentations.
193
    ///
194
    /// Default: `false`
195
    #[must_use]
196
1430
    pub fn separate_tuple_members(mut self, separate_tuple_members: bool) -> Self {
197
1430
        self.separate_tuple_members = separate_tuple_members;
198

            
199
1430
        self
200
1430
    }
201

            
202
    /// Configures whether a comment shall be added to every array element,
203
    /// indicating the index.
204
    ///
205
    /// Default: `false`
206
    #[must_use]
207
2002
    pub fn enumerate_arrays(mut self, enumerate_arrays: bool) -> Self {
208
2002
        self.enumerate_arrays = enumerate_arrays;
209

            
210
2002
        self
211
2002
    }
212

            
213
    /// Configures whether every array should be a single line (`true`)
214
    /// or a multi line one (`false`).
215
    ///
216
    /// When `false`, `["a","b"]` will serialize to
217
    /// ```
218
    /// [
219
    ///   "a",
220
    ///   "b",
221
    /// ]
222
    /// # ;
223
    /// ```
224
    /// When `true`, `["a","b"]` will instead serialize to
225
    /// ```
226
    /// ["a","b"]
227
    /// # ;
228
    /// ```
229
    ///
230
    /// Default: `false`
231
    #[must_use]
232
1144
    pub fn compact_arrays(mut self, compact_arrays: bool) -> Self {
233
1144
        self.compact_arrays = compact_arrays;
234

            
235
1144
        self
236
1144
    }
237

            
238
    /// Configures extensions
239
    ///
240
    /// Default: [`Extensions::empty()`]
241
    #[must_use]
242
12298
    pub fn extensions(mut self, extensions: Extensions) -> Self {
243
12298
        self.extensions = extensions;
244

            
245
12298
        self
246
12298
    }
247

            
248
    /// Configures whether strings should be serialized using escapes (true)
249
    /// or fall back to raw strings if the string contains a `"` (false).
250
    ///
251
    /// When `true`, `"a\nb"` will serialize to
252
    /// ```
253
    /// "a\nb"
254
    /// # ;
255
    /// ```
256
    /// When `false`, `"a\nb"` will instead serialize to
257
    /// ```
258
    /// "a
259
    /// b"
260
    /// # ;
261
    /// ```
262
    ///
263
    /// Default: `true`
264
    #[must_use]
265
4290
    pub fn escape_strings(mut self, escape_strings: bool) -> Self {
266
4290
        self.escape_strings = escape_strings;
267

            
268
4290
        self
269
4290
    }
270

            
271
    /// Configures whether every struct should be a single line (`true`)
272
    /// or a multi line one (`false`).
273
    ///
274
    /// When `false`, `Struct { a: 4, b: 2 }` will serialize to
275
    /// ```ignore
276
    /// Struct(
277
    ///     a: 4,
278
    ///     b: 2,
279
    /// )
280
    /// # ;
281
    /// ```
282
    /// When `true`, `Struct { a: 4, b: 2 }` will instead serialize to
283
    /// ```ignore
284
    /// Struct(a: 4, b: 2)
285
    /// # ;
286
    /// ```
287
    ///
288
    /// Default: `false`
289
    #[must_use]
290
1864
    pub fn compact_structs(mut self, compact_structs: bool) -> Self {
291
1864
        self.compact_structs = compact_structs;
292

            
293
1864
        self
294
1864
    }
295

            
296
    /// Configures whether every map should be a single line (`true`)
297
    /// or a multi line one (`false`).
298
    ///
299
    /// When `false`, a map with entries `{ "a": 4, "b": 2 }` will serialize to
300
    /// ```ignore
301
    /// {
302
    ///     "a": 4,
303
    ///     "b": 2,
304
    /// }
305
    /// # ;
306
    /// ```
307
    /// When `true`, a map with entries `{ "a": 4, "b": 2 }` will instead
308
    /// serialize to
309
    /// ```ignore
310
    /// {"a": 4, "b": 2}
311
    /// # ;
312
    /// ```
313
    ///
314
    /// Default: `false`
315
    #[must_use]
316
286
    pub fn compact_maps(mut self, compact_maps: bool) -> Self {
317
286
        self.compact_maps = compact_maps;
318

            
319
286
        self
320
286
    }
321

            
322
    /// Configures whether numbers should be printed without (`false`) or
323
    /// with (`true`) their explicit type suffixes.
324
    ///
325
    /// When `false`, the integer `12345u16` will serialize to
326
    /// ```ignore
327
    /// 12345
328
    /// # ;
329
    /// ```
330
    /// and the float `12345.6789f64` will serialize to
331
    /// ```ignore
332
    /// 12345.6789
333
    /// # ;
334
    /// ```
335
    /// When `true`, the integer `12345u16` will serialize to
336
    /// ```ignore
337
    /// 12345u16
338
    /// # ;
339
    /// ```
340
    /// and the float `12345.6789f64` will serialize to
341
    /// ```ignore
342
    /// 12345.6789f64
343
    /// # ;
344
    /// ```
345
    ///
346
    /// Default: `false`
347
    #[must_use]
348
17160
    pub fn number_suffixes(mut self, number_suffixes: bool) -> Self {
349
17160
        self.number_suffixes = number_suffixes;
350

            
351
17160
        self
352
17160
    }
353

            
354
    /// Configures whether ranges should be serialized using compact syntax (`true`)
355
    /// or as regular structs (`false`).
356
    ///
357
    /// Note: compact range syntax is only used when the range bounds are numeric
358
    /// values. If the bounds are non-numeric, the range will fall back to the
359
    /// regular struct representation regardless of this setting.
360
    ///
361
    /// When `false`, `0..5` will serialize to
362
    /// ```ignore
363
    /// (
364
    ///     start: 0,
365
    ///     end: 5,
366
    /// )
367
    /// # ;
368
    /// ```
369
    /// When `true`, `0..5` will instead serialize to
370
    /// ```ignore
371
    /// 0..5
372
    /// # ;
373
    /// ```
374
    /// and `1..=3` will serialize to
375
    /// ```ignore
376
    /// 1..=3
377
    /// # ;
378
    /// ```
379
    ///
380
    /// Default: `false`
381
    #[must_use]
382
858
    pub fn compact_ranges(mut self, compact_ranges: bool) -> Self {
383
858
        self.compact_ranges = compact_ranges;
384
858
        self
385
858
    }
386
}
387

            
388
impl Default for PrettyConfig {
389
72220
    fn default() -> Self {
390
        PrettyConfig {
391
            depth_limit: usize::MAX,
392
72220
            new_line: if cfg!(not(target_os = "windows")) {
393
72220
                Cow::Borrowed("\n")
394
            } else {
395
                Cow::Borrowed("\r\n") // GRCOV_EXCL_LINE
396
            },
397
72220
            indentor: Cow::Borrowed("    "),
398
72220
            separator: Cow::Borrowed(" "),
399
            struct_names: false,
400
            separate_tuple_members: false,
401
            enumerate_arrays: false,
402
72220
            extensions: Extensions::empty(),
403
            compact_arrays: false,
404
            escape_strings: true,
405
            compact_structs: false,
406
            compact_maps: false,
407
            number_suffixes: false,
408
72220
            path_meta: None,
409
            compact_ranges: false,
410
        }
411
72220
    }
412
}
413

            
414
/// The RON serializer.
415
///
416
/// You can just use [`to_string`] for deserializing a value.
417
/// If you want it pretty-printed, take a look at [`to_string_pretty`].
418
pub struct Serializer<W: fmt::Write> {
419
    output: W,
420
    pretty: Option<(PrettyConfig, Pretty)>,
421
    default_extensions: Extensions,
422
    is_empty: Option<bool>,
423
    newtype_variant: bool,
424
    recursion_limit: Option<usize>,
425
    // Tracks the number of opened implicit `Some`s, set to 0 on backtracking
426
    implicit_some_depth: usize,
427
}
428

            
429
2176
fn indent<W: fmt::Write>(output: &mut W, config: &PrettyConfig, pretty: &Pretty) -> fmt::Result {
430
2176
    if pretty.indent <= config.depth_limit {
431
2128
        for _ in 0..pretty.indent {
432
3024
            output.write_str(&config.indentor)?;
433
        }
434
48
    }
435
2176
    Ok(())
436
2176
}
437

            
438
impl<W: fmt::Write> Serializer<W> {
439
    /// Creates a new [`Serializer`].
440
    ///
441
    /// Most of the time you can just use [`to_string`] or
442
    /// [`to_string_pretty`].
443
12
    pub fn new(writer: W, config: Option<PrettyConfig>) -> Result<Self> {
444
12
        Self::with_options(writer, config, &Options::default())
445
12
    }
446

            
447
    /// Creates a new [`Serializer`].
448
    ///
449
    /// Most of the time you can just use [`to_string`] or
450
    /// [`to_string_pretty`].
451
3848
    pub fn with_options(
452
3848
        mut writer: W,
453
3848
        config: Option<PrettyConfig>,
454
3848
        options: &Options,
455
3848
    ) -> Result<Self> {
456
3848
        if let Some(conf) = &config {
457
1768
            if !conf.new_line.chars().all(is_whitespace_char) {
458
4
                return Err(Error::Message(String::from(
459
4
                    "Invalid non-whitespace `PrettyConfig::new_line`",
460
4
                )));
461
1764
            }
462
1764
            if !conf.indentor.chars().all(is_whitespace_char) {
463
4
                return Err(Error::Message(String::from(
464
4
                    "Invalid non-whitespace `PrettyConfig::indentor`",
465
4
                )));
466
1760
            }
467
1760
            if !conf.separator.chars().all(is_whitespace_char) {
468
4
                return Err(Error::Message(String::from(
469
4
                    "Invalid non-whitespace `PrettyConfig::separator`",
470
4
                )));
471
1756
            }
472

            
473
1756
            let non_default_extensions = !options.default_extensions;
474

            
475
1768
            for (extension_name, _) in (non_default_extensions & conf.extensions).iter_names() {
476
184
                write!(writer, "#![enable({})]", extension_name.to_lowercase())?;
477
184
                writer.write_str(&conf.new_line)?;
478
            }
479
2080
        };
480
        Ok(Serializer {
481
3836
            output: writer,
482
3836
            pretty: config.map(|conf| (conf, Pretty { indent: 0 })),
483
3836
            default_extensions: options.default_extensions,
484
3836
            is_empty: None,
485
            newtype_variant: false,
486
3836
            recursion_limit: options.recursion_limit,
487
            implicit_some_depth: 0,
488
        })
489
3848
    }
490

            
491
    /// Unwrap the `Writer` from the `Serializer`.
492
    #[inline]
493
    pub fn into_inner(self) -> W {
494
        self.output
495
    }
496

            
497
2332
    fn separate_tuple_members(&self) -> bool {
498
2332
        self.pretty
499
2332
            .as_ref()
500
2332
            .map_or(false, |(ref config, _)| config.separate_tuple_members)
501
2332
    }
502

            
503
1088
    fn compact_arrays(&self) -> bool {
504
1088
        self.pretty
505
1088
            .as_ref()
506
1088
            .map_or(false, |(ref config, _)| config.compact_arrays)
507
1088
    }
508

            
509
4078
    fn compact_structs(&self) -> bool {
510
4078
        self.pretty
511
4078
            .as_ref()
512
4078
            .map_or(false, |(ref config, _)| config.compact_structs)
513
4078
    }
514

            
515
1596
    fn compact_maps(&self) -> bool {
516
1596
        self.pretty
517
1596
            .as_ref()
518
1596
            .map_or(false, |(ref config, _)| config.compact_maps)
519
1596
    }
520

            
521
3726
    fn number_suffixes(&self) -> bool {
522
3726
        self.pretty
523
3726
            .as_ref()
524
3726
            .map_or(false, |(ref config, _)| config.number_suffixes)
525
3726
    }
526

            
527
1220
    fn compact_ranges(&self) -> bool {
528
1220
        self.pretty
529
1220
            .as_ref()
530
1220
            .map_or(false, |(ref config, _)| config.compact_ranges)
531
1220
    }
532

            
533
3016
    fn extensions(&self) -> Extensions {
534
3016
        self.default_extensions
535
3016
            | self
536
3016
                .pretty
537
3016
                .as_ref()
538
3016
                .map_or(Extensions::empty(), |(ref config, _)| config.extensions)
539
3016
    }
540

            
541
1352
    fn escape_strings(&self) -> bool {
542
1352
        self.pretty
543
1352
            .as_ref()
544
1352
            .map_or(true, |(ref config, _)| config.escape_strings)
545
1352
    }
546

            
547
1748
    fn start_indent(&mut self) -> Result<()> {
548
1748
        if let Some((ref config, ref mut pretty)) = self.pretty {
549
1032
            pretty.indent += 1;
550
1032
            if pretty.indent <= config.depth_limit {
551
1008
                let is_empty = self.is_empty.unwrap_or(false);
552

            
553
1008
                if !is_empty {
554
960
                    self.output.write_str(&config.new_line)?;
555
48
                }
556
24
            }
557
716
        }
558
1748
        Ok(())
559
1748
    }
560

            
561
1452
    fn indent(&mut self) -> fmt::Result {
562
1452
        if let Some((ref config, ref pretty)) = self.pretty {
563
1124
            indent(&mut self.output, config, pretty)?;
564
328
        }
565
1452
        Ok(())
566
1452
    }
567

            
568
1458
    fn end_indent(&mut self) -> fmt::Result {
569
1458
        if let Some((ref config, ref mut pretty)) = self.pretty {
570
1010
            if pretty.indent <= config.depth_limit {
571
986
                let is_empty = self.is_empty.unwrap_or(false);
572

            
573
986
                if !is_empty {
574
938
                    for _ in 1..pretty.indent {
575
458
                        self.output.write_str(&config.indentor)?;
576
                    }
577
48
                }
578
24
            }
579
1010
            pretty.indent -= 1;
580

            
581
1010
            self.is_empty = None;
582
448
        }
583
1458
        Ok(())
584
1458
    }
585

            
586
1160
    fn serialize_escaped_str(&mut self, value: &str) -> fmt::Result {
587
1160
        self.output.write_char('"')?;
588
1160
        let mut scalar = [0u8; 4];
589
7956
        for c in value.chars().flat_map(char::escape_debug) {
590
7944
            self.output.write_str(c.encode_utf8(&mut scalar))?;
591
        }
592
1160
        self.output.write_char('"')?;
593
1160
        Ok(())
594
1160
    }
595

            
596
32
    fn serialize_unescaped_or_raw_str(&mut self, value: &str) -> fmt::Result {
597
32
        if value.contains('"') || value.contains('\\') {
598
20
            let (_, num_consecutive_hashes) =
599
44
                value.chars().fold((0, 0), |(count, max), c| match c {
600
24
                    '#' => (count + 1, max.max(count + 1)),
601
20
                    _ => (0_usize, max),
602
44
                });
603
20
            let hashes: String = "#".repeat(num_consecutive_hashes + 1);
604
20
            self.output.write_char('r')?;
605
20
            self.output.write_str(&hashes)?;
606
20
            self.output.write_char('"')?;
607
20
            self.output.write_str(value)?;
608
20
            self.output.write_char('"')?;
609
20
            self.output.write_str(&hashes)?;
610
        } else {
611
12
            self.output.write_char('"')?;
612
12
            self.output.write_str(value)?;
613
12
            self.output.write_char('"')?;
614
        }
615
32
        Ok(())
616
32
    }
617

            
618
136
    fn serialize_escaped_byte_str(&mut self, value: &[u8]) -> fmt::Result {
619
136
        self.output.write_str("b\"")?;
620
5356
        for c in value.iter().flat_map(|c| core::ascii::escape_default(*c)) {
621
5356
            self.output.write_char(char::from(c))?;
622
        }
623
136
        self.output.write_char('"')?;
624
136
        Ok(())
625
136
    }
626

            
627
24
    fn serialize_unescaped_or_raw_byte_str(&mut self, value: &str) -> fmt::Result {
628
24
        if value.contains('"') || value.contains('\\') {
629
12
            let (_, num_consecutive_hashes) =
630
16
                value.chars().fold((0, 0), |(count, max), c| match c {
631
4
                    '#' => (count + 1, max.max(count + 1)),
632
12
                    _ => (0_usize, max),
633
16
                });
634
12
            let hashes: String = "#".repeat(num_consecutive_hashes + 1);
635
12
            self.output.write_str("br")?;
636
12
            self.output.write_str(&hashes)?;
637
12
            self.output.write_char('"')?;
638
12
            self.output.write_str(value)?;
639
12
            self.output.write_char('"')?;
640
12
            self.output.write_str(&hashes)?;
641
        } else {
642
12
            self.output.write_str("b\"")?;
643
12
            self.output.write_str(value)?;
644
12
            self.output.write_char('"')?;
645
        }
646
24
        Ok(())
647
24
    }
648

            
649
1640
    fn serialize_sint(&mut self, value: impl Into<LargeSInt>, suffix: &str) -> Result<()> {
650
        // TODO optimize
651
1640
        write!(self.output, "{}", value.into())?;
652

            
653
1640
        if self.number_suffixes() {
654
72
            write!(self.output, "{}", suffix)?;
655
1568
        }
656

            
657
1640
        Ok(())
658
1640
    }
659

            
660
1314
    fn serialize_uint(&mut self, value: impl Into<LargeUInt>, suffix: &str) -> Result<()> {
661
        // TODO optimize
662
1314
        write!(self.output, "{}", value.into())?;
663

            
664
1314
        if self.number_suffixes() {
665
72
            write!(self.output, "{}", suffix)?;
666
1242
        }
667

            
668
1314
        Ok(())
669
1314
    }
670

            
671
2944
    fn write_identifier(&mut self, name: &str) -> Result<()> {
672
2944
        self.validate_identifier(name)?;
673
2912
        let mut chars = name.chars();
674
2912
        if !chars.next().map_or(false, is_ident_first_char)
675
2892
            || !chars.all(is_xid_continue)
676
2848
            || [
677
2848
                "true", "false", "Some", "None", "inf", "inff32", "inff64", "NaN", "NaNf32",
678
2848
                "NaNf64",
679
2848
            ]
680
2848
            .contains(&name)
681
        {
682
104
            self.output.write_str("r#")?;
683
2808
        }
684
2912
        self.output.write_str(name)?;
685
2912
        Ok(())
686
2944
    }
687

            
688
    #[allow(clippy::unused_self)]
689
5136
    fn validate_identifier(&self, name: &str) -> Result<()> {
690
5136
        if name.is_empty() || !name.chars().all(is_ident_raw_char) {
691
60
            return Err(Error::InvalidIdentifier(name.into()));
692
5076
        }
693
5076
        Ok(())
694
5136
    }
695

            
696
    /// Checks if struct names should be emitted
697
    ///
698
    /// Note that when using the `explicit_struct_names` extension, this method will use an OR operation on the extension and the [`PrettyConfig::struct_names`] option. See also [`Extensions::EXPLICIT_STRUCT_NAMES`] for the extension equivalent.
699
1468
    fn struct_names(&self) -> bool {
700
1468
        self.extensions()
701
1468
            .contains(Extensions::EXPLICIT_STRUCT_NAMES)
702
1456
            || self
703
1456
                .pretty
704
1456
                .as_ref()
705
1456
                .map_or(false, |(pc, _)| pc.struct_names)
706
1468
    }
707
}
708

            
709
macro_rules! guard_recursion {
710
    ($self:expr => $expr:expr) => {{
711
        if let Some(limit) = &mut $self.recursion_limit {
712
            if let Some(new_limit) = limit.checked_sub(1) {
713
                *limit = new_limit;
714
            } else {
715
                return Err(Error::ExceededRecursionLimit);
716
            }
717
        }
718

            
719
        let result = $expr;
720

            
721
        if let Some(limit) = &mut $self.recursion_limit {
722
            *limit = limit.saturating_add(1);
723
        }
724

            
725
        result
726
    }};
727
}
728

            
729
impl<'a, W: fmt::Write> ser::Serializer for &'a mut Serializer<W> {
730
    type Error = Error;
731
    type Ok = ();
732
    type SerializeMap = Compound<'a, W>;
733
    type SerializeSeq = Compound<'a, W>;
734
    type SerializeStruct = Compound<'a, W>;
735
    type SerializeStructVariant = Compound<'a, W>;
736
    type SerializeTuple = Compound<'a, W>;
737
    type SerializeTupleStruct = Compound<'a, W>;
738
    type SerializeTupleVariant = Compound<'a, W>;
739

            
740
368
    fn serialize_bool(self, v: bool) -> Result<()> {
741
368
        self.output.write_str(if v { "true" } else { "false" })?;
742
368
        Ok(())
743
368
    }
744

            
745
136
    fn serialize_i8(self, v: i8) -> Result<()> {
746
136
        self.serialize_sint(v, "i8")
747
136
    }
748

            
749
84
    fn serialize_i16(self, v: i16) -> Result<()> {
750
84
        self.serialize_sint(v, "i16")
751
84
    }
752

            
753
1276
    fn serialize_i32(self, v: i32) -> Result<()> {
754
1276
        self.serialize_sint(v, "i32")
755
1276
    }
756

            
757
92
    fn serialize_i64(self, v: i64) -> Result<()> {
758
92
        self.serialize_sint(v, "i64")
759
92
    }
760

            
761
    #[cfg(feature = "integer128")]
762
52
    fn serialize_i128(self, v: i128) -> Result<()> {
763
52
        self.serialize_sint(v, "i128")
764
52
    }
765

            
766
612
    fn serialize_u8(self, v: u8) -> Result<()> {
767
612
        self.serialize_uint(v, "u8")
768
612
    }
769

            
770
84
    fn serialize_u16(self, v: u16) -> Result<()> {
771
84
        self.serialize_uint(v, "u16")
772
84
    }
773

            
774
448
    fn serialize_u32(self, v: u32) -> Result<()> {
775
448
        self.serialize_uint(v, "u32")
776
448
    }
777

            
778
120
    fn serialize_u64(self, v: u64) -> Result<()> {
779
120
        self.serialize_uint(v, "u64")
780
120
    }
781

            
782
    #[cfg(feature = "integer128")]
783
50
    fn serialize_u128(self, v: u128) -> Result<()> {
784
50
        self.serialize_uint(v, "u128")
785
50
    }
786

            
787
548
    fn serialize_f32(self, v: f32) -> Result<()> {
788
548
        if v.is_nan() && v.is_sign_negative() {
789
40
            write!(self.output, "-")?;
790
508
        }
791

            
792
548
        write!(self.output, "{}", v)?;
793

            
794
        // Equivalent to v.fract() == 0.0
795
        // See: https://docs.rs/num-traits/0.2.19/src/num_traits/float.rs.html#459-465
796
548
        if v % 1. == 0.0 {
797
260
            write!(self.output, ".0")?;
798
288
        }
799

            
800
548
        if self.number_suffixes() {
801
48
            write!(self.output, "f32")?;
802
500
        }
803

            
804
548
        Ok(())
805
548
    }
806

            
807
224
    fn serialize_f64(self, v: f64) -> Result<()> {
808
224
        if v.is_nan() && v.is_sign_negative() {
809
8
            write!(self.output, "-")?;
810
216
        }
811

            
812
224
        write!(self.output, "{}", v)?;
813

            
814
        // Equivalent to v.fract() == 0.0
815
        // See: https://docs.rs/num-traits/0.2.19/src/num_traits/float.rs.html#459-465
816
224
        if v % 1. == 0.0 {
817
124
            write!(self.output, ".0")?;
818
100
        }
819

            
820
224
        if self.number_suffixes() {
821
48
            write!(self.output, "f64")?;
822
176
        }
823

            
824
224
        Ok(())
825
224
    }
826

            
827
672
    fn serialize_char(self, v: char) -> Result<()> {
828
672
        self.output.write_char('\'')?;
829
672
        if v == '\\' || v == '\'' {
830
32
            self.output.write_char('\\')?;
831
640
        }
832
672
        write!(self.output, "{}", v)?;
833
672
        self.output.write_char('\'')?;
834
672
        Ok(())
835
672
    }
836

            
837
1192
    fn serialize_str(self, v: &str) -> Result<()> {
838
1192
        if self.escape_strings() {
839
1160
            self.serialize_escaped_str(v)?;
840
        } else {
841
32
            self.serialize_unescaped_or_raw_str(v)?;
842
        }
843

            
844
1192
        Ok(())
845
1192
    }
846

            
847
160
    fn serialize_bytes(self, v: &[u8]) -> Result<()> {
848
        // We need to fall back to escaping if the byte string would be invalid UTF-8
849
160
        if !self.escape_strings() {
850
28
            if let Ok(v) = core::str::from_utf8(v) {
851
24
                return self
852
24
                    .serialize_unescaped_or_raw_byte_str(v)
853
24
                    .map_err(Error::from);
854
4
            }
855
132
        }
856

            
857
136
        self.serialize_escaped_byte_str(v)?;
858

            
859
136
        Ok(())
860
160
    }
861

            
862
76
    fn serialize_none(self) -> Result<()> {
863
        // We no longer need to keep track of the depth
864
76
        let implicit_some_depth = self.implicit_some_depth;
865
76
        self.implicit_some_depth = 0;
866

            
867
76
        for _ in 0..implicit_some_depth {
868
12
            self.output.write_str("Some(")?;
869
        }
870
76
        self.output.write_str("None")?;
871
76
        for _ in 0..implicit_some_depth {
872
12
            self.output.write_char(')')?;
873
        }
874

            
875
76
        Ok(())
876
76
    }
877

            
878
532
    fn serialize_some<T>(self, value: &T) -> Result<()>
879
532
    where
880
532
        T: ?Sized + Serialize,
881
    {
882
532
        let implicit_some = self.extensions().contains(Extensions::IMPLICIT_SOME);
883
532
        if implicit_some {
884
120
            self.implicit_some_depth += 1;
885
120
        } else {
886
412
            self.newtype_variant = self
887
412
                .extensions()
888
412
                .contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
889
412
            self.output.write_str("Some(")?;
890
        }
891
532
        guard_recursion! { self => value.serialize(&mut *self)? };
892
276
        if implicit_some {
893
120
            self.implicit_some_depth = 0;
894
120
        } else {
895
156
            self.output.write_char(')')?;
896
156
            self.newtype_variant = false;
897
        }
898

            
899
276
        Ok(())
900
532
    }
901

            
902
228
    fn serialize_unit(self) -> Result<()> {
903
228
        if !self.newtype_variant {
904
224
            self.output.write_str("()")?;
905
4
        }
906

            
907
228
        Ok(())
908
228
    }
909

            
910
96
    fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
911
96
        if self.compact_ranges() && name == "RangeFull" {
912
4
            self.output.write_str("..")?;
913
4
            return Ok(());
914
92
        }
915

            
916
92
        if self.struct_names() && !self.newtype_variant {
917
16
            self.write_identifier(name)?;
918

            
919
8
            Ok(())
920
        } else {
921
76
            self.validate_identifier(name)?;
922
76
            self.serialize_unit()
923
        }
924
96
    }
925

            
926
240
    fn serialize_unit_variant(
927
240
        self,
928
240
        name: &'static str,
929
240
        _variant_index: u32,
930
240
        variant: &'static str,
931
240
    ) -> Result<()> {
932
240
        self.validate_identifier(name)?;
933
236
        self.write_identifier(variant)?;
934

            
935
232
        Ok(())
936
240
    }
937

            
938
444
    fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<()>
939
444
    where
940
444
        T: ?Sized + Serialize,
941
    {
942
444
        if name == crate::value::raw::RAW_VALUE_TOKEN {
943
196
            let implicit_some_depth = self.implicit_some_depth;
944
196
            self.implicit_some_depth = 0;
945

            
946
196
            for _ in 0..implicit_some_depth {
947
8
                self.output.write_str("Some(")?;
948
            }
949

            
950
196
            guard_recursion! { self => value.serialize(raw::Serializer::new(self)) }?;
951

            
952
84
            for _ in 0..implicit_some_depth {
953
8
                self.output.write_char(')')?;
954
            }
955

            
956
84
            return Ok(());
957
248
        }
958

            
959
248
        if self.extensions().contains(Extensions::UNWRAP_NEWTYPES) || self.newtype_variant {
960
40
            self.newtype_variant = false;
961

            
962
40
            self.validate_identifier(name)?;
963

            
964
40
            return guard_recursion! { self => value.serialize(&mut *self) };
965
208
        }
966

            
967
208
        if self.struct_names() {
968
12
            self.write_identifier(name)?;
969
        } else {
970
196
            self.validate_identifier(name)?;
971
        }
972

            
973
204
        self.implicit_some_depth = 0;
974

            
975
204
        self.output.write_char('(')?;
976
204
        guard_recursion! { self => value.serialize(&mut *self)? };
977
204
        self.output.write_char(')')?;
978

            
979
204
        Ok(())
980
444
    }
981

            
982
364
    fn serialize_newtype_variant<T>(
983
364
        self,
984
364
        name: &'static str,
985
364
        _variant_index: u32,
986
364
        variant: &'static str,
987
364
        value: &T,
988
364
    ) -> Result<()>
989
364
    where
990
364
        T: ?Sized + Serialize,
991
    {
992
364
        self.validate_identifier(name)?;
993
360
        self.write_identifier(variant)?;
994
356
        self.output.write_char('(')?;
995

            
996
356
        self.newtype_variant = self
997
356
            .extensions()
998
356
            .contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
999
356
        self.implicit_some_depth = 0;
356
        guard_recursion! { self => value.serialize(&mut *self)? };
354
        self.newtype_variant = false;
354
        self.output.write_char(')')?;
354
        Ok(())
364
    }
240
    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
240
        self.newtype_variant = false;
240
        self.implicit_some_depth = 0;
240
        self.output.write_char('[')?;
240
        if !self.compact_arrays() {
212
            if let Some(len) = len {
212
                self.is_empty = Some(len == 0);
212
            }
212
            self.start_indent()?;
28
        }
240
        Ok(Compound::new(self, false))
240
    }
352
    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
352
        let old_newtype_variant = self.newtype_variant;
352
        self.newtype_variant = false;
352
        self.implicit_some_depth = 0;
352
        if !old_newtype_variant {
324
            self.output.write_char('(')?;
28
        }
352
        if self.separate_tuple_members() {
32
            self.is_empty = Some(len == 0);
32
            self.start_indent()?;
320
        }
352
        Ok(Compound::new(self, old_newtype_variant))
352
    }
104
    fn serialize_tuple_struct(
104
        self,
104
        name: &'static str,
104
        len: usize,
104
    ) -> Result<Self::SerializeTupleStruct> {
104
        if self.struct_names() && !self.newtype_variant {
20
            self.write_identifier(name)?;
        } else {
84
            self.validate_identifier(name)?;
        }
100
        self.serialize_tuple(len)
104
    }
100
    fn serialize_tuple_variant(
100
        self,
100
        name: &'static str,
100
        _variant_index: u32,
100
        variant: &'static str,
100
        len: usize,
100
    ) -> Result<Self::SerializeTupleVariant> {
100
        self.newtype_variant = false;
100
        self.implicit_some_depth = 0;
100
        self.validate_identifier(name)?;
96
        self.write_identifier(variant)?;
92
        self.output.write_char('(')?;
92
        if self.separate_tuple_members() {
8
            self.is_empty = Some(len == 0);
8
            self.start_indent()?;
84
        }
92
        Ok(Compound::new(self, false))
100
    }
384
    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
384
        self.newtype_variant = false;
384
        self.implicit_some_depth = 0;
384
        self.output.write_char('{')?;
384
        if !self.compact_maps() {
380
            if let Some(len) = len {
120
                self.is_empty = Some(len == 0);
364
            }
380
            self.start_indent()?;
4
        }
384
        Ok(Compound::new(self, false))
384
    }
1124
    fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
1124
        let old_newtype_variant = self.newtype_variant;
1124
        if self.compact_ranges()
52
            && ((len == 2 && (name == "Range" || name == "RangeInclusive"))
32
                || (len == 1
24
                    && (name == "RangeFrom" || name == "RangeTo" || name == "RangeToInclusive")))
        {
44
            self.newtype_variant = false;
44
            self.implicit_some_depth = 0;
44
            return Ok(Compound::new_range(
44
                self,
44
                if name == "RangeInclusive" {
8
                    RangeKind::RangeInclusive
36
                } else if name == "RangeFrom" {
8
                    RangeKind::RangeFrom
28
                } else if name == "RangeTo" {
8
                    RangeKind::RangeTo
20
                } else if name == "RangeToInclusive" {
8
                    RangeKind::RangeToInclusive
                } else {
12
                    RangeKind::Range
                },
            ));
1080
        }
1080
        self.newtype_variant = false;
1080
        self.implicit_some_depth = 0;
1080
        if old_newtype_variant {
16
            self.validate_identifier(name)?;
        } else {
1064
            if self.struct_names() {
72
                self.write_identifier(name)?;
            } else {
992
                self.validate_identifier(name)?;
            }
1060
            self.output.write_char('(')?;
        }
1076
        if !self.compact_structs() {
1048
            self.is_empty = Some(len == 0);
1048
            self.start_indent()?;
28
        }
1076
        Ok(Compound::new(self, old_newtype_variant))
1124
    }
84
    fn serialize_struct_variant(
84
        self,
84
        name: &'static str,
84
        _variant_index: u32,
84
        variant: &'static str,
84
        len: usize,
84
    ) -> Result<Self::SerializeStructVariant> {
84
        self.newtype_variant = false;
84
        self.implicit_some_depth = 0;
84
        self.validate_identifier(name)?;
80
        self.write_identifier(variant)?;
76
        self.output.write_char('(')?;
76
        if !self.compact_structs() {
68
            self.is_empty = Some(len == 0);
68
            self.start_indent()?;
8
        }
76
        Ok(Compound::new(self, false))
84
    }
}
enum State {
    First,
    Rest,
}
#[doc(hidden)]
pub struct Compound<'a, W: fmt::Write> {
    ser: &'a mut Serializer<W>,
    state: State,
    newtype_variant: bool,
    sequence_index: usize,
    range: Option<RangeCompound>,
}
impl<'a, W: fmt::Write> Compound<'a, W> {
2220
    fn new(ser: &'a mut Serializer<W>, newtype_variant: bool) -> Self {
2220
        Compound {
2220
            ser,
2220
            state: State::First,
2220
            newtype_variant,
2220
            sequence_index: 0,
2220
            range: None,
2220
        }
2220
    }
44
    fn new_range(ser: &'a mut Serializer<W>, kind: RangeKind) -> Self {
44
        Compound {
44
            ser,
44
            state: State::First,
44
            newtype_variant: false,
44
            sequence_index: 0,
44
            range: Some(RangeCompound::new(kind)),
44
        }
44
    }
}
enum RangeKind {
    Range,
    RangeInclusive,
    RangeFrom,
    RangeTo,
    RangeToInclusive,
}
struct RangeCompound {
    kind: RangeKind,
    // Stores (field_key, number) so the actual key is preserved through fallback replay.
    first: Option<(&'static str, crate::value::Number)>,
    second: Option<(&'static str, crate::value::Number)>,
    fallback: bool,
}
impl RangeCompound {
3146
    fn new(kind: RangeKind) -> Self {
3146
        RangeCompound {
3146
            kind,
3146
            first: None,
3146
            second: None,
3146
            fallback: false,
3146
        }
3146
    }
    /// Try to serialize `value` as a number into a String buffer.
    /// Returns `Some(s)` if numeric, `None` otherwise.
60
    fn try_serialize_number<T>(value: &T) -> Option<crate::value::Number>
60
    where
60
        T: ?Sized + Serialize,
    {
60
        value.serialize(crate::value::NumberSerializer).ok()
60
    }
}
impl<'a, W: fmt::Write> Drop for Compound<'a, W> {
2264
    fn drop(&mut self) {
2264
        if let Some(limit) = &mut self.ser.recursion_limit {
2252
            *limit = limit.saturating_add(1);
2252
        }
2264
    }
}
impl<'a, W: fmt::Write> ser::SerializeSeq for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
608
    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
608
    where
608
        T: ?Sized + Serialize,
    {
608
        if let State::First = self.state {
204
            self.state = State::Rest;
204
        } else {
404
            self.ser.output.write_char(',')?;
404
            if let Some((ref config, ref mut pretty)) = self.ser.pretty {
224
                if pretty.indent <= config.depth_limit && !config.compact_arrays {
176
                    self.ser.output.write_str(&config.new_line)?;
                } else {
48
                    self.ser.output.write_str(&config.separator)?;
                }
180
            }
        }
608
        if !self.ser.compact_arrays() {
544
            self.ser.indent()?;
64
        }
608
        if let Some((ref mut config, ref mut pretty)) = self.ser.pretty {
348
            if pretty.indent <= config.depth_limit && config.enumerate_arrays {
72
                write!(self.ser.output, "/*[{}]*/ ", self.sequence_index)?;
72
                self.sequence_index += 1;
276
            }
260
        }
608
        guard_recursion! { self.ser => value.serialize(&mut *self.ser)? };
608
        Ok(())
608
    }
240
    fn end(self) -> Result<()> {
240
        if let State::Rest = self.state {
204
            if let Some((ref config, ref mut pretty)) = self.ser.pretty {
124
                if pretty.indent <= config.depth_limit && !config.compact_arrays {
96
                    self.ser.output.write_char(',')?;
96
                    self.ser.output.write_str(&config.new_line)?;
28
                }
80
            }
36
        }
240
        if !self.ser.compact_arrays() {
212
            self.ser.end_indent()?;
28
        }
        // seq always disables `self.newtype_variant`
240
        self.ser.output.write_char(']')?;
240
        Ok(())
240
    }
}
impl<'a, W: fmt::Write> ser::SerializeTuple for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
952
    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
952
    where
952
        T: ?Sized + Serialize,
    {
952
        if let State::First = self.state {
396
            self.state = State::Rest;
396
        } else {
556
            self.ser.output.write_char(',')?;
556
            if let Some((ref config, ref pretty)) = self.ser.pretty {
292
                if pretty.indent <= config.depth_limit && self.ser.separate_tuple_members() {
32
                    self.ser.output.write_str(&config.new_line)?;
                } else {
260
                    self.ser.output.write_str(&config.separator)?;
                }
264
            }
        }
952
        if self.ser.separate_tuple_members() {
84
            self.ser.indent()?;
868
        }
952
        guard_recursion! { self.ser => value.serialize(&mut *self.ser)? };
940
        Ok(())
952
    }
432
    fn end(self) -> Result<()> {
432
        if let State::Rest = self.state {
384
            if let Some((ref config, ref pretty)) = self.ser.pretty {
224
                if self.ser.separate_tuple_members() && pretty.indent <= config.depth_limit {
28
                    self.ser.output.write_char(',')?;
28
                    self.ser.output.write_str(&config.new_line)?;
196
                }
160
            }
48
        }
432
        if self.ser.separate_tuple_members() {
40
            self.ser.end_indent()?;
392
        }
432
        if !self.newtype_variant {
404
            self.ser.output.write_char(')')?;
28
        }
432
        Ok(())
432
    }
}
// Same thing but for tuple structs.
impl<'a, W: fmt::Write> ser::SerializeTupleStruct for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
168
    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
168
    where
168
        T: ?Sized + Serialize,
    {
168
        ser::SerializeTuple::serialize_element(self, value)
168
    }
100
    fn end(self) -> Result<()> {
100
        ser::SerializeTuple::end(self)
100
    }
}
impl<'a, W: fmt::Write> ser::SerializeTupleVariant for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
156
    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
156
    where
156
        T: ?Sized + Serialize,
    {
156
        ser::SerializeTuple::serialize_element(self, value)
156
    }
92
    fn end(self) -> Result<()> {
92
        ser::SerializeTuple::end(self)
92
    }
}
impl<'a, W: fmt::Write> ser::SerializeMap for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
832
    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
832
    where
832
        T: ?Sized + Serialize,
    {
832
        if let State::First = self.state {
360
            self.state = State::Rest;
360
        } else {
472
            self.ser.output.write_char(',')?;
472
            if let Some((ref config, ref pretty)) = self.ser.pretty {
432
                if pretty.indent <= config.depth_limit && !config.compact_maps {
428
                    self.ser.output.write_str(&config.new_line)?;
                } else {
4
                    self.ser.output.write_str(&config.separator)?;
                }
40
            }
        }
832
        if !self.ser.compact_maps() {
824
            self.ser.indent()?;
8
        }
832
        guard_recursion! { self.ser => key.serialize(&mut *self.ser) }
832
    }
832
    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
832
    where
832
        T: ?Sized + Serialize,
    {
832
        self.ser.output.write_char(':')?;
832
        if let Some((ref config, _)) = self.ser.pretty {
764
            self.ser.output.write_str(&config.separator)?;
68
        }
832
        guard_recursion! { self.ser => value.serialize(&mut *self.ser)? };
828
        Ok(())
832
    }
380
    fn end(self) -> Result<()> {
380
        if let State::Rest = self.state {
356
            if let Some((ref config, ref pretty)) = self.ser.pretty {
328
                if pretty.indent <= config.depth_limit && !config.compact_maps {
320
                    self.ser.output.write_char(',')?;
320
                    self.ser.output.write_str(&config.new_line)?;
8
                }
28
            }
24
        }
380
        if !self.ser.compact_maps() {
376
            self.ser.end_indent()?;
4
        }
        // map always disables `self.newtype_variant`
380
        self.ser.output.write_char('}')?;
380
        Ok(())
380
    }
}
impl<'a, W: fmt::Write> ser::SerializeStruct for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
    #[allow(clippy::too_many_lines)]
2108
    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
2108
    where
2108
        T: ?Sized + Serialize,
    {
2108
        if let Some(ref mut range) = self.range {
64
            if !range.fallback {
60
                match RangeCompound::try_serialize_number(value) {
56
                    Some(s) => {
                        // Validate that the field key matches what this range variant expects,
                        // and that we haven't received more fields than the variant allows.
56
                        let expected_key = match range.kind {
                            RangeKind::RangeTo | RangeKind::RangeToInclusive => {
16
                                if range.first.is_none() {
16
                                    "end"
                                } else {
                                    ""
                                }
                            }
                            _ => {
40
                                if range.first.is_none() {
24
                                    "start"
                                } else {
16
                                    "end"
                                }
                            }
                        };
56
                        let max_fields = match range.kind {
                            RangeKind::RangeFrom
                            | RangeKind::RangeTo
24
                            | RangeKind::RangeToInclusive => 1,
32
                            RangeKind::Range | RangeKind::RangeInclusive => 2,
                        };
56
                        if key != expected_key
56
                            || (range.first.is_some() && max_fields < 2)
56
                            || range.second.is_some()
                        {
                            range.fallback = true;
56
                        } else if range.first.is_none() {
40
                            // Store the actual key alongside the number so it is
40
                            // replayed correctly if we later fall back to struct form.
40
                            range.first = Some((key, s));
40
                        } else {
16
                            range.second = Some((key, s));
16
                        }
56
                        if !range.fallback {
56
                            return Ok(());
                        }
                    }
4
                    None => {
4
                        range.fallback = true;
4
                    }
                }
4
                self.ser.output.write_char('(')?;
4
                if !self.ser.compact_structs() {
                    self.ser.is_empty = Some(false);
                    self.ser.start_indent()?;
4
                }
                // Replay any buffered numeric fields using their actual stored keys.
4
                let buffered: [Option<(&'static str, crate::value::Number)>; 2] =
4
                    [range.first.take(), range.second.take()];
4
                for (bkey, bval) in buffered.into_iter().flatten() {
                    ser::SerializeStruct::serialize_field(self, bkey, &bval)?;
                }
                // fall through to emit the current field normally
4
            }
2044
        }
2052
        let mut restore_field = self.ser.pretty.as_mut().and_then(|(config, _)| {
1080
            config.path_meta.take().map(|mut field| {
60
                if let Some(fields) = field.fields_mut() {
60
                    config.path_meta = fields.remove(key);
60
                }
60
                field
60
            })
1080
        });
2052
        if let State::First = self.state {
1128
            self.state = State::Rest;
1128
        } else {
924
            self.ser.output.write_char(',')?;
924
            if let Some((ref config, ref pretty)) = self.ser.pretty {
536
                if pretty.indent <= config.depth_limit && !config.compact_structs {
472
                    self.ser.output.write_str(&config.new_line)?;
                } else {
64
                    self.ser.output.write_str(&config.separator)?;
                }
388
            }
        }
2052
        if !self.ser.compact_structs() {
1960
            if let Some((ref config, ref pretty)) = self.ser.pretty {
988
                indent(&mut self.ser.output, config, pretty)?;
988
                if let Some(ref field) = config.path_meta {
64
                    for doc_line in field.doc().lines() {
64
                        self.ser.output.write_str("/// ")?;
64
                        self.ser.output.write_str(doc_line)?;
64
                        self.ser.output.write_char('\n')?;
64
                        indent(&mut self.ser.output, config, pretty)?;
                    }
944
                }
972
            }
92
        }
2052
        self.ser.write_identifier(key)?;
2044
        self.ser.output.write_char(':')?;
2044
        if let Some((ref config, _)) = self.ser.pretty {
1080
            self.ser.output.write_str(&config.separator)?;
964
        }
2044
        guard_recursion! { self.ser => value.serialize(&mut *self.ser)? };
1766
        if let Some((ref mut config, _)) = self.ser.pretty {
1062
            core::mem::swap(&mut config.path_meta, &mut restore_field);
1062
            if let Some(ref mut field) = config.path_meta {
60
                if let Some(fields) = field.fields_mut() {
60
                    if let Some(restore_field) = restore_field {
44
                        fields.insert(key, restore_field);
44
                    }
                }
1002
            }
704
        };
1766
        Ok(())
2108
    }
910
    fn end(mut self) -> Result<()> {
910
        if let Some(ref mut range) = self.range {
44
            if !range.fallback {
                // All fields were numeric — emit compact syntax.
40
                let sep = match range.kind {
16
                    RangeKind::RangeInclusive | RangeKind::RangeToInclusive => "..=",
24
                    _ => "..",
                };
40
                match range.kind {
                    RangeKind::RangeFrom => {
8
                        if let Some((_, start)) = range.first.take() {
8
                            start.serialize(&mut *self.ser)?;
                        }
8
                        self.ser.output.write_str("..")?;
                    }
                    RangeKind::RangeTo | RangeKind::RangeToInclusive => {
16
                        self.ser.output.write_str(sep)?;
16
                        if let Some((_, end)) = range.first.take() {
16
                            end.serialize(&mut *self.ser)?;
                        }
                    }
                    RangeKind::Range | RangeKind::RangeInclusive => {
16
                        if let Some((_, start)) = range.first.take() {
16
                            start.serialize(&mut *self.ser)?;
                        }
16
                        self.ser.output.write_str(sep)?;
16
                        if let Some((_, end)) = range.second.take() {
16
                            end.serialize(&mut *self.ser)?;
                        }
                    }
                }
40
                return Ok(());
4
            }
            // fallback: close the struct normally (end_indent + ')' handled below)
866
        }
870
        if let State::Rest = self.state {
842
            if let Some((ref config, ref pretty)) = self.ser.pretty {
526
                if pretty.indent <= config.depth_limit && !config.compact_structs {
490
                    self.ser.output.write_char(',')?;
490
                    self.ser.output.write_str(&config.new_line)?;
36
                }
316
            }
28
        }
870
        if !self.ser.compact_structs() {
830
            self.ser.end_indent()?;
40
        }
870
        if !self.newtype_variant {
854
            self.ser.output.write_char(')')?;
16
        }
870
        Ok(())
910
    }
}
impl<'a, W: fmt::Write> ser::SerializeStructVariant for Compound<'a, W> {
    type Error = Error;
    type Ok = ();
152
    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
152
    where
152
        T: ?Sized + Serialize,
    {
152
        ser::SerializeStruct::serialize_field(self, key, value)
152
    }
72
    fn end(self) -> Result<()> {
72
        ser::SerializeStruct::end(self)
72
    }
}