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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::model::Tagged;

use std::default::Default;

pub trait Style {
    fn common_styles_apply(&mut self, _styles: &mut CommonStyles) {}

    fn tagged_styles_apply(&mut self, _value: &mut dyn Tagged) {}
}

impl<'a, 'b> Style for &'a mut [&'b mut dyn Style] {
    fn common_styles_apply(&mut self, styles: &mut CommonStyles) {
        for style in self.into_iter() {
            style.common_styles_apply(styles);
        }
    }

    fn tagged_styles_apply(&mut self, value: &mut dyn Tagged) {
        for style in self.into_iter() {
            style.tagged_styles_apply(value);
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct CommonStyles {
    indent: u8,
    threshold: u8,
    params: u16,
}

impl CommonStyles {
    fn _on(&mut self, val: u16) {
        self.params |= val;
    }

    fn _off(&mut self, val: u16) {
        self.params &= !val;
    }

    fn _set(&mut self, val: u16, onoff: bool) {
        if onoff {
            self._on(val)
        } else {
            self._off(val)
        }
    }

    fn _is(&self, val: u16) -> bool {
        self.params & val == val
    }

    pub fn flow(&self) -> bool {
        self._is(1)
    }

    pub fn set_flow(&mut self, val: bool) {
        self._set(1, val);
    }

    pub fn compact(&self) -> bool {
        self._is(2)
    }

    pub fn set_compact(&mut self, val: bool) {
        self._set(2, val)
    }

    pub fn respect_threshold(&self) -> bool {
        self._is(4)
    }

    pub fn set_respect_threshold(&mut self, val: bool) {
        self._set(4, val)
    }

    pub fn multiline(&self) -> bool {
        self._is(8)
    }

    pub fn set_multiline(&mut self, val: bool) {
        self._set(8, val)
    }

    pub fn issue_tag(&self) -> bool {
        self._is(16)
    }

    pub fn set_issue_tag(&mut self, val: bool) {
        self._set(16, val)
    }

    pub fn indent(&self) -> u8 {
        self.indent
    }

    pub fn set_indent(&mut self, value: u8) {
        if value > 0 {
            self.indent = value;
        }
    }

    pub fn threshold(&self) -> u8 {
        self.threshold
    }

    pub fn set_threshold(&mut self, value: u8) {
        if value > 0 {
            self.threshold = value;
        }
    }
}

impl Default for CommonStyles {
    fn default() -> CommonStyles {
        CommonStyles {
            params: 0,
            indent: 2,
            threshold: 120,
        }
    }
}

pub struct Indent(pub u8);

impl Style for Indent {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_indent(self.0)
    }
}

pub struct Threshold(pub u8);

impl Style for Threshold {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_threshold(self.0)
    }
}

pub const FLOW: Flow = Flow(true);
pub const NO_FLOW: Flow = Flow(false);

pub struct Flow(pub bool);

impl Style for Flow {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_flow(self.0)
    }
}

pub const COMPACT: Compact = Compact(true);
pub const NO_COMPACT: Compact = Compact(false);

pub struct Compact(pub bool);

impl Style for Compact {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_compact(self.0)
    }
}

pub const RESPECT_THRESHOLD: RespectThreshold = RespectThreshold(true);
pub const NO_RESPECT_THRESHOLD: RespectThreshold = RespectThreshold(false);

pub struct RespectThreshold(pub bool);

impl Style for RespectThreshold {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_respect_threshold(self.0)
    }
}

pub const MULTILINE: Multiline = Multiline(true);
pub const NO_MULTILINE: Multiline = Multiline(false);

pub struct Multiline(pub bool);

impl Style for Multiline {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_multiline(self.0)
    }
}

pub const ISSUE_TAG: IssueTag = IssueTag(true);
pub const NO_ISSUE_TAG: IssueTag = IssueTag(false);

pub struct IssueTag(pub bool);

impl Style for IssueTag {
    fn common_styles_apply(&mut self, style: &mut CommonStyles) {
        style.set_issue_tag(self.0)
    }
}