aircrafts: Arc all the Strings

This commit is contained in:
Astro 2021-10-30 21:01:12 +02:00
parent 61bf30b4dd
commit 19bc7765d7

View File

@ -1,13 +1,14 @@
use std::collections::HashMap;
use std::io::BufReader;
use std::fs::File;
use std::sync::Arc;
use serde::Deserialize;
#[derive(Debug)]
pub struct Aircraft {
pub owner: Option<String>,
pub registration: Option<String>,
pub model: Option<String>,
pub owner: Option<Arc<String>>,
pub registration: Option<Arc<String>>,
pub model: Option<Arc<String>>,
}
#[derive(Debug, Deserialize)]
@ -25,27 +26,30 @@ pub struct Aircrafts {
impl Aircrafts {
pub fn load(file: &str) -> Self {
let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap()));
let mut strings = HashMap::new();
let mut nullable = move |s: String| {
if s.len() > 0 {
let e = strings.entry(s.clone())
.or_insert_with(|| Arc::new(s));
Some(e.clone())
} else {
None
}
};
let mut data = HashMap::new();
let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap()));
for result in rdr.deserialize() {
let rec: Record = result.unwrap();
if rec.registration != "" {
fn nullable(s: String) -> Option<String> {
if s.len() > 0 {
Some(s)
} else {
None
}
}
data.insert(rec.icao24, Aircraft {
owner: nullable(rec.owner),
registration: nullable(rec.registration),
model: if rec.manufacturername == "" && rec.model == "" {
None
} else if rec.model.starts_with(&rec.manufacturername) {
Some(rec.model)
nullable(rec.model)
} else {
Some(format!("{} {}", rec.manufacturername, rec.model))
nullable(format!("{} {}", rec.manufacturername, rec.model))
},
});
}