locale_codes/lib.rs
1/*!
2An interface to all manner of locale-related information.
3
4This crate provides a locale-related codes/identifiers and any standards-based
5information concerning them. For example, ISO-396 language identifiers, or
6ISO-3166 country identifiers. These are under the module
7[`locale_codes::codes`](codes/index.html). These modules are effectively
8registries of standard code/identifiers and any metadate published as a part
9of the associated standard(s). For example, _Codes for the representation of
10currencies_, or ISO 4217 is based on a spreadsheet published directly by ISO
11itself with some additional fields added from other publicly accessible sources.
12
13While there is no formal type system or traits for modules exporting codes, there
14are definitely some patterns all of the current implementations follow.
15
161. modules typically implement a `lookup()` function that returns an `Option`,
171. although where some standards have both alphabetic and numeric identifiers
18 there are `lookup_by_alpha()` and `lookup_by_numeric()` instead, .
191. Most will also include a function `all_codes()` to retrieve a vector of all
20 the known identifiers,
211. or, `all_alpha_codes()` and `all_numeric_codes()` as appropriate.
22
23Some standards, specifically language and country, support 2-character and
243-character alphabetic identifiers, a single `lookup()` function is used to
25lookup either.
26
27## Example - Codes
28
29The following example demonstrates some of the components of the crate, at
30least some reasonable use cases.
31
321. Construct a _strict_ locale string where identifiers are checked against
33 known standard codes where possible.
341. Lookup the ISO-3166 data for the country (in the
35 [`CountryInfo`](codes/country/struct.CountryInfo.html) struct) identified
36 by the ISO-3166, part 2, 3-character identifier.
371. The data fromn the last call contains one or more regions (in the
38 [`RegionInfo`](/codes/region/struct.RegionInfo.html) struct), determine
39 the countries name from the `country_code`.
401. Now we have the country name we can lookup the details of the currencies
41 (in, the [`CurrencyInfo`](CurrencyInfo) struct).
42
43```
44use locale_codes::{country, currency, region};
45
46let mexico = country::lookup("MEX").unwrap();
47println!("{:?}", mexico);
48
49let mexico_region = region::lookup(mexico.country_code).unwrap();
50println!("{:?}", mexico_region);
51
52let currencies = currency::currencies_for_country_name(mexico_region.name.as_str());
53println!("{:?}", currencies);
54```
55
56## JSON Data Files
57
58The script [`create-data-modules`](https://github.com/johnstonskj/locale-codes/blob/master/create-data-modules.sh)
59on the other hand is used to process files downloaded, or scraped, from
60standards web sites to create data used by the library. This data is generated
61as JSON files in the `src/codes/data` folder and read as a part of the
62build for `codes` modules using the Rust `include!` macro.
63
64Currently data is generated for the following standards:
65
66* ISO 639 _Codes for the representation of names of languages_; Parts 1-4,
67 2-character and 3-character codes supported.
68* ISO 3166 _Codes for the representation of names of countries and their
69 subdivisions_; Part 1, 2-character codes, only.
70* ISO 4217 _Codes for the representation of currencies_; alphabetic and
71 numeric codes supported.
72* ISO 15924 _Codes for the representation of names of scripts_; alphabetic
73 and numeric codes supported.
74
75Each folder under `src-data` represents a single standard, which may
76generate one or more data sets. Each directory will contain a Python
77script, `generate.py` which is called by the top-level script to create
78the JSON in the correct location. Each should also contain a README
79that includes attribution for any data retrieved to make this possible.
80*/
81
82#[macro_use]
83extern crate lazy_static;
84#[macro_use]
85extern crate log;
86extern crate serde;
87extern crate serde_json;
88
89// ------------------------------------------------------------------------------------------------
90// Public Modules
91// ------------------------------------------------------------------------------------------------
92
93pub mod codeset;
94
95pub mod country;
96
97pub mod currency;
98
99pub mod language;
100
101pub mod region;
102
103pub mod script;