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
194
195
196
197
198
199
200
use std::fmt;
use elem::RQElem;
use elem::*;
use crate::pb::msg;
pub mod elem;
mod fragment;
mod macros;
pub type MessageElem = msg::elem::Elem;
#[derive(Debug, Default, Clone)]
pub struct MessageChain(pub Vec<MessageElem>);
impl MessageChain {
pub fn new<E: Into<Vec<MessageElem>>>(e: E) -> Self {
Self(e.into())
}
pub fn push<E: Into<Vec<MessageElem>>>(&mut self, e: E) {
self.0.extend(e.into())
}
pub fn anonymous(&self) -> Option<Anonymous> {
self.0.iter().find_map(|e| match e {
MessageElem::AnonGroupMsg(anonymous) => Some(Anonymous::from(anonymous.clone())),
_ => None,
})
}
pub fn reply(&self) -> Option<Reply> {
self.0.iter().find_map(|e| match e {
MessageElem::SrcMsg(src_msg) => Some(Reply::from(src_msg.clone())),
_ => None,
})
}
pub fn with_anonymous(&mut self, anonymous: Anonymous) {
self.0.insert(0, MessageElem::from(anonymous))
}
pub fn with_reply(&mut self, reply: Reply) {
let index = if self.anonymous().is_some() { 1 } else { 0 };
self.0.insert(index, MessageElem::from(reply))
}
}
impl<E> FromIterator<E> for MessageChain
where
E: Into<Vec<MessageElem>>,
{
fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self {
Self(iter.into_iter().flat_map(Into::into).collect())
}
}
impl IntoIterator for MessageChain {
type Item = RQElem;
type IntoIter = impl Iterator<Item = RQElem> + 'static;
fn into_iter(self) -> Self::IntoIter {
self.0
.into_iter()
.filter_map(|e| match e {
MessageElem::SrcMsg(_) => None,
MessageElem::AnonGroupMsg(_) => None,
_ => Some(e),
})
.map(RQElem::from)
}
}
impl From<Vec<msg::Elem>> for MessageChain {
fn from(elements: Vec<msg::Elem>) -> Self {
Self(elements.into_iter().filter_map(|e| e.elem).collect())
}
}
impl From<MessageChain> for Vec<msg::Elem> {
fn from(e: MessageChain) -> Self {
e.0.into_iter()
.map(|e| msg::Elem { elem: Some(e) })
.collect()
}
}
impl fmt::Display for MessageChain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for x in self.clone().into_iter() {
fmt::Display::fmt(&x, f)?
}
Ok(())
}
}
#[derive(Debug, Default, Clone)]
pub struct MessageChainBuilder {
pub elems: Vec<MessageElem>,
pub buf_string: String,
}
impl MessageChainBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn push<E: PushBuilder>(&mut self, elem: E) -> &mut Self {
E::push_builder(elem, self);
self
}
pub fn push_str(&mut self, str: &str) -> &mut Self {
self.buf_string.push_str(str);
self
}
pub fn build(mut self) -> MessageChain {
self.flush();
MessageChain::new(self.elems)
}
fn flush(&mut self) {
flush_builder(self);
}
}
pub trait PushElem {
fn push_to(elem: Self, vec: &mut Vec<MessageElem>);
}
pub trait PushBuilder {
fn push_builder(elem: Self, builder: &mut MessageChainBuilder);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iter() {
let mut chain = MessageChain::default();
chain.push(Text::new("hello".into()));
for e in chain.into_iter() {
println!("{:?}", e)
}
}
#[test]
fn test_display() {
let mut chain = MessageChain::default();
chain.with_anonymous(Anonymous::default());
chain.with_reply(Reply::default());
chain.push(Text::new("hello".into()));
chain.push(At::new(12345));
chain.push(Text::new("world".into()));
chain.push(Face::new(1));
chain.push(Dice::new(1));
chain.push(FingerGuessing::Rock);
chain.push(MarketFace {
name: "xx".into(),
..Default::default()
});
chain.push(LightApp::new("{}".into()));
println!("{}", chain);
println!("{:?}", chain.reply());
println!("{:?}", chain.anonymous());
for item in chain {
println!("{:?}", item)
}
}
#[test]
fn test_builder() {
let mut builder = MessageChainBuilder::new();
builder.push(Anonymous::default());
builder.push(Reply::default());
builder.push_str("hello");
builder.push(At::new(12345));
builder.push_str("world");
builder.push(Face::new(1));
builder.push(Dice::new(1));
builder.push(Text::new("hello".into()));
builder.push_str("world2");
builder.push(FingerGuessing::Rock);
builder.push(MarketFace {
name: "xx".into(),
..Default::default()
});
builder.push(LightApp::new("{}".into()));
let chain = builder.build();
println!("{}", chain);
assert!(chain.reply().is_some());
assert!(chain.anonymous().is_some());
for item in chain {
println!("{:?}", item)
}
}
}