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
//! [![ci-badge][]][ci] [![docs-badge][]][docs] [![crate-version]][crate-link]
//!
//! # country-parser
//!
//! A simple country parser library. Currently contains all ISO 3166-1 countries.
//!
//!
//! [ci]: https://github.com/Elinvynia/country-parser/actions?query=workflow%3ARust
//! [ci-badge]: https://img.shields.io/github/workflow/status/Elinvynia/country-parser/Rust/master?style=flat-square
//! [docs]: https://docs.rs/country-parser
//! [docs-badge]: https://img.shields.io/badge/docs-online-5023dd.svg?style=flat-square
//! [crate-link]: https://crates.io/crates/country-parser
//! [crate-version]: https://img.shields.io/crates/v/country-parser.svg?style=flat-square

#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![forbid(unsafe_code)]

#[macro_use]
extern crate lazy_static;

include!(concat!(env!("OUT_DIR"), "/data.rs"));

/// Attempts to parse the input into a valid country, checking all possible fields.
pub fn parse<T: AsRef<str>>(info: T) -> Option<Country> {
    let info = info.as_ref().to_lowercase();
    let countries: &Vec<Country> = &*DATA;

    countries
        .iter()
        .find(|c| {
            info == c.official_name.to_lowercase()
                || info == c.short_name.to_lowercase()
                || info == c.iso2.to_lowercase()
                || info == c.iso3.to_lowercase()
        })
        .cloned()
}

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

    #[test]
    fn test_country() {
        assert_eq!(parse("andorra").unwrap().iso2, "AN".to_string());
    }
}