use std::mem;
use std::io::{Read, Write, Result};
use std::path::Path;
use std::fs::File;

#[derive(Debug)]
#[repr(C)]
struct Header {
    name: [u8; 4],
    size: u32,
    reserved: u64,
}

fn parse<P: AsRef<Path>>(path: P) -> Result<()> {
    let mut file = File::open(path)?;

    let header: Header = {
        let mut h = [0u8; mem::size_of::<Header>()];
        file.read_exact(&mut h[..])?;
        unsafe { mem::transmute(h) }
    };

    println!("{:?}", header);
    Ok(())
}
fn write<P: AsRef<Path>>(path: P, h: Header) -> Result<()> {
    let mut file = File::create(path)?;
    let bytes: [u8; mem::size_of::<Header>()] = unsafe { mem::transmute(h) };
    file.write_all(&bytes)?;

    Ok(())
}

pub fn run_main() {
    let h = Header {
        name: [1,2,3,4],
        size: 0xabcdef,
        reserved: 0x0123456789abcdef,
    };

    write("/tmp/test.dat", h).unwrap();
    parse("/tmp/test.dat").unwrap();
}