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
use crate::{
    hira,
    kata,
    Result,
};

/// Trait to make a type translatable to kana
pub trait ToKana {
    /// Changes the implementing type to Hiragana
    fn hira(&self) -> Result;
    /// Changes the implementing type to Katakana
    fn kata(&self) -> Result;
}

impl ToKana for &str {
    fn hira(&self) -> Result {
        hira(&self)
    }

    fn kata(&self) -> Result {
        kata(&self)
    }
}

impl ToKana for String {
    fn hira(&self) -> Result {
        hira(&self)
    }

    fn kata(&self) -> Result {
        kata(&self)
    }
}

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

    #[test]
    fn hello_hira_string() {
        let hello = String::from("konnitiha");
        assert_eq!("こんにちは", hello.hira().unwrap());
    }

    #[test]
    fn hello_hira_str() {
        assert_eq!("こんにちは", "konnitiha".hira().unwrap());
    }

    #[test]
    fn world_kata_string() {
        let world = String::from("wa-rudo");
        assert_eq!("ワールド", world.kata().unwrap());
    }

    #[test]
    fn world_kata_str() {
        assert_eq!("ワールド", "wa-rudo".kata().unwrap());
    }
}