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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
use std::borrow::Cow;
use lazy_static::lazy_static;
use regex::Regex;
fn to_title_case<'a>(text: &'a str) -> Cow<'a, str> {
let mut chars = text.chars();
match chars.next() {
None => Cow::Borrowed(text),
Some(c) => Cow::Owned(
c.to_uppercase().collect::<String>() +
&chars.as_str().to_lowercase()
)
}
}
lazy_static! {
static ref FACE_RE: Regex = Regex::new(r"[:Xx][\)\]|p|P|d|D|o|O]").unwrap();
}
trait Trollify {
fn sentance_delimiter(&self) -> char {
'.'
}
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Borrowed(word)
}
fn troll_line<'l>(&self, line: &'l str) -> Cow<'l, str> {
Cow::Borrowed(line)
}
fn troll_sentence<'s>(&self, sentence: &'s str) -> Cow<'s, str> {
Cow::Borrowed(sentence)
}
fn troll_all<'a>(&self, all: &'a str) -> Cow<'a, str> {
Cow::Borrowed(all)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Borrowed(face)
}
}
struct Araida;
impl Trollify for Araida {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word.to_lowercase().replace("o", "0"))
}
}
struct Tavros;
impl Trollify for Tavros {
fn sentance_delimiter(&self) -> char {
','
}
fn troll_sentence<'s>(&self, sentence: &'s str) -> Cow<'s, str> {
let mut chars = sentence.chars();
match chars.next() {
None => Cow::Borrowed(sentence),
Some(c) => Cow::Owned(
c.to_lowercase().collect::<String>() +
&chars.as_str().to_uppercase()
)
}
}
fn troll_all<'a>(&self, all: &'a str) -> Cow<'a, str> {
Cow::Owned(all
.replace(".", ",")
.replace("?", ",")
.replace("!", ",")
)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Owned(String::from("}") + face)
}
}
struct Sollux;
impl Trollify for Sollux {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word
.to_lowercase()
.replace("s", "2")
.replace("i", "ii")
.replace("to", "two")
)
}
}
struct Karkat;
impl Trollify for Karkat {
fn troll_all<'a>(&self, all: &'a str) -> Cow<'a, str> {
Cow::Owned(all.to_uppercase())
}
}
struct Nepeta;
impl Trollify for Nepeta {
fn troll_line<'l>(&self, line: &'l str) -> Cow<'l, str> {
Cow::Owned(
String::from(":33 < ") +
&line.to_lowercase()
)
}
}
struct Kanaya;
impl Trollify for Kanaya {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
to_title_case(word)
}
}
struct Terezi;
impl Trollify for Terezi {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word
.to_uppercase()
.replace("A", "4")
.replace("I", "1")
.replace("E", "3")
)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Owned(
String::from(">") +
&face.replace(")", "]")
)
}
}
struct Vriska;
impl Trollify for Vriska {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word.replace("b", "8"))
}
fn troll_sentence<'s>(&self, sentence: &'s str) -> Cow<'s, str> {
to_title_case(sentence)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Owned(face
.replace(":", "::::")
.replace("X", "XXXX")
)
}
}
struct Equius;
impl Trollify for Equius {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word.replace("x", "%"))
}
fn troll_sentence<'s>(&self, sentence: &'s str) -> Cow<'s, str> {
to_title_case(sentence)
}
fn troll_line<'l>(&self, line: &'l str) -> Cow<'l, str> {
Cow::Owned(String::from("D --> ") + line)
}
}
struct Gamzee;
impl Trollify for Gamzee {
fn troll_all<'a>(&self, all: &'a str) -> Cow<'a, str> {
let chars = all.chars();
let mut next = true;
let mut buf = String::new();
for char in chars {
if char.is_alphabetic() {
if next {
buf += &char.to_uppercase().to_string();
} else {
buf += &char.to_lowercase().to_string();
}
next = !next;
} else {
buf.push(char);
}
}
Cow::Owned(buf)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Owned(face.replace(":", ":o"))
}
}
struct Eridan;
impl Trollify for Eridan {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word
.to_lowercase()
.replace("v", "vv")
.replace("w", "ww")
)
}
}
struct Feferi;
impl Trollify for Feferi {
fn troll_word<'w>(&self, word: &'w str) -> Cow<'w, str> {
Cow::Owned(word
.replace("h", ") (")
.replace("E", "-E")
)
}
fn troll_sentence<'s>(&self, sentence: &'s str) -> Cow<'s, str> {
to_title_case(sentence)
}
fn troll_face<'f>(&self, face: &'f str) -> Cow<'f, str> {
Cow::Owned(face.replace(":", "3:"))
}
}
#[derive(clap::ValueEnum, serde::Serialize, Clone, Copy, Debug)]
#[serde(rename_all = "kebab-case")]
pub enum Troll {
Araida,
Tavros,
Sollux,
Karkat,
Nepeta,
Kanaya,
Terezi,
Vriska,
Equius,
Gamzee,
Eridan,
Feferi
}
fn get_troll(troll: Troll) -> Box<dyn Trollify> {
match troll {
Troll::Araida => Box::new(Araida {}),
Troll::Tavros => Box::new(Tavros {}),
Troll::Sollux => Box::new(Sollux {}),
Troll::Karkat => Box::new(Karkat {}),
Troll::Nepeta => Box::new(Nepeta {}),
Troll::Kanaya => Box::new(Kanaya {}),
Troll::Terezi => Box::new(Terezi {}),
Troll::Vriska => Box::new(Vriska {}),
Troll::Equius => Box::new(Equius {}),
Troll::Gamzee => Box::new(Gamzee {}),
Troll::Eridan => Box::new(Eridan {}),
Troll::Feferi => Box::new(Feferi {}),
}
}
pub fn trollify<'a>(text: &'a str, troll: Troll) -> String {
let troll = get_troll(troll);
let text = troll.troll_all(text);
let sentences = text.split(troll.sentance_delimiter());
let mut buf = String::new();
for sentence in sentences {
let sentence = troll.troll_sentence(sentence);
sentence
.split(" ")
.into_iter()
.map(|word| {
match FACE_RE.is_match(word) {
true => troll.troll_face(word),
false => troll.troll_word(word)
}
})
.enumerate()
.for_each(|(idx, word)| {
if idx != 0 {
buf += " ";
}
buf += &word;
});
}
let text = buf;
let lines = text.split("\n");
let mut buf = String::new();
for (idx, line) in lines.enumerate() {
if idx != 0 {
buf.push('\n');
}
let line = troll.troll_line(line);
buf += &line;
}
let text = buf;
text
}
|