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
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate unidecode;
use serde::Deserialize;
use serde::Serialize;
use std::fs::File;
use std::io::Read;
use unidecode::unidecode;
#[derive(Serialize, Deserialize, Debug)]
pub struct City {
country: String,
name: String,
lat: String,
lng: String,
}
pub fn filter_city(filter: &str) -> Vec<City> {
let filter_upper_decode = unidecode(filter).to_ascii_uppercase();
let mut compare_string;
let mut s = String::new();
const PATH: &str = "assets/citys.json";
let mut file_path: std::path::PathBuf = std::path::PathBuf::new();
file_path.push(std::env::current_dir().unwrap().as_path());
file_path.push(PATH);
File::open(file_path.as_path())
.unwrap()
.read_to_string(&mut s)
.unwrap();
let _deserialized: Vec<City> = serde_json::from_str(&s).unwrap();
let mut city: Vec<City> = Vec::new();
for x in &_deserialized {
if filter.len() > 0 {
compare_string =
unidecode(x.name.clone().as_str()).to_ascii_uppercase();
if compare_string.contains(filter_upper_decode.as_str()) {
city.push(City {
country: x.country.clone(),
name: x.name.clone(),
lat: x.lat.clone(),
lng: x.lng.clone(),
});
}
}
}
city
}