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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! # postcode-rs
//! [![crates.io](https://img.shields.io/crates/d/postcode)](https://crates.io/crates/postcode)
//! [![crates.io](https://img.shields.io/crates/v/postcode)](https://crates.io/crates/postcode)
//! [![API](https://docs.rs/postcode/badge.svg)](https://docs.rs/postcode)
//!
//! Postcode & Geolocation API for the UK. This is an API client for [postcodes.io](https://postcodes.io/).
//!
//! ```
//! postcode = "0.1.1"
//! ```
//!
//! # Example
//! ```rust
//! use postcode::Postcode;
//!
//! #[async_std::main]
//! async fn main() {
//!     let code = "SW1W0NY";
//!  
//!     let post = Postcode::from_code(code).await.unwrap();
//!
//!     println!("{} ({}, {}) -> ({}, {})", code, post.region, post.country, post.latitude, post.longitude);
//! }
//! ```
//! ```
//! SW1W0NY (London, England) -> (51.495373, -0.147421)
//! ```

use serde_json::{Value, json};
use thiserror::Error;

/// The main struct that defines a postcode.
#[derive(Clone, PartialEq, Debug)]
pub struct Postcode {
    pub postcode: String,
    pub quality: f64,
    pub eastings: f64,
    pub northings: f64,
    pub country: String,
    pub nhs_ha: String,
    pub longitude: f64,
    pub latitude: f64,
    pub european_electoral_region: String,
    pub primary_care_trust: String,
    pub region: String,
    pub lsoa: String,
    pub msoa: String,
    pub incode: String,
    pub outcode: String,
    pub parliamentary_constituency: String,
    pub admin_district: String,
    pub parish: String,
    pub admin_county: String,
    pub admin_ward: String,
    pub ced: String,
    pub ccg: String,
    pub nuts: String,
    pub codes: Codes,
}

/// More obscure codes that describe a postcode.
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct Codes {
    pub admin_district: String,
    pub admin_county: String,
    pub admin_ward: String,
    pub parish: String,
    pub parliamentary_constituency: String,
    pub ccg: String,
    pub ccg_id: String,
    pub ced: String,
    pub nuts: String,
    pub lsoa: String,
    pub msoa: String,
    pub lau2: String,
}

impl Postcode {
    /// Get postcode information from the name.
    pub async fn from_code<T: AsRef<str>>(code: T) -> Result<Self, Error> {
        let res = match surf::get(format!("https://api.postcodes.io/postcodes/{}", code.as_ref())).recv_string().await {
            Ok(data) => data,
            Err(error) => return Err(Error::Http(error.to_string())),
        };

        let v: Value = match serde_json::from_str(&res) {
            Ok(data) => data,
            Err(error) => return Err(Error::Json(error.to_string())),
        };

        match &v["error"] {
            Value::String(error) => return Err(Error::Other(error.to_string())),
            _ => (),
        };

        return Self::json(&v["result"]);
    }

    /// Lookup multiple [Postcode] in a single HTTP request.
    pub async fn from_multi_lookup(codes: Vec<&str>) -> Result<Vec<Self>, Error> {
        let v = json!({ "postcodes": codes });

        let res = match surf::post("https://api.postcodes.io/postcodes")
            .body(v)
            .recv_string()
            .await {
            Ok(data) => data,
            Err(error) => return Err(Error::Http(error.to_string())),
        };

        let v: Value = match serde_json::from_str(&res) {
            Ok(data) => data,
            Err(error) => return Err(Error::Json(error.to_string())),
        };

        match &v["error"] {
            Value::String(error) => return Err(Error::Other(error.to_string())),
            _ => (),
        };

        let result = match &v["result"] {
            Value::Array(output) => output,
            _ => return Err(Error::Json(String::new())),
        };

        let mut array: Vec<Self> = Vec::new();

        for x in result {
            let j = Self::json(&x["result"])?;

            array.push(j);
        }

        return Ok(array);
    }

    /// Get the closest [Postcode] to a given coordinates.
    pub async fn from_coordinates(latitude: f64, longitude: f64) -> Result<Self, Error> {
        let res = match surf::get(format!("http://api.postcodes.io/postcodes?lon={}&lat={}", longitude, latitude)).recv_string().await {
            Ok(data) => data,
            Err(error) => return Err(Error::Http(error.to_string())),
        };

        let v: Value = match serde_json::from_str(&res) {
            Ok(data) => data,
            Err(error) => return Err(Error::Json(error.to_string())),
        };

        match &v["error"] {
            Value::String(error) => return Err(Error::Other(error.to_string())),
            _ => (),
        };

        return Self::json(&v["result"][0]);
    }

    /// Get a random [Postcode].
    pub async fn random() -> Result<Self, Error> {
        let res = match surf::get("https://api.postcodes.io/random/postcodes").recv_string().await {
            Ok(data) => data,
            Err(error) => return Err(Error::Http(error.to_string())),
        };

        let v: Value = match serde_json::from_str(&res) {
            Ok(data) => data,
            Err(error) => return Err(Error::Json(error.to_string())),
        };

        match &v["error"] {
            Value::String(error) => return Err(Error::Other(error.to_string())),
            _ => (),
        };

        return Self::json(&v["result"]);
    }

    fn json(v: &Value) -> Result<Self, Error> {
        let admin_district = v["codes"]["admin_district"].to_string().replace("\"", "");
        let admin_county = v["codes"]["admin_county"].to_string().replace("\"", "");
        let admin_ward = v["codes"]["admin_ward"].to_string().replace("\"", "");
        let parish = v["codes"]["parish"].to_string().replace("\"", "");
        let parliamentary_constituency = v["codes"]["parliamentary_constituency"].to_string().replace("\"", "");
        let ccg = v["codes"]["ccg"].to_string().replace("\"", "");
        let ccg_id = v["codes"]["ccg_id"].to_string().replace("\"", "");
        let ced = v["codes"]["ced"].to_string().replace("\"", "");
        let nuts = v["codes"]["nuts"].to_string().replace("\"", "");
        let lsoa = v["codes"]["lsoa"].to_string().replace("\"", "");
        let msoa = v["codes"]["msoa"].to_string().replace("\"", "");
        let lau2 = v["codes"]["lau2"].to_string().replace("\"", "");

        let codes = Codes {
            admin_district,
            admin_county,
            admin_ward,
            parish,
            parliamentary_constituency,
            ccg,
            ccg_id,
            ced,
            nuts,
            lsoa,
            msoa,
            lau2,
        };


        let postcode = v["postcode"].to_string().replace("\"", "");
        let quality = v["quality"].as_f64().unwrap_or(0.0);
        let eastings = v["eastings"].as_f64().unwrap_or(0.0);
        let northings = v["northings"].as_f64().unwrap_or(0.0);
        let country = v["country"].to_string().replace("\"", "");
        let nhs_ha = v["nhs_ha"].to_string().replace("\"", "");
        let longitude = v["longitude"].as_f64().unwrap_or(0.0);
        let latitude = v["latitude"].as_f64().unwrap_or(0.0);
        let european_electoral_region = v["european_electoral_region"].to_string().replace("\"", "");
        let primary_care_trust = v["primary_care_trust"].to_string().replace("\"", "");
        let region = v["region"].to_string().replace("\"", "");
        let lsoa = v["lsoa"].to_string().replace("\"", "");
        let msoa = v["msoa"].to_string().replace("\"", "");
        let incode = v["incode"].to_string().replace("\"", "");
        let outcode = v["outcode"].to_string().replace("\"", "");
        let parliamentary_constituency = v["parliamentary_constituency"].to_string().replace("\"", "");
        let admin_district = v["admin_district"].to_string().replace("\"", "");
        let parish = v["parish"].to_string().replace("\"", "");
        let admin_county = v["admin_county"].to_string().replace("\"", "");
        let admin_ward = v["admin_ward"].to_string().replace("\"", "");
        let ced = v["ced"].to_string().replace("\"", "");
        let ccg = v["ccg"].to_string().replace("\"", "");
        let nuts = v["codes"].to_string().replace("\"", "");

        let x = Self {
            postcode,
            quality,
            eastings,
            northings,
            country,
            nhs_ha,
            longitude,
            latitude,
            european_electoral_region,
            primary_care_trust,
            region,
            lsoa,
            msoa,
            incode,
            outcode,
            parliamentary_constituency,
            admin_district,
            parish,
            admin_county,
            admin_ward,
            ced,
            ccg,
            nuts,
            codes,
        };

        return Ok(x);
    }
}

/// Validate a UK postcode.
pub async fn validate<T: AsRef<str>>(code: T) -> Result<bool, Error> {
    let res = match surf::get(format!("https://api.postcodes.io/postcodes/{}/validate", code.as_ref())).recv_string().await {
        Ok(data) => data,
        Err(error) => return Err(Error::Http(error.to_string())),
    };

    let v: Value = match serde_json::from_str(&res) {
        Ok(data) => data,
        Err(error) => return Err(Error::Json(error.to_string())),
    };

    let result = match v["result"] {
        Value::Bool(value) => value,
        _ => return Err(Error::Json("result wasn't a bool".to_string())),
    };

    return Ok(result);
}

/// A basic error struct for [Postcode].
#[derive(Error, Debug, Clone, PartialEq, Hash)]
pub enum Error {
    #[error("Http Error: {0}")]
    Http(String),
    #[error("Http Error: {0}")]
    Json(String),
    #[error("Other Error: {0}")]
    Other(String),
}