| 1 | // (c) 2016-2017 Productize SPRL <[email protected]> |
| 2 | |
| 3 | use std::io; |
| 4 | |
| 5 | use formatter::*; |
| 6 | |
| 7 | use Sexp; |
| 8 | use error::SexpError; |
| 9 | use encode_string; |
| 10 | |
| 11 | struct Serializer<W, F = CompactFormatter> { |
| 12 | writer: W, |
| 13 | formatter: F, |
| 14 | } |
| 15 | |
| 16 | // dispatches only based on Formatter |
| 17 | impl<W> Serializer<W> |
| 18 | where |
| 19 | W: io::Write, |
| 20 | { |
| 21 | fn new(writer: W) -> Self { |
| 22 | Serializer::with_formatter(writer, CompactFormatter) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<W> Serializer<W, RulesFormatter> |
| 27 | where |
| 28 | W: io::Write, |
| 29 | { |
| 30 | fn new_rules(writer: W, rules: Rules) -> Self { |
| 31 | Serializer::with_formatter(writer, RulesFormatter::new(rules)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<W, F> Serializer<W, F> |
| 36 | where |
| 37 | W: io::Write, |
| 38 | F: Formatter, |
| 39 | { |
| 40 | fn with_formatter(writer: W, formatter: F) -> Self { |
| 41 | Serializer { |
| 42 | writer: writer, |
| 43 | formatter: formatter, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn serialize_str(&mut self, value: &str) -> Result<(), SexpError> { |
| 48 | write!(&mut self.writer, "{}", encode_string(value)).map_err(From::from) |
| 49 | } |
| 50 | |
| 51 | fn serialize(&mut self, value: &Sexp) -> Result<(), SexpError> { |
| 52 | match *value { |
| 53 | Sexp::String(ref s) => self.serialize_str(s), |
| 54 | Sexp::List(ref list) => { |
| 55 | let mut first = true; |
| 56 | if list.is_empty() { |
| 57 | self.formatter.open(&mut self.writer, None)?; |
| 58 | } else { |
| 59 | for v in list { |
| 60 | if first { |
| 61 | self.formatter.open(&mut self.writer, Some(v))?; |
| 62 | self.serialize(v)?; |
| 63 | first = false; |
| 64 | } else { |
| 65 | self.formatter.element(&mut self.writer, v)?; |
| 66 | self.serialize(v)?; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | self.formatter.close(&mut self.writer) |
| 71 | } |
| 72 | Sexp::Empty => Ok(()), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// serialize a symbolic-expression to a Writer |
| 78 | pub fn to_writer<W>(writer: &mut W, value: &Sexp) -> Result<(), SexpError> |
| 79 | where |
| 80 | W: io::Write, |
| 81 | { |
| 82 | let mut ser = Serializer::new(writer); |
| 83 | ser.serialize(value) |
| 84 | } |
| 85 | |
| 86 | /// serialize a symbolic-expression to a Writer using a Formatter |
| 87 | pub fn to_writer_with_formatter<W, F>( |
| 88 | writer: &mut W, |
| 89 | formatter: F, |
| 90 | value: &Sexp, |
| 91 | ) -> Result<(), SexpError> |
| 92 | where |
| 93 | W: io::Write, |
| 94 | F: Formatter, |
| 95 | { |
| 96 | let mut ser = Serializer::with_formatter(writer, formatter); |
| 97 | ser.serialize(value) |
| 98 | } |
| 99 | |
| 100 | /// serialize a symbolic-expression to a Writer using a Rules Formatter |
| 101 | pub fn to_writer_with_rules<W>(writer: &mut W, rules: Rules, value: &Sexp) -> Result<(), SexpError> |
| 102 | where |
| 103 | W: io::Write, |
| 104 | { |
| 105 | let mut ser = Serializer::new_rules(writer, rules); |
| 106 | ser.serialize(value) |
| 107 | } |
| 108 | |
| 109 | /// serialize a symbolic-expression to a Vec<u8> |
| 110 | pub fn to_vec(value: &Sexp) -> Result<Vec<u8>, SexpError> { |
| 111 | let mut writer = Vec::with_capacity(128); |
| 112 | to_writer(&mut writer, value)?; |
| 113 | Ok(writer) |
| 114 | } |
| 115 | |
| 116 | /// serialize a symbolic-expression to a Vec<u8> using Rules |
| 117 | pub fn to_vec_with_rules(value: &Sexp, rules: Rules) -> Result<Vec<u8>, SexpError> { |
| 118 | let mut writer = Vec::with_capacity(128); |
| 119 | to_writer_with_rules(&mut writer, rules, value)?; |
| 120 | Ok(writer) |
| 121 | } |
| 122 | |
| 123 | /// serialize a symbolic-expression to a Vec<u8> using a Formatter |
| 124 | pub fn to_vec_with_formatter<F>(value: &Sexp, formatter: F) -> Result<Vec<u8>, SexpError> |
| 125 | where |
| 126 | F: Formatter, |
| 127 | { |
| 128 | let mut writer = Vec::with_capacity(128); |
| 129 | to_writer_with_formatter(&mut writer, formatter, value)?; |
| 130 | Ok(writer) |
| 131 | } |
| 132 | |
| 133 | /// serialize a symbolic-expression to a String |
| 134 | pub fn to_string(value: &Sexp) -> Result<String, SexpError> { |
| 135 | let vec = to_vec(value)?; |
| 136 | let string = String::from_utf8(vec)?; |
| 137 | Ok(string) |
| 138 | } |
| 139 | |
| 140 | /// serialize a symbolic-expression to a String using Rules |
| 141 | pub fn to_string_with_rules(value: &Sexp, rules: Rules) -> Result<String, SexpError> { |
| 142 | let vec = to_vec_with_rules(value, rules)?; |
| 143 | let string = String::from_utf8(vec)?; |
| 144 | Ok(string) |
| 145 | } |
| 146 | |
| 147 | /// serialize a symbolic-expression to a String using a Formatter |
| 148 | pub fn to_string_with_formatter<F>(value: &Sexp, formatter: F) -> Result<String, SexpError> |
| 149 | where |
| 150 | F: Formatter, |
| 151 | { |
| 152 | let vec = to_vec_with_formatter(value, formatter)?; |
| 153 | let string = String::from_utf8(vec)?; |
| 154 | Ok(string) |
| 155 | } |