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
use std::collections::HashMap;
use std::error::Error;
use std::fmt;

use unicode_segmentation::UnicodeSegmentation;

lazy_static! {
    static ref CHARACTER_VALUES: HashMap<&'static str, u8> = hashmap! {
        "🫂" => 200,
        "💖" => 50,
        "✨" => 10,
        "🥺" => 5,
        "," => 1,
        "❤️" => 0,
    };

    static ref CHARACTER_VALUES_REVERSED: HashMap<u8, &'static str> = hashmap! {
        200 => "🫂" ,
        50 => "💖",
        10 => "✨",
        5 => "🥺",
        1 => ",",
        0 => "❤️",
    };
}

#[derive(Debug)]
pub struct TranslationError {
    pub why: String
}

impl fmt::Display for TranslationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.why)
    }
}

impl Error for TranslationError {}

pub fn encode_string(input: &dyn AsRef<str>) -> String {
    input
        .as_ref()
        .chars()
        .map(encode_char)
        .collect::<String>()
}

pub fn encode_char(char: char) -> String {
    let mut buffer = String::new();
    let mut value = char as u8;

    if value == 0 {
        return CHARACTER_VALUES_REVERSED[&0].to_string();
    }

    loop {
        let (to_push, subtract_by) = match value {
            200..=255 => (CHARACTER_VALUES_REVERSED[&200], 200),
            50..=199 => (CHARACTER_VALUES_REVERSED[&50], 50),
            10..=49 => (CHARACTER_VALUES_REVERSED[&10], 10),
            5..=9 => (CHARACTER_VALUES_REVERSED[&5], 5),
            1..=4 => (CHARACTER_VALUES_REVERSED[&1], 1),
            _ => break
        };

        buffer.push_str(to_push);
        value -= subtract_by;
    };

    buffer.push_str("👉👈");
    buffer
}

pub fn decode_string(input: &dyn AsRef<str>) -> Result<String, TranslationError> {
    let r = input.as_ref();
    {
        if r.contains("\u{200B}") {
            r.trim_end_matches("\u{200B}")
            .split("\u{200B}")
        }
        else {
            r.trim_end_matches("👉👈")
            .split("👉👈")
        }
    }
    .map(|c| decode_char(&c))
    .collect::<Result<String, _>>()
}

pub fn decode_char(input: &dyn AsRef<str>) -> Result<char, TranslationError> {
    let result = input
        .as_ref()
        .graphemes(true)
        .map(|g| {
            CHARACTER_VALUES
                .get(g)
                .ok_or(TranslationError {
                    why: format!("Cannot decode character {}", g)
                })
        })
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .sum::<u8>();

    Ok(result as char)
}

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

    #[test]
    fn test_string_encode() {
        assert_eq!(
            encode_string(&"Test"),
            "💖✨✨✨,,,,👉👈💖💖,👉👈💖💖✨🥺👉👈💖💖✨🥺,👉👈".to_string()
        );
    }

    #[test]
    fn test_char_encode() {
        assert_eq!(
            encode_char('h'),
            "💖💖,,,,👉👈".to_string(),
        );
    }

    #[test]
    fn test_string_decode() {
        assert_eq!(
            decode_string(&"💖✨✨✨,,,,\u{200B}💖💖,\u{200B}💖💖✨🥺\u{200B}💖💖✨🥺,\u{200B}").unwrap(),
            "Test".to_string()
        );
    }

    #[test]
    fn test_char_decode() {
        assert_eq!(
            decode_char(&"💖💖,,,,").unwrap(),
            'h',
        );
    }
}