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
use std::{ collections::HashMap };

pub trait Output { fn output(&self, input: &str) -> String; }
pub trait Mapper {
    type Input;
    type Result;
    /// maps a single ```char``` to another one.
    /// The implementing Mapper must provide a HashMap<char, char>
    fn map(&self, c: Self::Input) -> Result<Self::Result, String>;
}

/// The ```Simple``` Mapper takes an set of input characters [a] and maps it to the character set [b]
#[derive(Debug)] pub struct Simple(HashMap<char, char>);

impl Simple {
    pub fn new(a: &str, b: &str) -> Self { Simple(a.chars().zip(b.chars()).collect()) }
    pub fn default() -> Self {
        Self::new (
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!\"§$%&/()=?,.-;:_'{[]}<>^|",
            "ДВСDЁҒGНІЈКLМПОРQЯЅТЦЏШХЧZавсdёfgніјкlмпорqгѕтцѵшхчz 1234567890!\"§$%&/()=?,.-;:_'{[]}<>ˇ|"
        )
    }
}

impl Mapper for Simple {
    type Input = char;
    type Result = char;
    fn map(&self, c: Self::Input) -> Result<char, String> {
        let out: char = *self.0.get(&c).unwrap_or(&c); 
        Ok(out) 
    }
}
impl Output for Simple {
    fn output(&self, input: &str) -> String {
        input.chars().map(|c| self.map(c).unwrap()).collect()
    }
}