| 1 | // (c) 2016 Productize SPRL <[email protected]> |
| 2 | |
| 3 | use std::io; |
| 4 | use std::collections::HashMap; |
| 5 | |
| 6 | use error::SexpError; |
| 7 | use Sexp; |
| 8 | |
| 9 | /// trait for formatting the serialization of a symbolic-expression |
| 10 | pub trait Formatter { |
| 11 | /// Called when serializing a '('. |
| 12 | fn open<W>(&mut self, writer: &mut W, value: Option<&Sexp>) -> Result<(), SexpError> |
| 13 | where |
| 14 | W: io::Write; |
| 15 | |
| 16 | /// Called when serializing a ' VAL'. |
| 17 | fn element<W>(&mut self, writer: &mut W, value: &Sexp) -> Result<(), SexpError> |
| 18 | where |
| 19 | W: io::Write; |
| 20 | |
| 21 | /// Called when serializing a ')'. |
| 22 | fn close<W>(&mut self, writer: &mut W) -> Result<(), SexpError> |
| 23 | where |
| 24 | W: io::Write; |
| 25 | } |
| 26 | |
| 27 | pub struct CompactFormatter; |
| 28 | |
| 29 | impl Formatter for CompactFormatter { |
| 30 | fn open<W>(&mut self, writer: &mut W, _value: Option<&Sexp>) -> Result<(), SexpError> |
| 31 | where |
| 32 | W: io::Write, |
| 33 | { |
| 34 | writer.write_all(b"(").map_err(From::from) |
| 35 | } |
| 36 | fn element<W>(&mut self, writer: &mut W, _value: &Sexp) -> Result<(), SexpError> |
| 37 | where |
| 38 | W: io::Write, |
| 39 | { |
| 40 | writer.write_all(b" ").map_err(From::from) |
| 41 | } |
| 42 | |
| 43 | fn close<W>(&mut self, writer: &mut W) -> Result<(), SexpError> |
| 44 | where |
| 45 | W: io::Write, |
| 46 | { |
| 47 | writer.write_all(b")").map_err(From::from) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// datatype used by the example Formatter `RulesFormatter` |
| 52 | pub type Rules = HashMap<&'static str, i64>; |
| 53 | |
| 54 | pub struct RulesFormatter { |
| 55 | indent: Vec<u8>, |
| 56 | indent_before: Rules, |
| 57 | } |
| 58 | |
| 59 | impl Default for RulesFormatter { |
| 60 | fn default() -> Self { |
| 61 | let idb = HashMap::new(); |
| 62 | RulesFormatter { |
| 63 | indent: vec![b' ', b' '], // two spaces |
| 64 | indent_before: idb, |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl RulesFormatter { |
| 70 | pub fn new(indent_before: Rules) -> RulesFormatter { |
| 71 | RulesFormatter { |
| 72 | indent: vec![b' ', b' '], // two spaces |
| 73 | indent_before: indent_before, |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl Formatter for RulesFormatter { |
| 79 | fn open<W>(&mut self, writer: &mut W, value: Option<&Sexp>) -> Result<(), SexpError> |
| 80 | where |
| 81 | W: io::Write, |
| 82 | { |
| 83 | // if first element is string and it has an indent setting |
| 84 | if let Some(sexp) = value { |
| 85 | if let Sexp::String(ref s) = *sexp { |
| 86 | let s: &str = s; |
| 87 | if let Some(&i) = self.indent_before.get(s) { |
| 88 | writer.write_all(b"\n")?; |
| 89 | for _ in 0..i { |
| 90 | writer.write_all(&self.indent)?; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | writer.write_all(b"(").map_err(From::from) |
| 96 | } |
| 97 | |
| 98 | fn element<W>(&mut self, writer: &mut W, value: &Sexp) -> Result<(), SexpError> |
| 99 | where |
| 100 | W: io::Write, |
| 101 | { |
| 102 | // if containing value is a list and it has indent_before |
| 103 | // don't put the space |
| 104 | if let Sexp::List(ref l) = *value { |
| 105 | if !l.is_empty() { |
| 106 | if let Sexp::String(ref s) = l[0] { |
| 107 | let s: &str = s; // why needed? |
| 108 | if self.indent_before.contains_key(s) { |
| 109 | return Ok(()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | writer.write_all(b" ").map_err(From::from) |
| 115 | } |
| 116 | |
| 117 | fn close<W>(&mut self, writer: &mut W) -> Result<(), SexpError> |
| 118 | where |
| 119 | W: io::Write, |
| 120 | { |
| 121 | writer.write_all(b")").map_err(From::from) |
| 122 | } |
| 123 | } |