aircrafts: mangle more data
This commit is contained in:
parent
f782f1a810
commit
ce25726600
|
@ -3,8 +3,15 @@ use std::io::BufReader;
|
|||
use std::fs::File;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug)]
|
||||
pub struct Aircraft {
|
||||
pub owner: Option<String>,
|
||||
pub registration: Option<String>,
|
||||
pub model: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Record {
|
||||
pub icao24: String,
|
||||
pub registration: String,
|
||||
pub manufacturername: String,
|
||||
|
@ -21,9 +28,26 @@ impl Aircrafts {
|
|||
let mut rdr = csv::Reader::from_reader(BufReader::new(File::open(file).unwrap()));
|
||||
let mut data = HashMap::new();
|
||||
for result in rdr.deserialize() {
|
||||
let aircraft: Aircraft = result.unwrap();
|
||||
if aircraft.registration != "" {
|
||||
data.insert(aircraft.icao24.clone(), aircraft);
|
||||
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)
|
||||
} else {
|
||||
Some(format!("{} {}", rec.manufacturername, rec.model))
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
println!("Loaded aircraft database with {} entries", data.len());
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -36,19 +36,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||
while let Some(event) = events.recv().await {
|
||||
println!("event: {:?}", event);
|
||||
|
||||
let flight = if let Some(flight) = event.info.get_flight() {
|
||||
format!("Flug {} [{}]", flight, event.info.get_hex())
|
||||
} else {
|
||||
format!("[{}]", event.info.get_hex())
|
||||
};
|
||||
let identifier;
|
||||
if let Some(aircraft) = aircrafts.find(&event.info.get_hex()) {
|
||||
println!("aircraft: {:?}", aircraft);
|
||||
// TODO: aircraft.model.starts_with(&aircraft.manufacturername)
|
||||
identifier = format!(
|
||||
"{} {} ({} {})",
|
||||
aircraft.owner, aircraft.registration,
|
||||
aircraft.manufacturername, aircraft.model
|
||||
);
|
||||
} else if let Some(flight) = event.info.get_flight() {
|
||||
identifier = format!("Flug {} [{}]", flight, event.info.get_hex());
|
||||
match (&aircraft.owner, &aircraft.registration, &aircraft.model) {
|
||||
(Some(owner), Some(registration), Some(model)) =>
|
||||
identifier = format!("{} {} ({})", owner, registration, model),
|
||||
(Some(owner), None, Some(model)) =>
|
||||
identifier = format!("{} {} ({})", owner, flight, model),
|
||||
(Some(owner), Some(registration), None) =>
|
||||
identifier = format!("{} {}", owner, registration),
|
||||
(Some(owner), None, None) =>
|
||||
identifier = format!("{} {}", owner, flight),
|
||||
(None, Some(registration), Some(model)) =>
|
||||
identifier = format!("{} ({})", registration, model),
|
||||
(None, None, Some(model)) =>
|
||||
identifier = format!("{} ({})", flight, model),
|
||||
(None, Some(registration), None) =>
|
||||
identifier = format!("{}", registration),
|
||||
(None, None, None) =>
|
||||
identifier = flight,
|
||||
}
|
||||
} else {
|
||||
identifier = format!("[{}]", event.info.get_hex());
|
||||
identifier = flight;
|
||||
}
|
||||
|
||||
let text = match event.action {
|
||||
|
|
Loading…
Reference in New Issue