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
// ISC License (ISC)
//
// Copyright (c) 2016, Austin Hellyer <hello@austinhellyer.me>
//
// 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 3166-3?
// | ISO 3166-3 is part of the ISO 3166 standard published by the International
// | Organization for Standardization (ISO), and defines codes for country
// | names which have been deleted from ISO 3166-1 since its first publication
// | in 1974.
// |
// | - [Wikipedia](http://en.wikipedia.org/wiki/ISO_3166-3)
//
// Originally by taiyaeix on GitHub.

mod codes;

use codes::former_country_codes;

/// Struct containing a Former Country Code's ISO 3166-1 information,
/// containing 3 pieces of information:
///
/// - alpha2: Two-character code for the country code;
/// - alpha3: Three-character code for the country code;
/// - num: Three digit code assigned to the country code.
///
/// This struct derives Clone and Debug.
#[derive(Clone, Debug)]
pub struct FormerCountryCodeCodes<'a> {
    alpha2: &'a str,
    alpha3: &'a str,
    num: &'a str,
}

/// Struct defining a Former Country Code as defined by ISO 3166-3. This
/// contains 5 pieces of information:
///
/// - code: Four-letter code assigned for former country name;
/// - codes_former: Field containing a FormerCountryCodeCodes struct;
/// - description: Reason why code was deprecated (eg: country merge, divided);
/// - name: The former country name;
/// - validity: The period in years of which this code was valid.
///
/// This struct derives Clone and Debug.
#[derive(Clone, Debug)]
pub struct FormerCountryCode<'a> {
    pub code: &'a str,
    pub codes_former: FormerCountryCodeCodes<'a>,
    pub description: &'a str,
    pub name: &'a str,
    pub validity: [i16; 2],
}

/// Returns an Option<Vec<FormerCountryCode>> of all FormerCountryCodes defined
/// by ISO 3166-3.
///
/// # Examples
///
/// An example of a valid usage:
///
/// ```
/// extern crate iso3166_3;
///
/// fn main() {
///     let countries = iso3166_3::all().unwrap();
/// }
/// ```
pub fn all<'a>() -> Option<Vec<FormerCountryCode<'a>>> {
    let codes: Vec<FormerCountryCode> = former_country_codes();

    if codes.len() > 0 {
        Some(codes)
    } else {
        None
    }
}

/// Returns an Option<FormerCountryCode> of the FormerCountryCode with the
/// given code.
///
/// # Examples
///
/// An example of a valid usage:
///
/// ```
/// extern crate iso3166_3;
///
/// fn main() {
///     let countries = iso3166_3::code("DDDE").unwrap();
/// }
/// ```
pub fn code<'a>(code_given: &str) -> Option<FormerCountryCode<'a>> {
    let mut code_ret: Option<FormerCountryCode> = None;

    for code in former_country_codes() {
        if code.code == code_given {
            code_ret = Some(code.clone());

            break;
        }
    }

    code_ret
}

/// Returns an Option<Vec<FormerCountryCode>> of ISO 3166-3 country codes that
/// were valid from a range of two given years.
///
/// # Examples
///
/// Retrieve codes valid from years 1974-1990:
///
/// ```
/// extern crate iso3166_3;
///
/// fn main() {
///     let countries = iso3166_3::validity(Some(1974), Some(1990)).unwrap();
/// }
/// ```
///
/// Retrieve codes valid from the year 1990 onward:
///
/// ```
/// extern crate iso3166_3;
///
/// fn main() {
///     let countries = iso3166_3::validity(Some(1990), None);
/// }
/// ```
///
/// Retrieve codes valid until 1998 and prior:
///
/// ```
/// extern crate iso3166_3;
///
/// fn main() {
///     let countries = iso3166_3::validity(None, Some(1998));
/// }
/// ```
pub fn validity<'a>(from: Option<i16>,
                    to: Option<i16>) -> Option<Vec<FormerCountryCode<'a>>> {
    let mut codes: Vec<FormerCountryCode> = vec![];

    let do_from: bool = from.is_some();
    let do_to: bool = to.is_some();

    let val_from: i16 = if do_from { from.unwrap() } else { 0 };
    let val_to: i16 = if do_to { to.unwrap() } else { 0 };

    for code in former_country_codes() {
        let matches: bool = if do_from && do_to {
            code.validity[0] >= val_from && code.validity[1] <= val_to
        } else if do_from {
            code.validity[0] >= val_from
        } else if do_to {
            code.validity[1] <= val_to
        } else {
            false
        };

        if matches {
            codes.push(code.clone());
        }
    }

    if codes.len() > 0 {
        Some(codes)
    } else {
        None
    }
}