country_parser/lib.rs
1//! [![ci-badge][]][ci] [![docs-badge][]][docs] [![crate-version]][crate-link]
2//!
3//! # country-parser
4//!
5//! A simple country parser library. Currently contains all ISO 3166-1 countries.
6//!
7//!
8//! [ci]: https://github.com/Elinvynia/country-parser/actions?query=workflow%3ARust
9//! [ci-badge]: https://img.shields.io/github/workflow/status/Elinvynia/country-parser/Rust/master?style=flat-square
10//! [docs]: https://docs.rs/country-parser
11//! [docs-badge]: https://img.shields.io/badge/docs-online-5023dd.svg?style=flat-square
12//! [crate-link]: https://crates.io/crates/country-parser
13//! [crate-version]: https://img.shields.io/crates/v/country-parser.svg?style=flat-square
14
15#![warn(missing_debug_implementations)]
16#![warn(missing_docs)]
17#![forbid(unsafe_code)]
18
19#[macro_use]
20extern crate lazy_static;
21
22include!(concat!(env!("OUT_DIR"), "/data.rs"));
23
24/// Attempts to parse the input into a valid country, checking all possible fields.
25pub fn parse<T: AsRef<str>>(info: T) -> Option<Country> {
26 let info = info.as_ref().to_lowercase();
27 let countries: &Vec<Country> = &*DATA;
28
29 countries
30 .iter()
31 .find(|c| {
32 info == c.official_name.to_lowercase()
33 || info == c.short_name.to_lowercase()
34 || info == c.iso2.to_lowercase()
35 || info == c.iso3.to_lowercase()
36 })
37 .cloned()
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn test_country() {
46 assert_eq!(parse("andorra").unwrap().iso2, "AN".to_string());
47 }
48}