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
use super::{Channel, Encodable, MaybeEmpty};
use std::io::{Result, Write};
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(::serde::Deserialize))]
pub struct Marker<'a> {
pub(crate) channel: &'a str,
pub(crate) comment: Option<&'a str>,
}
pub fn marker<'a>(channel: &'a str, comment: impl Into<Option<&'a str>>) -> Marker<'_> {
Marker {
channel,
comment: comment.into(),
}
}
impl<'a> Encodable for Marker<'a> {
fn encode<W: Write + ?Sized>(&self, buf: &mut W) -> Result<()> {
fn truncate(s: &str) -> &str {
const MAX: usize = 140;
if s.len() <= MAX {
return s;
}
for n in (0..=MAX).rev() {
if s.is_char_boundary(n) {
return &s[..n];
}
}
""
}
write_cmd!(buf, Channel(self.channel) => "/marker{}", MaybeEmpty(self.comment.map(truncate)))
}
}
#[cfg(test)]
mod tests {
use super::super::*;
use super::*;
#[test]
fn marker_encode() {
test_encode(
marker("#museun", Some("this is an example")),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_encode(
marker("#museun", "this is an example"),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_encode(
marker("#museun", "a".repeat(200).as_str()),
format!("PRIVMSG #museun :/marker {}\r\n", "a".repeat(140)),
);
test_encode(marker("#museun", None), "PRIVMSG #museun :/marker\r\n");
}
#[test]
fn marker_ensure_channel_encode() {
test_encode(
marker("museun", Some("this is an example")),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_encode(
marker("museun", "this is an example"),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_encode(
marker("museun", "a".repeat(200).as_str()),
format!("PRIVMSG #museun :/marker {}\r\n", "a".repeat(140)),
);
test_encode(marker("museun", None), "PRIVMSG #museun :/marker\r\n");
}
#[test]
#[cfg(feature = "serde")]
fn marker_serde() {
test_serde(
marker("#museun", Some("this is an example")),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_serde(
marker("#museun", "this is an example"),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_serde(
marker("#museun", "a".repeat(200).as_str()),
format!("PRIVMSG #museun :/marker {}\r\n", "a".repeat(140)),
);
test_serde(marker("#museun", None), "PRIVMSG #museun :/marker\r\n");
}
#[test]
#[cfg(feature = "serde")]
fn marker_ensure_channel_serde() {
test_serde(
marker("museun", Some("this is an example")),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_serde(
marker("museun", "this is an example"),
"PRIVMSG #museun :/marker this is an example\r\n",
);
test_serde(
marker("museun", "a".repeat(200).as_str()),
format!("PRIVMSG #museun :/marker {}\r\n", "a".repeat(140)),
);
test_serde(marker("museun", None), "PRIVMSG #museun :/marker\r\n");
}
}