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
use crate::{DOT_LINE, LETTER};
use std::collections::BTreeMap;

/// Decodes a morse representation string into an ascii string
///
/// # Examples
/// ```
/// use morse::decode;
///
/// assert_eq!(decode::decode("... ___ ...").unwrap(), "sos");
/// ```
/// # Errors
///
/// Decoding will error with a `morse::TranslationError`
/// when an unsupported morse character is being decoded.
/// The error structure contains a `Vec<String> unsupported_characters`
/// to show what characters failed.
///
/// # Notice
/// SOS HELP      = ...___...  .... . ._.. .__.        => ...___........._...__.
///  I AM HIS DATE = ..  ._ __  .... .. ...  _.. ._ _ . => ...___........._...__.
pub fn decode(input: &str) -> String {
    let mut result = String::new();
    let mut map = BTreeMap::new();
    for (k, v) in DOT_LINE.iter().zip(LETTER.iter()) {
        map.insert(*k, v);
    }
    let normed = input.replace("*", ".").replace("-", "_");
    for word in normed.trim().split("/") {
        for c in word.trim().split(" ") {
            let ch = match map.get(c) {
                Some(o) => **o,
                None => decode_missing(c),
            };
            result.push(ch);
        }
        result.push(' ');
    }
    result.pop();
    result
}

fn decode_missing(s: &str) -> char {
    let n = s.replace('_', "1").replace('.', "0");
    if let Ok(u) = u32::from_str_radix(&n, 2) {
        if let Some(c) = std::char::from_u32(u - 64) {
            return c;
        }
    }
    return ' ';
}