Debian Codegraph @

src/formatter.rs

raw symbols SCIP
1 // (c) 2016 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 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
5
6 use error::SexpError;
7 use Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#;
8
9 /// trait for formatting the serialization of a symbolic-expression
10 pub trait Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter# {
11 /// Called when serializing a '('.
12 fn openrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#open().<W>(&mut self, writer: &mut W, value: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&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#>
13 where
14 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#;
15
16 /// Called when serializing a ' VAL'.
17 fn elementrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#element().<W>(&mut self, 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#>
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 /// Called when serializing a ')'.
22 fn closerust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter#close().<W>(&mut self, writer: &mut W) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#>
23 where
24 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#;
25 }
26
27 pub struct CompactFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/CompactFormatter#;
28
29 impl Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter# for CompactFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/CompactFormatter# {
30 fn openrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[CompactFormatter][Formatter]open().<W>(&mut self, writer: &mut W, _value: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&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#>
31 where
32 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#,
33 {
34 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"(").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().)
35 }
36 fn elementrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[CompactFormatter][Formatter]element().<W>(&mut self, 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#>
37 where
38 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#,
39 {
40 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b" ").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().)
41 }
42
43 fn closerust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[CompactFormatter][Formatter]close().<W>(&mut self, writer: &mut W) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#>
44 where
45 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#,
46 {
47 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b")").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().)
48 }
49 }
50
51 /// datatype used by the example Formatter `RulesFormatter`
52 pub type Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules# = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<&'static str, i64>;
53
54 pub struct RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
55 indentrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
56 indent_beforerust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent_before.: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#,
57 }
58
59 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
60 fn defaultrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter][Default]default().() -> Selfrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter][Default] {
61 let idb = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
62 RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
63 indentrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b' ', b' '], // two spaces
64 indent_beforerust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent_before.: idb,
65 }
66 }
67 }
68
69 impl RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
70 pub fn newrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter]new().(indent_before: Rulesrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Rules#) -> RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
71 RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
72 indentrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b' ', b' '], // two spaces
73 indent_beforerust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent_before.: indent_before,
74 }
75 }
76 }
77
78 impl Formatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/Formatter# for RulesFormatterrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter# {
79 fn openrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter][Formatter]open().<W>(&mut self, writer: &mut W, value: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&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#>
80 where
81 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#,
82 {
83 // if first element is string and it has an indent setting
84 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(sexp) = value {
85 if let Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Stringrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#String#(ref s) = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().sexp {
86 let s: &str = s;
87 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&i) = self.indent_beforerust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent_before..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(s) {
88 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"\n")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
89 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#i {
90 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&self.indentrust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
91 }
92 }
93 }
94 }
95 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"(").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().)
96 }
97
98 fn elementrust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter][Formatter]element().<W>(&mut self, 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#>
99 where
100 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#,
101 {
102 // if containing value is a list and it has indent_before
103 // don't put the space
104 if let Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Listrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#List#(ref l) = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value {
105 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().l.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
106 if let Sexprust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#::Stringrust-analyzer cargo symbolic_expressions 5.0.3 sexp/Sexp#String#(ref s) = l[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). {
107 let s: &str = s; // why needed?
108 if self.indent_beforerust-analyzer cargo symbolic_expressions 5.0.3 formatter/RulesFormatter#indent_before..contains_keyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]contains_key().(s) {
109 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
110 }
111 }
112 }
113 }
114 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b" ").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().)
115 }
116
117 fn closerust-analyzer cargo symbolic_expressions 5.0.3 formatter/impl#[RulesFormatter][Formatter]close().<W>(&mut self, writer: &mut W) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), SexpErrorrust-analyzer cargo symbolic_expressions 5.0.3 error/SexpError#>
118 where
119 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#,
120 {
121 writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b")").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().)
122 }
123 }