pyc_shell/translator/
mod.rs

1//! ## Translator
2//!
3//! `translator` is the module which takes care of translating latin to russian cyrillic and viceversa
4
5/*
6*
7*   Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
8*
9* 	This file is part of "Pyc"
10*
11*   Pyc is free software: you can redistribute it and/or modify
12*   it under the terms of the GNU General Public License as published by
13*   the Free Software Foundation, either version 3 of the License, or
14*   (at your option) any later version.
15*
16*   Pyc is distributed in the hope that it will be useful,
17*   but WITHOUT ANY WARRANTY; without even the implied warranty of
18*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*   GNU General Public License for more details.
20*
21*   You should have received a copy of the GNU General Public License
22*   along with Pyc.  If not, see <http://www.gnu.org/licenses/>.
23*
24*/
25
26pub mod ioprocessor;
27pub mod lang;
28
29use lang::Language;
30
31/// ### Translator
32///
33/// Struct used to convert form cyrillic script to latin script and viceversa
34pub trait Translator {
35  /// ### to_latin
36  ///
37  /// Converts a string which contains russian cyrillic characters into a latin string.
38  /// Characters between '"' (quotes) are escaped, expressions inside escaped blocks are translitarated anyway
39  fn to_latin(&self, input: &String) -> String;
40
41  /// ### to_cyrillic
42  ///
43  /// Converts a string which contains latin characters into a russian cyrillic string.
44  /// Characters between quotes are escapes
45  fn to_cyrillic(&self, input: &String) -> String;
46}
47
48/// ### new_translator
49///
50/// instantiates a new Translator with the provided language,
51/// associating the correct conversion functions
52pub fn new_translator(language: Language) -> Box<dyn Translator> {
53  match language {
54    Language::Belarusian => Box::new(lang::Belarusian {}),
55    Language::Bulgarian => Box::new(lang::Bulgarian {}),
56    Language::Russian => Box::new(lang::Russian {}),
57    Language::Serbian => Box::new(lang::Serbian {}),
58    Language::Ukrainian => Box::new(lang::Ukrainian {}),
59    Language::Nil => Box::new(lang::Nil {})
60  }
61}
62
63#[cfg(test)]
64mod tests {
65
66  use super::*;
67
68  #[test]
69  fn test_translator_new() {
70    //Just don't panic
71    let _ = new_translator(Language::Belarusian);
72    let _ = new_translator(Language::Bulgarian);
73    let _ = new_translator(Language::Russian);
74    let _ = new_translator(Language::Serbian);
75    let _ = new_translator(Language::Ukrainian);
76    let _ = new_translator(Language::Nil);
77  }
78
79}