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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
//! Converting S-expression values into text.
//!
//! # Terminology
//!
//! The process of serializing S-expressions to their textual
//! representation is referred to "writing" in Lisp. To avoid
//! confusion with Rust's `Write` trait, `lexpr` uses "printing"
//! instead.

use std::io;

use crate::number::{self, Number};
pub use crate::syntax::{CharSyntax, KeywordSyntax, StringSyntax};
use crate::Value;

/// Options for printing S-expressions.
#[derive(Copy, Clone, Debug)]
pub struct Options {
    keyword_syntax: KeywordSyntax,
    nil_syntax: NilSyntax,
    bool_syntax: BoolSyntax,
    vector_syntax: VectorSyntax,
    bytes_syntax: BytesSyntax,
    string_syntax: StringSyntax,
    char_syntax: CharSyntax,
}

impl Options {
    /// Construct parser options suitable for printing Emacs Lisp.
    ///
    /// Keywords will use prefix notation (i.e. `:some-keyword`), the
    /// special nil value will be represented as a symbol, and
    /// booleans will be represented by `nil` and `t`.
    pub fn elisp() -> Self {
        Options {
            keyword_syntax: KeywordSyntax::ColonPrefix,
            nil_syntax: NilSyntax::Symbol,
            bool_syntax: BoolSyntax::Symbol,
            vector_syntax: VectorSyntax::Brackets,
            bytes_syntax: BytesSyntax::Elisp,
            string_syntax: StringSyntax::Elisp,
            char_syntax: CharSyntax::Elisp,
        }
    }

    /// Set the syntax to use for printing keywords.
    pub fn with_keyword_syntax(mut self, syntax: KeywordSyntax) -> Self {
        self.keyword_syntax = syntax;
        self
    }

    /// Set the syntax to use to print the special nil value.
    pub fn with_nil_syntax(mut self, syntax: NilSyntax) -> Self {
        self.nil_syntax = syntax;
        self
    }

    /// Set the syntax to use to print boolean values.
    pub fn with_bool_syntax(mut self, syntax: BoolSyntax) -> Self {
        self.bool_syntax = syntax;
        self
    }

    /// Set the syntax for printing vectors.
    pub fn with_vector_syntax(mut self, syntax: VectorSyntax) -> Self {
        self.vector_syntax = syntax;
        self
    }

    /// Set the syntax to use for printing byte vectors.
    pub fn with_bytes_syntax(mut self, syntax: BytesSyntax) -> Self {
        self.bytes_syntax = syntax;
        self
    }

    /// Set the syntax used for printing strings.
    pub fn with_string_syntax(mut self, syntax: StringSyntax) -> Self {
        self.string_syntax = syntax;
        self
    }

    /// Set the syntax used for printing characters.
    pub fn with_char_syntax(mut self, syntax: CharSyntax) -> Self {
        self.char_syntax = syntax;
        self
    }
}

impl Default for Options {
    fn default() -> Self {
        Options {
            keyword_syntax: KeywordSyntax::Octothorpe,
            nil_syntax: NilSyntax::Token,
            bool_syntax: BoolSyntax::Token,
            vector_syntax: VectorSyntax::Octothorpe,
            bytes_syntax: BytesSyntax::R7RS,
            string_syntax: StringSyntax::R6RS,
            char_syntax: CharSyntax::R6RS,
        }
    }
}

/// How to print the special nil value.
#[derive(Debug, Clone, Copy)]
pub enum NilSyntax {
    /// Output a `nil` symbol.
    Symbol,
    /// Output the `#nil` token.
    Token,
    /// Output the empty list.
    EmptyList,
    /// Output a boolean false value.
    False,
}

/// How to print boolean values.
#[derive(Debug, Clone, Copy)]
pub enum BoolSyntax {
    /// Use the Scheme tokens `#t` and `#f`
    Token,
    /// Use symbols `nil` and `t`.
    Symbol,
}

/// How to print vectors.
#[derive(Debug, Clone, Copy)]
pub enum VectorSyntax {
    /// Use Scheme notation, i.e. `#(...)`.
    Octothorpe,
    /// Use brackets, as used in Emacs Lisp.
    Brackets,
}

/// How to print byte vectors.
#[derive(Debug, Clone, Copy)]
pub enum BytesSyntax {
    /// Use R6RS byte vector syntax, e.g. `#vu8(1 2 3)`.
    R6RS,
    /// Use R7RS byte vector syntax, e.g. `#u8(1 2 3)`.
    R7RS,
    /// Use Emacs Lisp unibyte string syntax, e.g. `"\001\002\003"`.
    Elisp,
}

/// Represents a character escape code in a type-safe manner.
pub enum CharEscape {
    /// An escaped quote `"`
    Quote,
    /// An escaped reverse solidus `\`
    ReverseSolidus,
    /// Alert, also known as "bell" (usually escaped as `\a`)
    Alert,
    /// An escaped backspace character (usually escaped as `\b`)
    Backspace,
    /// An escaped line feed character (usually escaped as `\n`)
    LineFeed,
    /// An escaped carriage return character (usually escaped as `\r`)
    CarriageReturn,
    /// An escaped tab character (usually escaped as `\t`)
    Tab,
    /// An escaped ASCII plane control character (usually escaped as
    /// `\u00XX` where `XX` are two hex characters)
    AsciiControl(u8),
}

impl CharEscape {
    #[inline]
    fn from_escape_table(escape: u8, byte: u8) -> CharEscape {
        match escape {
            self::AA => CharEscape::Alert,
            self::BB => CharEscape::Backspace,
            self::TT => CharEscape::Tab,
            self::NN => CharEscape::LineFeed,
            self::RR => CharEscape::CarriageReturn,
            self::QU => CharEscape::Quote,
            self::BS => CharEscape::ReverseSolidus,
            self::UU => CharEscape::AsciiControl(byte),
            _ => unreachable!(),
        }
    }
}

/// Different vector types
pub enum VectorType {
    /// Generic vector, containing elements of any type.
    Generic,
    /// Byte vector, containing only byte (octet) values.
    Byte,
}

/// This trait abstracts away serializing the S-expression pieces,
/// which allows the implementer to optionally pretty print the
/// S-expression output, as well as to allow customizing the printing
/// for various S-expression "dialects".
///
/// The default implementation produces Scheme-syntax S-expression
/// text.
pub trait Formatter {
    /// Writes a representation of the special nil value to the specified writer.
    #[inline]
    fn write_nil<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"#nil")
    }

    /// Writes a representation of the special nil value to the specified writer.
    #[inline]
    fn write_null<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"()")
    }

    /// Writes a representation of a boolean value to the specified writer.
    ///
    /// The implementation provided by the trait will use the Scheme notation
    /// (`#t` and `#f`).
    #[inline]
    fn write_bool<W: ?Sized>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(if value { b"#t" } else { b"#f" })
    }

    /// Writes an integer value like `-123` to the specified writer.
    #[inline]
    fn write_number<W: ?Sized>(&mut self, writer: &mut W, value: &Number) -> io::Result<()>
    where
        W: io::Write,
    {
        struct Write<'a, W: io::Write + ?Sized> {
            writer: &'a mut W,
        }
        impl<'a, W: io::Write + ?Sized> number::Visitor for Write<'a, W> {
            type Value = ();
            type Error = io::Error;

            fn error<T: Into<String>>(msg: T) -> io::Error {
                io::Error::new(io::ErrorKind::Other, msg.into())
            }
            fn visit_u64(self, n: u64) -> io::Result<()> {
                itoa::write(self.writer, n).map(drop)
            }
            fn visit_i64(self, n: i64) -> io::Result<()> {
                itoa::write(self.writer, n).map(drop)
            }
            fn visit_f64(self, n: f64) -> io::Result<()> {
                let mut buffer = ryu::Buffer::new();
                let s = buffer.format(n);
                self.writer.write_all(s.as_bytes())
            }
        }
        value.visit(Write { writer })
    }

    /// Writes a charactor to the specified writer.
    ///
    /// The implementation provided by the trait will use Scheme notation
    /// (`#\C`).
    fn write_char<W: ?Sized>(&mut self, writer: &mut W, c: char) -> io::Result<()>
    where
        W: io::Write,
    {
        write_scheme_char(writer, c)
    }

    /// Called before each series of `write_string_fragment` and
    /// `write_char_escape`.  Writes a `"` to the specified writer.
    #[inline]
    fn begin_string<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"\"")
    }

    /// Called after each series of `write_string_fragment` and
    /// `write_char_escape`.  Writes a `"` to the specified writer.
    #[inline]
    fn end_string<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"\"")
    }

    /// Writes a string fragment that doesn't need any escaping to the specified
    /// writer.
    #[inline]
    fn write_string_fragment<W: ?Sized>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(fragment.as_bytes())
    }

    /// Writes a character escape code to the specified writer.
    #[inline]
    fn write_char_escape<W: ?Sized>(
        &mut self,
        writer: &mut W,
        char_escape: CharEscape,
    ) -> io::Result<()>
    where
        W: io::Write,
    {
        write_r6rs_char_escape(writer, char_escape)
    }

    /// Writes a symbol to the specified writer.
    #[inline]
    fn write_symbol<W: ?Sized>(&mut self, writer: &mut W, name: &str) -> io::Result<()>
    where
        W: io::Write,
    {
        // TODO: We might need to escape and/or use pipe notation.
        writer.write_all(name.as_bytes())
    }

    /// Writes a keyword to the specified writer.
    #[inline]
    fn write_keyword<W: ?Sized>(&mut self, writer: &mut W, name: &str) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"#:")?;
        writer.write_all(name.as_bytes())
    }

    /// Writes a byte vector to the specified writer.
    #[inline]
    fn write_bytes<W: ?Sized>(&mut self, writer: &mut W, bytes: &[u8]) -> io::Result<()>
    where
        W: io::Write,
    {
        write_scheme_vector(self, writer, VectorType::Byte, bytes, |writer, &octet| {
            itoa::write(writer, octet).map(|_| ())
        })
    }

    /// Called before any list elements.  Writes a `(` to the specified
    /// writer.
    #[inline]
    fn begin_list<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b"(")
    }

    /// Called after all list elements have been written.  Writes a `)` to the
    /// specified writer.
    #[inline]
    fn end_list<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b")")
    }

    /// Called before starting to write a list or vector element. Writes a space
    /// to the specified writer, if needed.
    #[inline]
    fn begin_seq_element<W: ?Sized>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
    where
        W: io::Write,
    {
        if first {
            Ok(())
        } else {
            writer.write_all(b" ")
        }
    }

    /// Called after every list or vector element.
    #[inline]
    fn end_seq_element<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        Ok(())
    }

    /// Called before any vector elements.  Will write `#(` for generic vectors,
    /// or `#u8(` for byte vectors, to the specified writer.
    #[inline]
    fn begin_vector<W: ?Sized>(&mut self, kind: VectorType, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        match kind {
            VectorType::Generic => writer.write_all(b"#("),
            VectorType::Byte => writer.write_all(b"#u8("),
        }
    }

    /// Called after all vector elements have been written.  Writes a `)` to the
    /// specified writer.
    #[inline]
    fn end_vector<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b")")
    }

    /// Called before writing the tail of an improper list, or more
    /// generally, the `cdr` field of a cons cell.  Writes a `.` to
    /// the specified writer.
    #[inline]
    fn write_dot<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(b".")
    }
}

/// This structure compacts a S-expression value on a single line,
/// using the default representation, which is similar to Scheme.
#[derive(Clone, Debug)]
pub struct DefaultFormatter;

impl Formatter for DefaultFormatter {}

/// A formatter which can be tuned with regards to S-expressions representation.
#[derive(Clone, Debug)]
pub struct CustomizedFormatter {
    options: Options,
}

impl Formatter for CustomizedFormatter {
    fn write_nil<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.nil_syntax {
            NilSyntax::EmptyList => writer.write_all(b"()"),
            NilSyntax::Symbol => writer.write_all(b"nil"),
            NilSyntax::Token => writer.write_all(b"#nil"),
            NilSyntax::False => self.write_bool(writer, false),
        }
    }

    fn write_bool<W: ?Sized>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.bool_syntax {
            BoolSyntax::Symbol => writer.write_all(if value { b"t" } else { b"nil" }),
            BoolSyntax::Token => writer.write_all(if value { b"#t" } else { b"#f" }),
        }
    }

    fn write_keyword<W: ?Sized>(&mut self, writer: &mut W, name: &str) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.keyword_syntax {
            KeywordSyntax::ColonPostfix => {
                writer.write_all(name.as_bytes())?;
                writer.write_all(b":")
            }
            KeywordSyntax::ColonPrefix => {
                writer.write_all(b":")?;
                writer.write_all(name.as_bytes())
            }
            KeywordSyntax::Octothorpe => {
                writer.write_all(b"#:")?;
                writer.write_all(name.as_bytes())
            }
        }
    }

    fn begin_vector<W: ?Sized>(&mut self, kind: VectorType, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.vector_syntax {
            VectorSyntax::Brackets => writer.write_all(b"["),
            VectorSyntax::Octothorpe => match kind {
                VectorType::Generic => writer.write_all(b"#("),
                VectorType::Byte => match self.options.bytes_syntax {
                    BytesSyntax::R6RS => writer.write_all(b"#vu8("),
                    BytesSyntax::R7RS => writer.write_all(b"#u8("),
                    _ => panic!("invalid combination of VectorSyntax and BytesSyntax"),
                },
            },
        }
    }

    fn end_vector<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.vector_syntax {
            VectorSyntax::Brackets => writer.write_all(b"]"),
            VectorSyntax::Octothorpe => writer.write_all(b")"),
        }
    }

    fn write_char<W: ?Sized>(&mut self, writer: &mut W, c: char) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.char_syntax {
            CharSyntax::R6RS => write_scheme_char(writer, c),
            CharSyntax::Elisp => write_elisp_char(writer, c),
        }
    }

    /// Writes a character escape code to the specified writer.
    #[inline]
    fn write_char_escape<W: ?Sized>(
        &mut self,
        writer: &mut W,
        char_escape: CharEscape,
    ) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.string_syntax {
            StringSyntax::R6RS => write_r6rs_char_escape(writer, char_escape),
            StringSyntax::Elisp => write_elisp_char_escape(writer, char_escape),
        }
    }

    fn write_bytes<W: ?Sized>(&mut self, writer: &mut W, bytes: &[u8]) -> io::Result<()>
    where
        W: io::Write,
    {
        match self.options.bytes_syntax {
            BytesSyntax::R6RS | BytesSyntax::R7RS => {
                write_scheme_vector(self, writer, VectorType::Byte, bytes, |writer, &octet| {
                    itoa::write(writer, octet).map(|_| ())
                })
            }
            BytesSyntax::Elisp => {
                static OCTAL_CHARS: &[u8] = b"012345678";
                writer.write_all(b"\"")?;
                for octet in bytes {
                    writer.write_all(b"\\")?;
                    for &triplet in &[
                        (octet >> 6) & 0b111u8,
                        (octet >> 3) & 0b111u8,
                        octet & 0b111u8,
                    ] {
                        let index = triplet as usize;
                        writer.write_all(&OCTAL_CHARS[index..=index])?;
                    }
                }
                writer.write_all(b"\"")
            }
        }
    }
}

/// A printer for S-expression values.
#[derive(Debug)]
pub struct Printer<W, F = DefaultFormatter> {
    writer: W,
    formatter: F,
}

impl<W> Printer<W, CustomizedFormatter>
where
    W: io::Write,
{
    /// Construct an S-expression printer tuned given the specified options.
    pub fn with_options(writer: W, options: Options) -> Self {
        Printer {
            writer,
            formatter: CustomizedFormatter { options },
        }
    }
}

impl<W, F> Printer<W, F>
where
    W: io::Write,
    F: Formatter,
{
    /// Creates a new S-expression printer whose output will be
    /// written to the writer specified.
    #[inline]
    pub fn with_formatter(writer: W, formatter: F) -> Self {
        Printer { writer, formatter }
    }

    /// Unwrap the `Writer` from the `Printer`.
    #[inline]
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Output the representation of the specified value to the underlying
    /// writer.
    pub fn print(&mut self, value: &Value) -> io::Result<()> {
        match value {
            Value::Nil => self.formatter.write_nil(&mut self.writer),
            Value::Null => self.formatter.write_null(&mut self.writer),
            Value::Bool(b) => self.formatter.write_bool(&mut self.writer, *b),
            Value::Number(n) => self.formatter.write_number(&mut self.writer, &n),
            Value::Char(c) => self.formatter.write_char(&mut self.writer, *c),
            Value::Symbol(name) => self.formatter.write_symbol(&mut self.writer, &name),
            Value::Keyword(name) => self.formatter.write_keyword(&mut self.writer, &name),
            Value::String(s) => format_escaped_str(&mut self.writer, &mut self.formatter, &s),
            Value::Bytes(bytes) => self.formatter.write_bytes(&mut self.writer, bytes),
            Value::Cons(elements) => {
                self.formatter.begin_list(&mut self.writer)?;
                for (i, pair) in elements.iter().enumerate() {
                    self.formatter.begin_seq_element(&mut self.writer, i == 0)?;
                    self.print(pair.car())?;
                    self.formatter.end_seq_element(&mut self.writer)?;
                    match pair.cdr() {
                        Value::Null | Value::Cons(_) => {}
                        _ => {
                            self.formatter.begin_seq_element(&mut self.writer, false)?;
                            self.formatter.write_dot(&mut self.writer)?;
                            self.formatter.end_seq_element(&mut self.writer)?;
                            self.formatter.begin_seq_element(&mut self.writer, false)?;
                            self.print(pair.cdr())?;
                            self.formatter.end_seq_element(&mut self.writer)?;
                        }
                    }
                }
                self.formatter.end_list(&mut self.writer)
            }
            Value::Vector(elements) => {
                self.write_vector(VectorType::Generic, elements.iter(), |printer, element| {
                    printer.print(element)
                })
            }
        }
    }

    fn write_vector<I, O>(&mut self, kind: VectorType, elements: I, mut output: O) -> io::Result<()>
    where
        I: IntoIterator,
        O: FnMut(&mut Self, I::Item) -> io::Result<()>,
    {
        self.formatter.begin_vector(kind, &mut self.writer)?;
        for (i, element) in elements.into_iter().enumerate() {
            self.formatter.begin_seq_element(&mut self.writer, i == 0)?;
            output(self, element)?;
            self.formatter.end_seq_element(&mut self.writer)?;
        }
        self.formatter.end_vector(&mut self.writer)
    }
}

fn write_scheme_vector<F: ?Sized, W: ?Sized, I, O>(
    fmt: &mut F,
    writer: &mut W,
    kind: VectorType,
    elements: I,
    mut output: O,
) -> io::Result<()>
where
    F: Formatter,
    W: io::Write,
    I: IntoIterator,
    O: FnMut(&mut W, I::Item) -> io::Result<()>,
{
    fmt.begin_vector(kind, writer)?;
    for (i, element) in elements.into_iter().enumerate() {
        fmt.begin_seq_element(writer, i == 0)?;
        output(writer, element)?;
        fmt.end_seq_element(writer)?;
    }
    fmt.end_vector(writer)
}

impl<W> Printer<W>
where
    W: io::Write,
{
    /// Creates a new S-expression printer.
    #[inline]
    pub fn new(writer: W) -> Self {
        Printer::with_formatter(writer, DefaultFormatter)
    }
}

impl<W, F> io::Write for Printer<W, F>
where
    W: io::Write,
{
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.writer.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

fn format_escaped_str<W: ?Sized, F: ?Sized>(
    writer: &mut W,
    formatter: &mut F,
    value: &str,
) -> io::Result<()>
where
    W: io::Write,
    F: Formatter,
{
    formatter.begin_string(writer)?;
    format_escaped_str_contents(writer, formatter, value)?;
    formatter.end_string(writer)?;
    Ok(())
}

fn format_escaped_str_contents<W: ?Sized, F: ?Sized>(
    writer: &mut W,
    formatter: &mut F,
    value: &str,
) -> io::Result<()>
where
    W: io::Write,
    F: Formatter,
{
    let bytes = value.as_bytes();

    let mut start = 0;

    for (i, &byte) in bytes.iter().enumerate() {
        let escape = ESCAPE[byte as usize];
        if escape == 0 {
            continue;
        }

        if start < i {
            formatter.write_string_fragment(writer, &value[start..i])?;
        }

        let char_escape = CharEscape::from_escape_table(escape, byte);
        formatter.write_char_escape(writer, char_escape)?;

        start = i + 1;
    }

    if start != bytes.len() {
        formatter.write_string_fragment(writer, &value[start..])?;
    }

    Ok(())
}

fn write_r6rs_char_escape<W: ?Sized>(writer: &mut W, char_escape: CharEscape) -> io::Result<()>
where
    W: io::Write,
{
    use self::CharEscape::*;

    let s = match char_escape {
        Quote => b"\\\"",
        ReverseSolidus => b"\\\\",
        Alert => b"\\a",
        Backspace => b"\\b",
        LineFeed => b"\\n",
        CarriageReturn => b"\\r",
        Tab => b"\\t",
        AsciiControl(byte) => {
            static HEX_DIGITS: [u8; 16] = *b"0123456789ABCDEF";
            let bytes = &[
                b'\\',
                b'x',
                HEX_DIGITS[(byte >> 4) as usize],
                HEX_DIGITS[(byte & 0xF) as usize],
                b';',
            ];
            return writer.write_all(bytes);
        }
    };

    writer.write_all(s)
}

fn write_elisp_char_escape<W: ?Sized>(writer: &mut W, char_escape: CharEscape) -> io::Result<()>
where
    W: io::Write,
{
    use self::CharEscape::*;

    let s = match char_escape {
        Quote => b"\\\"",
        ReverseSolidus => b"\\\\",
        Alert => b"\\a",
        Backspace => b"\\b",
        LineFeed => b"\\n",
        CarriageReturn => b"\\r",
        Tab => b"\\t",
        AsciiControl(byte) => {
            // Note we use the `\uNNNN` syntax here, as a hexadecimal or octal
            // escape might turn the string into a unibyte string.
            static HEX_DIGITS: [u8; 16] = *b"0123456789ABCDEF";
            let bytes = &[
                b'\\',
                b'u',
                b'0',
                b'0',
                HEX_DIGITS[(byte >> 4) as usize],
                HEX_DIGITS[(byte & 0xF) as usize],
            ];
            return writer.write_all(bytes);
        }
    };

    writer.write_all(s)
}

fn write_scheme_char<W: ?Sized>(writer: &mut W, c: char) -> io::Result<()>
where
    W: io::Write,
{
    let n = u32::from(c);
    if n >= 32 && n < 127 {
        // ASCII, excluding non-printable characters
        let buf = [b'#', b'\\', n as u8];
        writer.write_all(&buf)
    } else {
        // TODO: we should probably output UTF-8 here, if reasonable, to be
        // consistent with the behaviour inside strings.
        write!(writer, "#\\x{:x}", n)
    }
}

fn write_elisp_char<W: ?Sized>(writer: &mut W, c: char) -> io::Result<()>
where
    W: io::Write,
{
    let n = u32::from(c);
    if n >= 32 && n < 127 {
        let c = n as u8;
        // ASCII, excluding non-printable characters
        if ELISP_ESCAPE_CHARS.contains(&c) {
            writer.write_all(&[b'?', b'\\', c])
        } else {
            writer.write_all(&[b'?', c])
        }
    } else {
        // TODO: we should probably output UTF-8 here, if reasonable, to be
        // consistent with the behaviour inside strings.
        write!(writer, "?\\x{:x}", n)
    }
}

const AA: u8 = b'a'; // \x07
const BB: u8 = b'b'; // \x08
const TT: u8 = b't'; // \x09
const NN: u8 = b'n'; // \x0A
const RR: u8 = b'r'; // \x0D
const QU: u8 = b'"'; // \x22
const BS: u8 = b'\\'; // \x5C
const UU: u8 = b'u'; // \x00...\x1F except the ones above
const __: u8 = 0;

// Lookup table of escape sequences. A value of b'x' at index i means that byte
// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.
static ESCAPE: [u8; 256] = [
    //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    UU, UU, UU, UU, UU, UU, UU, AA, BB, TT, NN, UU, UU, RR, UU, UU, // 0
    UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1
    __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
    __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, UU, // 7
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
    __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
];

static ELISP_ESCAPE_CHARS: &[u8] = b"()[]\\;|'`#.,";

/// Serialize the given value value as S-expression text into the IO stream,
/// using the default printer options.
#[inline]
pub fn to_writer<W: io::Write>(writer: W, value: &Value) -> io::Result<()> {
    let mut printer = Printer::new(writer);
    printer.print(value)?;
    Ok(())
}

/// Serialize the given value value as S-expression text into the IO stream.
#[inline]
pub fn to_writer_custom<W: io::Write>(
    writer: W,
    value: &Value,
    options: Options,
) -> io::Result<()> {
    let mut printer = Printer::with_options(writer, options);
    printer.print(value)?;
    Ok(())
}

/// Serialize the given value as byte vector containing S-expression text, using
/// the default printer options.
#[inline]
pub fn to_vec(value: &Value) -> io::Result<Vec<u8>> {
    let mut writer = Vec::with_capacity(128);
    to_writer(&mut writer, value)?;
    Ok(writer)
}

/// Serialize the given value as byte vector containing S-expression text.
#[inline]
pub fn to_vec_custom(value: &Value, options: Options) -> io::Result<Vec<u8>> {
    let mut writer = Vec::with_capacity(128);
    to_writer_custom(&mut writer, value, options)?;
    Ok(writer)
}

/// Serialize the given value an S-expression string,
/// using the default printer options.
#[inline]
pub fn to_string(value: &Value) -> io::Result<String> {
    let vec = to_vec(value)?;
    let string = unsafe {
        // We do not emit invalid UTF-8.
        String::from_utf8_unchecked(vec)
    };
    Ok(string)
}

/// Serialize the given value an S-expression string.
#[inline]
pub fn to_string_custom(value: &Value, options: Options) -> io::Result<String> {
    let vec = to_vec_custom(value, options)?;
    let string = unsafe {
        // We do not emit invalid UTF-8.
        String::from_utf8_unchecked(vec)
    };
    Ok(string)
}

#[cfg(test)]
mod tests;