| 1 | // (c) 2016-2017 Productize SPRL <[email protected]> |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 4 | |
| 5 | use formatter::*; |
| 6 | |
| 7 | use Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#; |
| 8 | use error::SexpError; |
| 9 | use encode_stringrust-analyzer cargo symbolic_expressions 5.0.3 sexp/encode_string().; |
| 10 | |
| 11 | struct Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#<W, F = CompactFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/CompactFormatter#> { |
| 12 | writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer.: W, |
| 13 | formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter.: F, |
| 14 | } |
| 15 | |
| 16 | // dispatches only based on Formatter |
| 17 | impl<W> Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#<W> |
| 18 | where |
| 19 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 20 | { |
| 21 | fn newrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W>`]new().(writer: W) -> Selfrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W>`] { |
| 22 | Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#::with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]with_formatter().(writer, CompactFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/CompactFormatter#) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<W> Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#<W, RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#> |
| 27 | where |
| 28 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 29 | { |
| 30 | fn new_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, RulesFormatter>`]new_rules().(writer: W, rules: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#) -> Selfrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, RulesFormatter>`] { |
| 31 | Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#::with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]with_formatter().(writer, RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#::newrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter]new().(rules)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<W, F> Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#<W, F> |
| 36 | where |
| 37 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 38 | F: Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#, |
| 39 | { |
| 40 | fn with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]with_formatter().(writer: W, formatter: F) -> Selfrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`] { |
| 41 | Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer# { |
| 42 | writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer.: writer, |
| 43 | formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter.: formatter, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn serialize_strrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize_str().(&mut self, value: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 48 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut self.writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer., "{}", encode_stringrust-analyzer cargo symbolic_expressions 5.0.3 sexp/encode_string().(value)).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#::fromrust-analyzer cargo symbolic_expressions 5.0.3 error/impl#[SexpError][`From<io::Error>`]from().) |
| 49 | } |
| 50 | |
| 51 | fn serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(&mut self, value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 52 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value { |
| 53 | Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Stringrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#String#(ref s) => self.serialize_strrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize_str().(s), |
| 54 | Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Listrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#List#(ref list) => { |
| 55 | let mut first = true; |
| 56 | if list.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 57 | self.formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter..openrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#open().(&mut self.writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 58 | } else { |
| 59 | for v in list { |
| 60 | if first { |
| 61 | self.formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter..openrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#open().(&mut self.writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 62 | self.serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(v)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 63 | first = false; |
| 64 | } else { |
| 65 | self.formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter..elementrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#element().(&mut self.writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer., v)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 66 | self.serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(v)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | self.formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#formatter..closerust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#close().(&mut self.writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#writer.) |
| 71 | } |
| 72 | Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Emptyrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#Empty# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// serialize a symbolic-expression to a Writer |
| 78 | pub fn to_writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer().<W>(writer: &mut W, value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> |
| 79 | where |
| 80 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 81 | { |
| 82 | let mut ser = Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#::newrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W>`]new().(writer); |
| 83 | ser.serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(value) |
| 84 | } |
| 85 | |
| 86 | /// serialize a symbolic-expression to a Writer using a Formatter |
| 87 | pub fn to_writer_with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer_with_formatter().<W, F>( |
| 88 | writer: &mut W, |
| 89 | formatter: F, |
| 90 | value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#, |
| 91 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> |
| 92 | where |
| 93 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 94 | F: Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#, |
| 95 | { |
| 96 | let mut ser = Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#::with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]with_formatter().(writer, formatter); |
| 97 | ser.serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(value) |
| 98 | } |
| 99 | |
| 100 | /// serialize a symbolic-expression to a Writer using a Rules Formatter |
| 101 | pub fn to_writer_with_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer_with_rules().<W>(writer: &mut W, rules: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#, value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> |
| 102 | where |
| 103 | W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 104 | { |
| 105 | let mut ser = Serializerrust-analyzer cargo symbolic_expressions 5.0.3 ser/Serializer#::new_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, RulesFormatter>`]new_rules().(writer, rules); |
| 106 | ser.serializerust-analyzer cargo symbolic_expressions 5.0.3 ser/impl#[`Serializer<W, F>`]serialize().(value) |
| 107 | } |
| 108 | |
| 109 | /// serialize a symbolic-expression to a Vec<u8> |
| 110 | pub fn to_vecrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec().(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 111 | let mut writer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(128); |
| 112 | to_writerrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer().(&mut writer, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 113 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(writer) |
| 114 | } |
| 115 | |
| 116 | /// serialize a symbolic-expression to a Vec<u8> using Rules |
| 117 | pub fn to_vec_with_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec_with_rules().(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#, rules: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 118 | let mut writer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(128); |
| 119 | to_writer_with_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer_with_rules().(&mut writer, rules, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 120 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(writer) |
| 121 | } |
| 122 | |
| 123 | /// serialize a symbolic-expression to a Vec<u8> using a Formatter |
| 124 | pub fn to_vec_with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec_with_formatter().<F>(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#, formatter: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> |
| 125 | where |
| 126 | F: Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#, |
| 127 | { |
| 128 | let mut writer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(128); |
| 129 | to_writer_with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_writer_with_formatter().(&mut writer, formatter, value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 130 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(writer) |
| 131 | } |
| 132 | |
| 133 | /// serialize a symbolic-expression to a String |
| 134 | pub fn to_stringrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_string().(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 135 | let vec = to_vecrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 136 | let string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(vec)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 137 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(string) |
| 138 | } |
| 139 | |
| 140 | /// serialize a symbolic-expression to a String using Rules |
| 141 | pub fn to_string_with_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_string_with_rules().(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#, rules: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> { |
| 142 | let vec = to_vec_with_rulesrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec_with_rules().(value, rules)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 143 | let string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(vec)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 144 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(string) |
| 145 | } |
| 146 | |
| 147 | /// serialize a symbolic-expression to a String using a Formatter |
| 148 | pub fn to_string_with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_string_with_formatter().<F>(value: &Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#, formatter: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#> |
| 149 | where |
| 150 | F: Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#, |
| 151 | { |
| 152 | let vec = to_vec_with_formatterrust-analyzer cargo symbolic_expressions 5.0.3 ser/to_vec_with_formatter().(value, formatter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 153 | let string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(vec)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 154 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(string) |
| 155 | } |