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
use crate::message::formatted_detail::{FormattedMessageComponent, FormattedMessageDetail, FormattedString, Style};
use crate::message::MessageDetail;
pub trait FormattedStringAppendable {
fn append(&mut self, s: FormattedString) -> &mut Self;
fn append_styled<S: ToString>(&mut self, s: S, style: Style) -> &mut Self {
self.append(FormattedString::styled(s, style));
self
}
fn append_plain<S: ToString>(&mut self, s: S) -> &mut Self {
self.append(FormattedString::plain(s));
self
}
}
pub struct MessageDetailBuilder {
contents: Vec<FormattedMessageComponent>,
raw: String,
}
impl MessageDetailBuilder {
pub fn new() -> Self {
Self::with_raw(String::from("Raw not available"))
}
pub fn with_raw(raw: String) -> Self {
Self {
contents: vec![],
raw,
}
}
pub fn raw(&mut self, raw: String) -> &mut Self {
self.raw = raw;
self
}
pub fn section<F, S>(&mut self, name: S, apply: F) -> &mut Self
where F: FnOnce(&mut SectionBuilder),
S: ToString {
let mut section = SectionBuilder {
name: name.to_string(),
contents: vec![],
};
apply(&mut section);
self.contents.push(section.build());
self
}
pub fn text_block<F>(&mut self, apply: F) -> &mut Self
where F: FnOnce(&mut TextBlockBuilder) {
let mut text_block = TextBlockBuilder::default();
apply(&mut text_block);
self.contents.push(text_block.build());
self
}
pub fn build(self) -> MessageDetail {
MessageDetail::Formatted(FormattedMessageDetail::new(self.raw, self.contents))
}
}
#[derive(Default)]
pub struct TextBlockBuilder {
contents: Vec<FormattedString>,
}
impl TextBlockBuilder {
pub fn build(self) -> FormattedMessageComponent {
FormattedMessageComponent::Text(self.contents)
}
pub fn build_vec(self) -> Vec<FormattedString> {
self.contents
}
}
impl FormattedStringAppendable for TextBlockBuilder {
fn append(&mut self, s: FormattedString) -> &mut Self {
self.contents.push(s);
self
}
}
pub struct SectionBuilder {
name: String,
contents: Vec<FormattedString>,
}
impl SectionBuilder {
pub fn build(self) -> FormattedMessageComponent {
FormattedMessageComponent::Section(self.name, self.contents)
}
}
impl FormattedStringAppendable for SectionBuilder {
fn append(&mut self, s: FormattedString) -> &mut Self {
self.contents.push(s);
self
}
}
#[cfg(test)]
mod test {
use crate::message::formatted_detail::{FormattedMessageComponent, FormattedMessageDetail, FormattedString, Style};
use crate::message::detail_builder::{FormattedStringAppendable, MessageDetailBuilder};
use crate::message::MessageDetail;
#[test]
fn test_detail_builder() {
let mut builder = MessageDetailBuilder::new();
builder.text_block(|block| {
block.append_plain("Base Description");
})
.section("New section", |section| {
section.append_styled("hello", Style::Monospace);
});
let built = builder.build();
let test = MessageDetail::Formatted(FormattedMessageDetail::new(
"Raw not available".to_string(),
vec![
FormattedMessageComponent::Text(vec![FormattedString::plain("Base Description".to_owned())]),
FormattedMessageComponent::Section("New section".to_owned(),
vec![FormattedString::styled("hello", Style::Monospace)])]
));
assert_eq!(built, test);
}
}