Skip to main content

geonames_lib/model/
alternate_name.rs

1use serde::{Deserialize, Serialize};
2use crate::err::{DeserializeErr};
3
4#[derive(Deserialize, Serialize, Clone, Debug)]
5pub struct AlternateName {
6    /// the id of this alternate name, int
7    pub alternate_name_id: i32,
8    /// geonameId referring to id in table 'geoname', int
9    pub geo_name_id: i32,
10    /// iso 639 language code 2- or 3-characters; 4-characters 'post' for postal codes and 'iata','icao' and faac for airport codes, fr_1793 for French Revolution names,  abbr for abbreviation, link to a website (mostly to wikipedia), wkdt for the wikidataid, varchar(7)
11    pub isolanguage: Option<String>,
12    /// alternate name or name variant, varchar(400)
13    pub alternate_name: String,
14    /// '1', if this alternate name is an official/preferred name
15    pub is_preferred_name: bool,
16    /// '1', if this is a short name like 'California' for 'State of California'
17    pub is_short_name: bool,
18    /// '1', if this alternate name is a colloquial or slang term. Example: 'Big Apple' for 'New York'.
19    pub is_colloquial: bool,
20    /// '1', if this alternate name is historic and was used in the past. Example 'Bombay' for 'Mumbai'.
21    pub is_historic: bool,
22    // from period when the name was used
23    //pub from: i32,
24    // to period when the name was used
25    // pub to: i32,
26}
27
28
29impl AlternateName {
30    /// Receive string example
31    /// ```c
32    /// 1554355	5128581	en	Big Apple			1
33    /// ```
34    pub fn deserialize_from_string(str: &str) -> Result<Self, DeserializeErr> {
35        let vec = str.split("	").collect::<Vec<&str>>();
36        Ok(Self {
37            alternate_name_id: vec[0].parse::<i32>()?,
38            geo_name_id: vec[1].parse::<i32>()?,
39            isolanguage: Some(vec[2].to_owned()).filter(|x| !x.is_empty()),
40            alternate_name: vec[3].to_owned(),
41            is_preferred_name: !vec[4].is_empty(),
42            is_short_name: !vec[5].is_empty(),
43            is_colloquial: !vec[6].is_empty(),
44            is_historic: !vec[7].is_empty(),
45        })
46    }
47}
48
49
50#[cfg(test)]
51mod tests {
52    use crate::model::AlternateName;
53
54    #[test]
55    fn deserialize_string_to_alternate_name() {
56        use std::fs::File;
57        use std::io::{BufRead, BufReader};
58
59        for line in BufReader::new(File::open("./test-data/alternate_name_gb.txt").unwrap()).lines() {
60            let _alternate_name = AlternateName::deserialize_from_string(&line.unwrap()).unwrap();
61        }
62    }
63}