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
// (c) 2016-2017 Productize SPRL <joost@productize.be>

use std::io;

use formatter::*;

use Sexp;
use error::SexpError;
use encode_string;

struct Serializer<W, F = CompactFormatter> {
    writer: W,
    formatter: F,
}

// dispatches only based on Formatter
impl<W> Serializer<W>
where
    W: io::Write,
{
    fn new(writer: W) -> Self {
        Serializer::with_formatter(writer, CompactFormatter)
    }
}

impl<W> Serializer<W, RulesFormatter>
where
    W: io::Write,
{
    fn new_rules(writer: W, rules: Rules) -> Self {
        Serializer::with_formatter(writer, RulesFormatter::new(rules))
    }
}

impl<W, F> Serializer<W, F>
where
    W: io::Write,
    F: Formatter,
{
    fn with_formatter(writer: W, formatter: F) -> Self {
        Serializer {
            writer: writer,
            formatter: formatter,
        }
    }

    fn serialize_str(&mut self, value: &str) -> Result<(), SexpError> {
        write!(&mut self.writer, "{}", encode_string(value)).map_err(From::from)
    }

    fn serialize(&mut self, value: &Sexp) -> Result<(), SexpError> {
        match *value {
            Sexp::String(ref s) => self.serialize_str(s),
            Sexp::List(ref list) => {
                let mut first = true;
                if list.is_empty() {
                    self.formatter.open(&mut self.writer, None)?;
                } else {
                    for v in list {
                        if first {
                            self.formatter.open(&mut self.writer, Some(v))?;
                            self.serialize(v)?;
                            first = false;
                        } else {
                            self.formatter.element(&mut self.writer, v)?;
                            self.serialize(v)?;
                        }
                    }
                }
                self.formatter.close(&mut self.writer)
            }
            Sexp::Empty => Ok(()),
        }
    }
}

/// serialize a symbolic-expression to a Writer
pub fn to_writer<W>(writer: &mut W, value: &Sexp) -> Result<(), SexpError>
where
    W: io::Write,
{
    let mut ser = Serializer::new(writer);
    ser.serialize(value)
}

/// serialize a symbolic-expression to a Writer using a Formatter
pub fn to_writer_with_formatter<W, F>(
    writer: &mut W,
    formatter: F,
    value: &Sexp,
) -> Result<(), SexpError>
where
    W: io::Write,
    F: Formatter,
{
    let mut ser = Serializer::with_formatter(writer, formatter);
    ser.serialize(value)
}

/// serialize a symbolic-expression to a Writer using a Rules Formatter
pub fn to_writer_with_rules<W>(writer: &mut W, rules: Rules, value: &Sexp) -> Result<(), SexpError>
where
    W: io::Write,
{
    let mut ser = Serializer::new_rules(writer, rules);
    ser.serialize(value)
}

/// serialize a symbolic-expression to a Vec<u8>
pub fn to_vec(value: &Sexp) -> Result<Vec<u8>, SexpError> {
    let mut writer = Vec::with_capacity(128);
    to_writer(&mut writer, value)?;
    Ok(writer)
}

/// serialize a symbolic-expression to a Vec<u8> using Rules
pub fn to_vec_with_rules(value: &Sexp, rules: Rules) -> Result<Vec<u8>, SexpError> {
    let mut writer = Vec::with_capacity(128);
    to_writer_with_rules(&mut writer, rules, value)?;
    Ok(writer)
}

/// serialize a symbolic-expression to a Vec<u8> using a Formatter
pub fn to_vec_with_formatter<F>(value: &Sexp, formatter: F) -> Result<Vec<u8>, SexpError>
where
    F: Formatter,
{
    let mut writer = Vec::with_capacity(128);
    to_writer_with_formatter(&mut writer, formatter, value)?;
    Ok(writer)
}

/// serialize a symbolic-expression to a String
pub fn to_string(value: &Sexp) -> Result<String, SexpError> {
    let vec = to_vec(value)?;
    let string = String::from_utf8(vec)?;
    Ok(string)
}

/// serialize a symbolic-expression to a String using Rules
pub fn to_string_with_rules(value: &Sexp, rules: Rules) -> Result<String, SexpError> {
    let vec = to_vec_with_rules(value, rules)?;
    let string = String::from_utf8(vec)?;
    Ok(string)
}

/// serialize a symbolic-expression to a String using a Formatter
pub fn to_string_with_formatter<F>(value: &Sexp, formatter: F) -> Result<String, SexpError>
where
    F: Formatter,
{
    let vec = to_vec_with_formatter(value, formatter)?;
    let string = String::from_utf8(vec)?;
    Ok(string)
}