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
use std::vec;
use lazy_static::lazy_static;
use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize};
lazy_static! {
pub static ref CITIES: Cities = get_cities();
}
pub type Cities = Vec<City>;
#[derive(Debug)]
pub struct City {
pub country: String,
pub name: String,
pub lat: f64,
pub lng: f64,
}
type RawCities = Vec<RawCity>;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawCity {
country: String,
name: String,
lat: String,
lng: String,
}
fn load_json_data() -> Vec<u8> {
include_bytes!("../upstream/cities.json").to_vec()
}
fn get_cities() -> Cities {
let raw_cities: RawCities = serde_json::from_slice(&load_json_data()[..]).unwrap();
let mut cities: Cities = vec![];
for rawcity in raw_cities.iter() {
cities.push(City {
country: rawcity.country.to_owned(),
name: rawcity.name.to_owned(),
lat: rawcity.lat.parse::<f64>().unwrap(),
lng: rawcity.lng.parse::<f64>().unwrap(),
})
}
return cities;
}
pub fn get_random_cities() -> &'static City{
return CITIES.choose(&mut rand::thread_rng()).unwrap();
}