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
use serde::ser::{Serialize, Serializer};
use serde::de::{Visitor,Deserialize, Deserializer, Error};

use std::fmt;

/**
* Enum defines API.ai supported languages and their text values used on the server
*
*/
#[derive(PartialEq)]
pub enum Language {
    BrazilianPortuguese,
    ChineseCantonese,
    ChineseSimplified,
    ChineseTraditional,
    English,
    Dutch,
    French,
    German,
    Italian,
    Japanese,
    Korean,
    Portuguese,
    Russian,
    Spanish,
    Ukranian,
}

impl Language{

    /**
    * This function can be called by a language enum instance to get the associated string
    * for use inside the api
    */
    pub fn value(&self) -> &'static str {

        match *self {
            Language::BrazilianPortuguese => "pt-BR",
            Language::ChineseCantonese => "zh-HK",
            Language::ChineseSimplified=> "zh-CN",
            Language::ChineseTraditional => "zh-TW",
            Language::English => "en",
            Language::Dutch => "nl",
            Language::French => "fr",
            Language::German => "de",
            Language::Italian => "it",
            Language::Japanese => "ja",
            Language::Korean => "ko",
            Language::Portuguese => "pt",
            Language::Russian => "ru",
            Language::Spanish => "es",
            Language::Ukranian => "uk"
        }

    }

}

#[derive(Debug)]
struct LanguageVisitor;


impl Visitor for LanguageVisitor {
    type Value = Language;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a language string as specified by api.ai docs.")
    }

    fn visit_str<E>(self, value: &str) -> Result<Language, E>
        where E: Error
    {
        match value {
            "pt-BR" => Ok(Language::BrazilianPortuguese),
            "zh-HK" => Ok(Language::ChineseCantonese),
            "zh-CN" => Ok(Language::ChineseSimplified),
            "zh-TW" => Ok(Language::ChineseTraditional),
            "en" => Ok(Language::English),
            "nl" => Ok(Language::Dutch),
            "fr" => Ok(Language::French),
            "de" => Ok(Language::German),
            "it" => Ok(Language::Italian),
            "ja" => Ok(Language::Japanese),
            "ko" => Ok(Language::Korean),
            "pt" => Ok(Language::Portuguese),
            "ru" => Ok(Language::Russian),
            "es" => Ok(Language::Spanish),
            "uk" => Ok(Language::Ukranian),
             _ => Err(E::custom(format!("Language {} was not a supported string.", value).as_str()  ))
        }
    }

    fn visit_string<E>(self, value: String) -> Result<Language, E>
        where E: Error
    {
        self.visit_str(value.as_str())
    }

}

// JSON value representation
impl Serialize for Language{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer {
        serializer.serialize_str(self.value())
    }
}

impl Default for Language {
    fn default() -> Language { Language::English }
}

impl fmt::Debug for Language {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value())
    }
}

impl Deserialize for Language {
    fn deserialize<D>(deserializer: D) -> Result<Language, D::Error>
        where D: Deserializer
    {
        deserializer.deserialize_str(LanguageVisitor)
    }
}