morse_lib/
lib.rs

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
use std::{cell::RefCell, thread, time};

mod morse_char;
use morse_char::*;

mod morse_unit;
pub use morse_unit::MorseUnit;

mod morse_processors;
use morse_processors::*;

mod display_chars;
use display_chars::DisplayChars;

mod sound;
use sound::Sound;

#[derive(Debug, PartialEq, Clone)]
pub struct Morse {
    morse: Vec<MorseChar>,
    language: String,
    display_as: DisplayChars,
    sound: Sound,
    from_char_converter: fn(char) -> Vec<MorseUnit>,
    into_char_converter: fn(Vec<MorseUnit>) -> char,
}

impl Morse {
    pub fn new(
        language: String,
        from_char: fn(char) -> Vec<MorseUnit>,
        into_char: fn(Vec<MorseUnit>) -> char,
    ) -> Morse {
        Morse {
            morse: Vec::new(),
            language,
            display_as: DisplayChars::default(),
            sound: Sound::default(),
            from_char_converter: from_char,
            into_char_converter: into_char,
        }
    }
    pub fn from_str(text: &str) -> Morse {
        let mut morse: Vec<MorseChar> = Vec::new();

        for letter in text.chars() {
            morse.push(MorseChar::from_char(letter, "International", from_int_char));
        }

        Morse {
            morse,
            ..Morse::default()
        }
    }

    pub fn parse_text(&mut self, text: &str) {
        let mut morse: Vec<MorseChar> = Vec::new();

        for letter in text.chars() {
            morse.push(MorseChar::from_char(
                letter,
                &self.language,
                self.from_char_converter,
            ));
        }
    }

    pub fn from_bin(bin: &str) -> Morse {
        let words: Vec<&str> = bin.split("0000000").collect();
        let mut morse: Vec<MorseChar> = Vec::new();

        for word in words {
            let letters: Vec<&str> = word.split("000").collect();

            for letter in letters {
                morse.push(MorseChar::from_bin(letter, "International", into_int_char));
            }
        }

        Morse {
            morse,
            ..Morse::default()
        }
    }

    pub fn parse_bin(&mut self, bin: &str) {
        let words: Vec<&str> = bin.split("0000000").collect();

        for word in words {
            let letters: Vec<&str> = word.split("000").collect();

            for letter in letters {
                self.morse.push(MorseChar::from_bin(
                    letter,
                    &self.language,
                    self.into_char_converter,
                ));
            }
        }
    }

    pub fn to_beep(&self) {
        let morse = RefCell::new(self.morse.clone());
        for (idx, m_char) in morse.borrow_mut().iter_mut().enumerate() {
            m_char.frequency(self.sound.frequency);
            m_char.play_speed(self.sound.speed);

            m_char.to_beep();

            // The space between letters is three units
            if idx < self.morse.len() - 1 {
                thread::sleep(time::Duration::from_secs(3));
            }
        }
    }

    pub fn get_language(&self) -> String {
        self.language.clone()
    }

    pub fn dot_as(&mut self, alias: &str) {
        self.display_as.dot = alias.to_string();
    }

    pub fn line_as(&mut self, alias: &str) {
        self.display_as.line = alias.to_string();
    }

    pub fn whitespace_as(&mut self, alias: &str) {
        self.display_as.whitespace = alias.to_string();
    }

    pub fn frequency(&mut self, frequency: f32) {
        self.sound.frequency = frequency;
    }
    pub fn play_speed(&mut self, speed: f32) {
        self.sound.speed = speed;
    }

    pub fn to_bin_str(&self) -> String {
        let mut string = String::new();

        for (idx, m_char) in self.morse.iter().enumerate() {
            string.push_str(&m_char.to_bin_str());

            // The space between letters is three units
            if idx < self.morse.len() - 1 {
                string.push_str("000");
            }
        }

        string
    }
}

impl Default for Morse {
    fn default() -> Self {
        Self {
            morse: Vec::new(),
            language: "International".to_string(),
            display_as: DisplayChars::default(),
            sound: Sound::default(),
            from_char_converter: from_int_char,
            into_char_converter: into_int_char,
        }
    }
}

impl ToString for Morse {
    fn to_string(&self) -> String {
        let mut string = String::new();
        let morse = RefCell::new(self.morse.clone());

        for (idx, m_char) in morse.borrow_mut().iter_mut().enumerate() {
            m_char.dot_as(&self.display_as.dot);
            m_char.line_as(&self.display_as.line);
            m_char.whitespace_as(&self.display_as.whitespace);
            string.push_str(&m_char.to_string());

            // The space between letters is three units
            if idx < self.morse.len() - 1 {
                string.push_str("   ");
            }
        }

        string
    }
}

#[cfg(test)]
mod morse_tests {
    use super::*;

    #[test]
    fn create_from_text_str() {
        assert_eq!(
            Morse::from_str("Hello").to_bin_str(),
            "1010101000100010111010100010111010100011101110111"
        );
    }

    #[test]
    fn create_from_binary_str() {
        const HELLO_BIN: &str = "1010101000100010111010100010111010100011101110111";
        assert_eq!(Morse::from_bin(HELLO_BIN).to_bin_str(), HELLO_BIN);
    }

    #[test]
    fn get_language() {
        assert_eq!(
            Morse::from_str("hello").get_language(),
            "International".to_string()
        );
        assert_eq!(
            Morse::from_bin("1").get_language(),
            "International".to_string()
        );
    }

    #[test]
    fn to_string() {
        assert_eq!(
            Morse::from_str("hi u").to_string(),
            ". . . .   . .       . . ⚊"
        );
    }

    #[test]
    fn to_bin_str() {
        assert_eq!(
            Morse::from_str("hi u").to_bin_str(),
            "101010100010100000001010111"
        );
    }
    #[test]
    fn set_aliases_for_whitespace_lines_and_dots() {
        let mut morse = Morse::from_str("hi u");

        morse.dot_as("🔥");
        morse.line_as("➖");
        morse.whitespace_as("🚧");

        assert_eq!(morse.to_string(), "🔥 🔥 🔥 🔥   🔥 🔥   🚧   🔥 🔥 ➖");
    }
}