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
mod substitution;

use substitution::{MorseChunk, itu_substitution, gerke_substitution};

pub type Plaintext = String;
pub type Morse = String;

// MORSE TYPES
pub enum MorseType {
    ITU, // International
    Morse, //American
    Gerke // Continental
}

// MORSE FUNCTIONS
fn with_substitute(word: &Plaintext, substitue_method: fn(char) -> MorseChunk) -> Morse {
    word.to_uppercase().chars().into_iter().map(|chr|{ substitue_method(chr) })
    .collect::<Vec<MorseChunk>>().join("")
}

pub trait MorseSubstitution {
    fn to_morse(&self, morse_type: MorseType) -> Morse;
}

impl MorseSubstitution for Plaintext {
    fn to_morse(&self, morse_type: MorseType) -> Morse {
        match morse_type {
            MorseType::ITU => with_substitute(&self, itu_substitution),
            MorseType::Gerke => with_substitute(&self, gerke_substitution),
            _ => panic!("Other methods than ITU not implemented. Type")
        }
    }
}