pseudo_cyrillic/
lib.rs

1/// Converts letters of the Russian alphabet into Latin characters similar in outline.
2///
3/// # Examples
4///
5/// ```
6/// # use pseudo_cyrillic::convert;
7///
8/// assert_eq!(convert("Воин Света"), "BouH CBema");
9/// ```
10pub fn convert(original: &str) -> String {
11    let mut new_string = String::with_capacity(original.len());
12
13    for ch in original.chars() {
14        let analog = match ch {
15            // Строчные буквы
16            'а' => "a",
17            'б' => "6",
18            'в' => "B",
19            'г' => "r",
20            'д' => "D",
21            'е' => "e",
22            'ё' => "e",
23            'ж' => "zh",
24            'з' => "3",
25            'и' => "u",
26            'й' => "u",
27            'к' => "k",
28            'л' => "JI",
29            'м' => "M",
30            'н' => "H",
31            'о' => "o",
32            'п' => "n",
33            'р' => "p",
34            'с' => "c",
35            'т' => "m",
36            'у' => "y",
37            'ф' => "qp",
38            'х' => "x",
39            'ц' => "u",
40            'ч' => "4",
41            'ш' => "LLI",
42            'щ' => "LLj",
43            'ъ' => "b",
44            'ы' => "bI",
45            'ь' => "b",
46            'э' => "3",
47            'ю' => "IO",
48            'я' => "R",
49            // ПРОПИСНЫЕ БУКВЫ
50            'А' => "A",
51            'Б' => "6",
52            'В' => "B",
53            'Г' => "r",
54            'Д' => "D",
55            'Е' => "E",
56            'Ё' => "E",
57            'Ж' => "ZH",
58            'З' => "3",
59            'И' => "u",
60            'Й' => "u",
61            'К' => "K",
62            'Л' => "JI",
63            'М' => "M",
64            'Н' => "H",
65            'О' => "O",
66            'П' => "n",
67            'Р' => "P",
68            'С' => "C",
69            'Т' => "T",
70            'У' => "Y",
71            'Ф' => "qp",
72            'Х' => "X",
73            'Ц' => "U",
74            'Ч' => "4",
75            'Ш' => "LLI",
76            'Щ' => "LLj",
77            'Ъ' => "b",
78            'Ы' => "bI",
79            'Ь' => "b",
80            'Э' => "3",
81            'Ю' => "IO",
82            'Я' => "R",
83            // Остальные символы оставляем без изменений
84            _ => {
85                new_string.push(ch);
86                continue
87            }
88        };
89
90        new_string.push_str(analog);
91    }
92
93    new_string
94}
95
96#[cfg(test)]
97mod tests {
98    #![allow(non_snake_case)]
99
100    use super::*;
101
102    #[test]
103    fn test_convert() {
104        assert_eq!(convert(
105            "абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ A1"),
106                   "a6BrDeezh3uukJIMHonpcmyqpxu4LLILLjbbIb3IORA6BrDEEZH3uuKJIMHOnPCTYqpXU4LLILLjbbIb3IOR A1"
107        );
108    }
109
110    #[test]
111    fn test_a() {
112        assert_eq!(convert("а"), "a");
113    }
114
115    #[test]
116    fn test_b() {
117        assert_eq!(convert("б"), "6");
118    }
119
120    #[test]
121    fn test_v() {
122        assert_eq!(convert("в"), "B");
123    }
124
125    #[test]
126    fn test_g() {
127        assert_eq!(convert("г"), "r");
128    }
129
130    #[test]
131    fn test_d() {
132        assert_eq!(convert("д"), "D");
133    }
134
135    #[test]
136    fn test_e() {
137        assert_eq!(convert("е"), "e");
138    }
139
140    #[test]
141    fn test_yo() {
142        assert_eq!(convert("ё"), "e");
143    }
144
145    #[test]
146    fn test_zh() {
147        assert_eq!(convert("ж"), "zh");
148    }
149
150    #[test]
151    fn test_z() {
152        assert_eq!(convert("з"), "3");
153    }
154
155    #[test]
156    fn test_i() {
157        assert_eq!(convert("и"), "u");
158    }
159
160    #[test]
161    fn test_j() {
162        assert_eq!(convert("й"), "u");
163    }
164
165    #[test]
166    fn test_k() {
167        assert_eq!(convert("к"), "k");
168    }
169
170    #[test]
171    fn test_l() {
172        assert_eq!(convert("л"), "JI");
173    }
174
175    #[test]
176    fn test_m() {
177        assert_eq!(convert("м"), "M");
178    }
179
180    #[test]
181    fn test_n() {
182        assert_eq!(convert("н"), "H");
183    }
184
185    #[test]
186    fn test_o() {
187        assert_eq!(convert("о"), "o");
188    }
189
190    #[test]
191    fn test_p() {
192        assert_eq!(convert("п"), "n");
193    }
194
195    #[test]
196    fn test_r() {
197        assert_eq!(convert("р"), "p");
198    }
199
200    #[test]
201    fn test_s() {
202        assert_eq!(convert("с"), "c");
203    }
204
205    #[test]
206    fn test_t() {
207        assert_eq!(convert("т"), "m");
208    }
209
210    #[test]
211    fn test_u() {
212        assert_eq!(convert("у"), "y");
213    }
214
215    #[test]
216    fn test_f() {
217        assert_eq!(convert("ф"), "qp");
218    }
219
220    #[test]
221    fn test_x() {
222        assert_eq!(convert("х"), "x");
223    }
224
225    #[test]
226    fn test_c() {
227        assert_eq!(convert("ц"), "u");
228    }
229
230    #[test]
231    fn test_ch() {
232        assert_eq!(convert("ч"), "4");
233    }
234
235    #[test]
236    fn test_sh() {
237        assert_eq!(convert("ш"), "LLI");
238    }
239
240    #[test]
241    fn test_shh() {
242        assert_eq!(convert("щ"), "LLj");
243    }
244
245    #[test]
246
247    fn test_tverdyi_znak() {
248        assert_eq!(convert("ъ"), "b");
249    }
250
251    #[test]
252    fn test_yy() {
253        assert_eq!(convert("ы"), "bI");
254    }
255
256    #[test]
257    fn test_myagkiy_znak() {
258        assert_eq!(convert("ь"), "b");
259    }
260
261    #[test]
262    fn test_ee() {
263        assert_eq!(convert("э"), "3");
264    }
265
266    #[test]
267    fn test_yu() {
268        assert_eq!(convert("ю"), "IO");
269    }
270
271    #[test]
272    fn test_ya() {
273        assert_eq!(convert("я"), "R");
274    }
275
276    #[test]
277    fn test_A() {
278        assert_eq!(convert("А"), "A");
279    }
280
281    #[test]
282    fn test_B() {
283        assert_eq!(convert("Б"), "6");
284    }
285
286    #[test]
287    fn test_V() {
288        assert_eq!(convert("В"), "B");
289    }
290
291    #[test]
292    fn test_G() {
293        assert_eq!(convert("Г"), "r");
294    }
295
296    #[test]
297    fn test_D() {
298        assert_eq!(convert("Д"), "D");
299    }
300
301    #[test]
302    fn test_E() {
303        assert_eq!(convert("Е"), "E");
304    }
305
306    #[test]
307    fn test_Yo() {
308        assert_eq!(convert("Ё"), "E");
309    }
310
311    #[test]
312    fn test_Zh() {
313        assert_eq!(convert("Ж"), "ZH");
314    }
315
316    #[test]
317    fn test_Z() {
318        assert_eq!(convert("З"), "3");
319    }
320
321    #[test]
322    fn test_I() {
323        assert_eq!(convert("И"), "u");
324    }
325
326    #[test]
327    fn test_J() {
328        assert_eq!(convert("Й"), "u");
329    }
330
331    #[test]
332    fn test_K() {
333        assert_eq!(convert("К"), "K");
334    }
335
336    #[test]
337    fn test_L() {
338        assert_eq!(convert("Л"), "JI");
339    }
340
341    #[test]
342    fn test_M() {
343        assert_eq!(convert("М"), "M");
344    }
345
346    #[test]
347    fn test_N() {
348        assert_eq!(convert("Н"), "H");
349    }
350
351    #[test]
352    fn test_O() {
353        assert_eq!(convert("О"), "O");
354    }
355
356    #[test]
357    fn test_P() {
358        assert_eq!(convert("П"), "n");
359    }
360
361    #[test]
362    fn test_R() {
363        assert_eq!(convert("Р"), "P");
364    }
365
366    #[test]
367    fn test_S() {
368        assert_eq!(convert("С"), "C");
369    }
370
371    #[test]
372    fn test_T() {
373        assert_eq!(convert("Т"), "T");
374    }
375
376    #[test]
377    fn test_U() {
378        assert_eq!(convert("У"), "Y");
379    }
380
381    #[test]
382    fn test_F() {
383        assert_eq!(convert("Ф"), "qp");
384    }
385
386    #[test]
387    fn test_X() {
388        assert_eq!(convert("Х"), "X");
389    }
390
391    #[test]
392    fn test_C() {
393        assert_eq!(convert("Ц"), "U");
394    }
395
396    #[test]
397    fn test_Ch() {
398        assert_eq!(convert("Ч"), "4");
399    }
400
401    #[test]
402    fn test_Sh() {
403        assert_eq!(convert("Ш"), "LLI");
404    }
405
406    #[test]
407    fn test_Shh() {
408        assert_eq!(convert("Щ"), "LLj");
409    }
410
411    #[test]
412    fn test_Tverdiy_znak() {
413        assert_eq!(convert("Ъ"), "b");
414    }
415
416    #[test]
417    fn test_YY() {
418        assert_eq!(convert("Ы"), "bI");
419    }
420
421    #[test]
422    fn test_Myagkiy_znak() {
423        assert_eq!(convert("Ь"), "b");
424    }
425
426    #[test]
427    fn test_Ee() {
428        assert_eq!(convert("Э"), "3");
429    }
430
431    #[test]
432    fn test_Yu() {
433        assert_eq!(convert("Ю"), "IO");
434    }
435
436    #[test]
437    fn test_Ya() {
438        assert_eq!(convert("Я"), "R");
439    }
440}