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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
// 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-1? // // | ISO 3166-1 is part of the ISO 3166 standard published by the International // | Organization for Standardization (ISO), and defines codes for the names of // | countries, dependent territories, and special areas of geographical // | interest. // | // | - [Wikipedia](http://en.wikipedia.org/wiki/ISO_3166-1) // // Originally by taiyaeix on GitHub. mod codes; pub use codes::all; /// Struct that contains the data for each Country Code defined by ISO 3166-1, /// including the following pieces of information: /// /// - `alpha2` - Two-character Alpha-2 code. /// - `alpha3` - Three-character Alpha-3 code. /// - `name` - English short name of country. /// - `num` - Numeric code of country. /// /// Derives from Clone and Debug. #[derive(Clone, Debug)] pub struct CountryCode<'a> { pub alpha2: &'a str, pub alpha3: &'a str, pub name: &'a str, pub num: &'a str, } /// Returns an `Option` of a `CountryCode` with the given alpha2 code. /// /// # Examples /// /// ```rust /// let country = iso3166_1::alpha2("AF").unwrap(); /// ``` pub fn alpha2<'a>(alpha2: &str) -> Option<CountryCode<'a>> { let mut code_ret: Option<CountryCode> = None; for code in all() { if code.alpha2 == alpha2 { code_ret = Some(code.clone()); break; } } code_ret } /// Returns an `Option` of a `CountryCode` with the given alpha3 code. /// /// # Examples /// /// ```rust /// let country = iso3166_1::alpha3("ATA").unwrap(); /// ``` pub fn alpha3<'a>(alpha3: &str) -> Option<CountryCode<'a>> { let mut code_ret: Option<CountryCode> = None; for code in all() { if code.alpha3 == alpha3 { code_ret = Some(code.clone()); break; } } code_ret } /// Returns an `Option` of a `CountryCode` with the given name. /// /// # Examples /// /// ```rust /// let country = iso3166_1::name("Angola").unwrap(); /// ``` pub fn name<'a>(name: &str) -> Option<CountryCode<'a>> { let mut code_ret: Option<CountryCode> = None; for code in all() { if code.name == name { code_ret = Some(code.clone()); break; } } code_ret } /// Returns an `Option` of a `CountryCode` with the given numeric value. /// /// # Examples /// /// ```rust /// let country = iso3166_1::num("016").unwrap(); /// ``` pub fn num<'a>(num: &str) -> Option<CountryCode<'a>> { let mut code_ret: Option<CountryCode> = None; for code in all() { if code.num == num { code_ret = Some(code.clone()); break; } } code_ret } /// Returns a `Vec` of `CountryCode`s that have a numeric value within the range /// of the `from` and `to` given. The from and to are optional, and can either /// be `None` or `Some(&str)` for variations of the range wanted. /// /// # Examples /// /// Getting all values between `100` and `300`: /// /// ```rust /// let countries = iso3166_1::num_range(Some("100"), Some("300")); /// ``` /// /// Getting all values from `400` and beyond: /// /// ```rust /// let countries = iso3166_1::num_range(Some("400"), None); /// ``` /// /// Getting all values up to `500`: /// /// ```rust /// let countries = iso3166_1::num_range(None, Some("500")); /// ``` /// /// Getting no values, if that's your thing: /// /// ``` /// let countries = iso3166_1::num_range(None, None); /// ``` pub fn num_range<'a>(from: Option<&str>, to: Option<&str>) -> Vec<CountryCode<'a>> { let mut codes: Vec<CountryCode> = vec![]; let from_do: bool = from.is_some(); let to_do: bool = to.is_some(); let from_val: i16 = if from_do { from.unwrap().parse::<i16>().unwrap() } else { 0 }; let to_val: i16 = if to_do { to.unwrap().parse::<i16>().unwrap() } else { 0 }; for code in all() { let num_as_int: i16 = code.num.parse::<i16>().unwrap(); let gte: bool = num_as_int >= from_val; let lte: bool = num_as_int <= to_val; let matches: bool = if from_do && to_do { gte && lte } else if from_do { gte } else if to_do { lte } else { false }; if matches { codes.push(code.clone()); } } codes }