aircrafts: Arc all the Strings
This commit is contained in:
parent
61bf30b4dd
commit
19bc7765d7
|
@ -1,13 +1,14 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::sync::Arc;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Aircraft {
|
pub struct Aircraft {
|
||||||
pub owner: Option<String>,
|
pub owner: Option<Arc<String>>,
|
||||||
pub registration: Option<String>,
|
pub registration: Option<Arc<String>>,
|
||||||
pub model: Option<String>,
|
pub model: Option<Arc<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
@ -25,27 +26,30 @@ pub struct Aircrafts {
|
||||||
|
|
||||||
impl Aircrafts {
|
impl Aircrafts {
|
||||||
pub fn load(file: &str) -> Self {
|
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 data = HashMap::new();
|
let mut nullable = move |s: String| {
|
||||||
for result in rdr.deserialize() {
|
|
||||||
let rec: Record = result.unwrap();
|
|
||||||
if rec.registration != "" {
|
|
||||||
fn nullable(s: String) -> Option<String> {
|
|
||||||
if s.len() > 0 {
|
if s.len() > 0 {
|
||||||
Some(s)
|
let e = strings.entry(s.clone())
|
||||||
|
.or_insert_with(|| Arc::new(s));
|
||||||
|
Some(e.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
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 != "" {
|
||||||
data.insert(rec.icao24, Aircraft {
|
data.insert(rec.icao24, Aircraft {
|
||||||
owner: nullable(rec.owner),
|
owner: nullable(rec.owner),
|
||||||
registration: nullable(rec.registration),
|
registration: nullable(rec.registration),
|
||||||
model: if rec.manufacturername == "" && rec.model == "" {
|
model: if rec.manufacturername == "" && rec.model == "" {
|
||||||
None
|
None
|
||||||
} else if rec.model.starts_with(&rec.manufacturername) {
|
} else if rec.model.starts_with(&rec.manufacturername) {
|
||||||
Some(rec.model)
|
nullable(rec.model)
|
||||||
} else {
|
} else {
|
||||||
Some(format!("{} {}", rec.manufacturername, rec.model))
|
nullable(format!("{} {}", rec.manufacturername, rec.model))
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user