ED_LRR/rust/src/galaxy.rs

84 lines
1.8 KiB
Rust

use serde::Deserialize;
use serde_json;
use std::io::{BufRead, BufReader};
use std::str;
#[derive(Debug, Deserialize)]
struct Coords {
x: f32,
y: f32,
z: f32,
}
#[derive(Debug, Deserialize)]
struct Body {
name: String,
#[serde(rename = "type")]
body_type: String,
#[serde(rename = "subType")]
sub_type: String,
#[serde(rename = "distanceToArrival")]
distance: f32,
}
#[derive(Debug, Deserialize)]
struct System {
coords: Coords,
name: String,
bodies: Vec<Body>,
}
/*
pub id: u32,
pub star_type: String,
pub system: String,
pub body: String,
pub mult: f32,
pub distance: u32,
pub x: f32,
pub y: f32,
pub z: f32,
*/
fn main() -> std::io::Result<()> {
better_panic::install();
let mut buffer = String::new();
let mut bz2_reader = std::process::Command::new("bzip2")
.args(&["-d", "-c", r#"E:\EDSM\galaxy.json.bz2"#])
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to spawn execute bzip2!");
let mut reader = BufReader::new(
bz2_reader
.stdout
.as_mut()
.expect("Failed to open stdout of child process"),
);
let mut count = 0;
while let Ok(n) = reader.read_line(&mut buffer) {
if n == 0 {
break;
}
buffer = buffer
.trim()
.trim_end_matches(|c| c == ',')
.trim()
.to_string();
if let Ok(sys) = serde_json::from_str::<System>(&buffer) {
for b in &sys.bodies {
if b.body_type == "Star" {
count += 1;
if (count % 100_000) == 0 {
println!("{}: {:?}", count, b);
}
}
}
}
buffer.clear();
}
println!("Total: {}", count);
Ok(())
}