// Taken from https://github.com/kostya/benchmarks/blob/master/json/json.rs/src/json_struct.rs

extern crate memmap;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use memmap::Mmap;

use std::fs::File;
use std::str;

#[derive(Deserialize)]
pub struct Coordinate {
    x: f64,
    y: f64,
    z: f64,
}

#[derive(Deserialize)]
pub struct TestStruct  {
    coordinates: Vec<Coordinate>,
}

fn main() {
    let file = File::open("sample.json").unwrap();
    let mmap = unsafe { Mmap::map(&file).unwrap() };
    let s = str::from_utf8(&mmap[..]).unwrap();

    let jobj: TestStruct = serde_json::from_str(s).unwrap();

    let len = jobj.coordinates.len() as f64;
    let mut x = 0_f64;
    let mut y = 0_f64;
    let mut z = 0_f64;

    for coord in &jobj.coordinates {
        x += coord.x;
        y += coord.y;
        z += coord.z;
    }

    println!("{}", x / len);
    println!("{}", y / len);
    println!("{}", z / len);
}