Skip to content

Commit

Permalink
Added ICAO code <> country
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecardleitao committed Feb 10, 2024
1 parent 50cb605 commit c2de60b
Show file tree
Hide file tree
Showing 4 changed files with 1,342 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/aircraft_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reqwest;
use serde::{Deserialize, Serialize};
use serde_json;

use crate::{fs, fs_s3};
use crate::{fs, fs_s3, CountryIcaoRanges};

/// [`HashMap`] between tail number (e.g. "OY-TWM") and an [`Aircraft`]
pub type Aircrafts = HashMap<String, Aircraft>;
Expand All @@ -23,6 +23,8 @@ pub struct Aircraft {
pub type_designator: String,
/// The model
pub model: String,
/// The country in ISO 3166 of the aircraft
pub country: Arc<str>,
}

static DATABASE: &'static str = "db-20231106";
Expand Down Expand Up @@ -110,6 +112,8 @@ async fn children<'a: 'async_recursion>(
pub async fn load_aircrafts(
client: Option<&fs_s3::ContainerClient>,
) -> Result<Aircrafts, Box<dyn Error>> {
let country_ranges = CountryIcaoRanges::new();

let prefixes = (b'A'..=b'F').chain(b'0'..b'9');
let prefixes = prefixes.map(|x| std::str::from_utf8(&[x]).unwrap().to_string());

Expand All @@ -135,13 +139,15 @@ pub async fn load_aircrafts(
let tail_number = std::mem::take(&mut data[0])?;
let type_designator = std::mem::take(&mut data[1])?;
let model = std::mem::take(&mut data[3])?;
let country = country_ranges.country(&icao_number).unwrap();
Some((
tail_number.clone(),
Aircraft {
icao_number: icao_number.to_ascii_lowercase().into(),
tail_number,
type_designator,
model,
country: country.clone(),
},
))
});
Expand Down
Loading

0 comments on commit c2de60b

Please sign in to comment.