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
// ISC License (ISC)
//
// Copyright (c) 2016, Zeyla Hellyer <zeylahellyer@gmail.com>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
// RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
// CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// What is ISO 4217?
//
// | ISO 4217 is a standard published by the International Organization for
// | Standardization, which delineates currency designators, country codes
// | (alpha and numeric), and references to minor units in three tables.
// |
// | - [Wikipedia](http://en.wikipedia.org/wiki/ISO_4217)
//
// Originally by zeyla on GitHub.

mod codes;

pub use codes::all;

/// Data for each Currency Code defined by ISO 4217.
#[derive(Clone, Debug)]
pub struct CurrencyCode {
    /// 3-letter code of the currency
    pub alpha3: &'static str,
    /// Vector of Alpha2 codes for the countries that use the currency
    pub countries: &'static [&'static str],
    /// Number of decimals
    pub exp: i8,
    /// Fully readable and used name
    pub name: &'static str,
    /// Assigned 3-digit numeric code
    pub num: &'static str,
}

/// Returns the CurrencyCode with the given Alpha3 code, if one exists.
pub fn alpha3(alpha3: &str) -> Option<&'static CurrencyCode> {
    all().iter().find(|c| c.alpha3 == alpha3)
}

/// Returns a vector of all CurrencyCodes that use a given Alpha2 code.
pub fn country(country: &str) -> Vec<&'static CurrencyCode> {
    all().iter().filter(|c| c.countries.contains(&country)).collect()
}

/// Returns a vector of all CurrencyCodes with the specified decimal place.
pub fn exp(exp: i8) -> Vec<&'static CurrencyCode> {
    all().iter().filter(|c| c.exp == exp).collect()
}

/// Returns the CurrencyCode with the given name, if one exists.
pub fn name(name: &str) -> Option<&'static CurrencyCode> {
    all().iter().find(|c| c.name == name)
}

/// Returns the CurrencyCode with the given numerical code, if one exists.
pub fn num(num: &str) -> Option<&'static CurrencyCode> {
    all().iter().find(|c| c.num == num)
}