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
//! `accept-language` is a tiny library for parsing the Accept-Language header from browsers (as defined [here](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)).
//!
//! It's intended to be used in a webserver that supports some level of internationalization (i18n),
//! but can be used anytime an Accept-Language header string is available.
//!
//! In order to help facilitate better i18n, a function is provided to return the intersection of
//! the languages the user prefers and the languages your application supports.
//!
//! # Example
//!
//! ```
//! use accept_language::{intersection, parse};
//!
//! let user_languages = parse("en-US, en-GB;q=0.5");
//! let common_languages = intersection("en-US, en-GB;q=0.5", vec!["en-US", "de", "en-GB"]);
//! ```
use std::str;
use std::str::FromStr;
use std::cmp::Ordering;

#[derive(Debug)]
struct Language {
    name: String,
    quality: f64
}

impl Eq for Language {}

impl Ord for Language {
    fn cmp(&self, other: &Language) -> Ordering {
        if self.quality > other.quality {
            Ordering::Less
        } else if self.quality < other.quality {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    }
}

impl PartialOrd for Language {
    fn partial_cmp(&self, other: &Language) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Language {
    fn eq(&self, other: &Language) -> bool {
        self.quality == other.quality
    }
}

impl Language {
    fn new(tag: &str) -> Language {
        let mut tag_parts = tag.split(";");
        let name = match tag_parts.nth(0) {
            Some(name_str) => name_str.to_string(),
            None => String::from("")
        };
        let quality = match tag_parts.nth(0) {
            Some(quality_str) => Language::quality_with_default(quality_str),
            None => 1.0
        };

        Language {
            name: name,
            quality: quality
        }
    }

    fn quality_with_default(raw_quality: &str) -> f64 {
        let quality_str = &raw_quality[2..];

        match f64::from_str(&quality_str) {
            Ok(q) => q,
            Err(_) => 0.0
        }
    }
}

/// Parse a raw Accept-Language header value into an ordered list of language tags.
/// This should return the exact same list as `window.navigator.languages` in supported browsers.
///
/// # Example
///
/// ```
/// use accept_language::parse;
///
/// let user_languages = parse("en-US, en-GB;q=0.5");
/// ```
pub fn parse(raw_languages: &str) -> Vec<String> {
    let stripped_languages = raw_languages.clone().replace(" ", "");
    let language_strings: Vec<&str> = stripped_languages.split(",").collect();
    let mut languages: Vec<Language> = language_strings
        .iter()
        .map(|l| Language::new(l))
        .collect();

    languages.sort();

    languages
        .iter()
        .map(|ref l| l.name.to_owned())
        .filter(|l| !l.is_empty())
        .collect()
}

/// Compare an Accept-Language header value with your application's supported languages to find
/// the common languages that could be presented to a user.
///
/// # Example
///
/// ```
/// use accept_language::intersection;
///
/// let common_languages = intersection("en-US, en-GB;q=0.5", vec!["en-US", "de", "en-GB"]);
/// ```
pub fn intersection(raw_languages: &str, supported_languages: Vec<&str>) -> Vec<String> {
    let user_languages = parse(raw_languages);
    let intersection = user_languages
        .into_iter()
        .filter(|l| supported_languages.contains(&l.as_str()))
        .map(|l| l.to_string())
        .collect();

    intersection
}

#[cfg(test)]
mod tests {
    use super::{intersection, Language, parse};

    static MOCK_ACCEPT_LANGUAGE: &str = "en-US, de;q=0.7, jp;q=0.1";

    #[test]
    fn it_creates_a_new_language_from_a_string() {
        let language = Language::new("en-US;q=0.7");

        assert_eq!(language, Language { name: String::from("en-US"), quality: 0.7 })
    }

    #[test]
    fn it_creates_a_new_language_from_a_string_with_a_default_quality() {
        let language = Language::new("en-US");

        assert_eq!(language, Language { name: String::from("en-US"), quality: 1.0 })
    }

    #[test]
    fn it_parses_quality() {
        let quality = Language::quality_with_default("q=0.5");

        assert_eq!(quality, 0.5)
    }

    #[test]
    fn it_parses_an_invalid_quality() {
        let quality = Language::quality_with_default("q=yolo");

        assert_eq!(quality, 0.0)
    }

    #[test]
    fn it_parses_a_valid_accept_language_header() {
        let user_languages = parse(MOCK_ACCEPT_LANGUAGE);

        assert_eq!(user_languages, vec![String::from("en-US"), String::from("de"), String::from("jp")])
    }

    #[test]
    fn it_parses_an_empty_accept_language_header() {
        let user_languages = parse("");

        assert_eq!(user_languages.len(), 0)
    }

    #[test]
    fn it_sorts_languages_by_quality() {
        let user_languages = parse("en-US, de;q=0.1, jp;q=0.7");

        assert_eq!(user_languages, vec![String::from("en-US"), String::from("jp"), String::from("de")])
    }

    #[test]
    fn it_returns_language_intersections() {
        let common_languages = intersection(MOCK_ACCEPT_LANGUAGE, vec!["en-US", "jp"]);

        assert_eq!(common_languages, vec![String::from("en-US"), String::from("jp")])
    }

    #[test]
    fn it_returns_an_empty_array_when_no_intersections() {
        let common_languages = intersection(MOCK_ACCEPT_LANGUAGE, vec!["fr", "en-GB"]);

        assert_eq!(common_languages.len(), 0)
    }
}