1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;
use bytes::Bytes;
use crate::header::EventStreamHeader;

mod header;
pub mod raw;

/// Decimation value; a pixel's sensitivity.
pub type D = u8;

/// Number of ticks elapsed since a given pixel last fired an [`pixel::Event`]
pub type DeltaT = u32;

/// Measure of an amount of light intensity
pub type Intensity = f32;

/// Pixel x- or y- coordinate address in the ADΔER model
pub type PixelAddress = u16;

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Coord {
    pub x: PixelAddress,
    pub y: PixelAddress,
    pub c: Option<u8>,
}

/// An ADΔER event representation
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Event {
    pub coord: Coord,
    pub d: D,
    pub delta_t: DeltaT,
}

impl From<&Coord> for Bytes {
    fn from(coord: &Coord) -> Self {
        match coord.c {
            None => {
                Bytes::from([
                    &coord.x.to_be_bytes() as &[u8],
                    &coord.y.to_be_bytes() as &[u8],
                ].concat()
                )
            }
            Some(c) => {
                Bytes::from([
                    &coord.x.to_be_bytes() as &[u8],
                    &coord.y.to_be_bytes() as &[u8],
                    &c.to_be_bytes() as &[u8]
                ].concat()
                )
            }
        }

    }
}

impl From<&Event> for Bytes {
    fn from(event: &Event) -> Self {
        Bytes::from([
            &Bytes::from(&event.coord).to_vec() as &[u8],
            &event.d.to_be_bytes() as &[u8],
            &event.delta_t.to_be_bytes() as &[u8]
        ].concat()
        )
    }
}

pub trait Codec {
    fn new() -> Self;

    fn open_writer<P: AsRef<Path>>(&mut self, path: P) -> Result<(), std::io::Error>{
        let file = File::create(&path)?;
        self.set_output_stream(Some(BufWriter::new(file)));
        Ok(())
    }

    fn open_reader<P: AsRef<Path>>(&mut self, path: P) -> Result<(), std::io::Error>{
        let file = File::open(&path)?;
        self.set_input_stream(Some(BufReader::new(file)));
        Ok(())
    }

    /// Flush the stream so that program can be exited safely
    fn flush_writer(&mut self);
    fn close_writer(&mut self);

    /// Close the stream so that program can be exited safely
    fn close_reader(&mut self);

    fn set_output_stream(&mut self, stream: Option<BufWriter<File>>);
    fn set_input_stream(&mut self, stream: Option<BufReader<File>>);

    fn encode_header(&mut self,
                     width: u16,
                     height: u16,
                     tps: u32,
                     ref_interval: u32,
                     delta_t_max: u32,
                     channels: u8);

    fn decode_header(&mut self);

    fn encode_event(&mut self, event: &Event);
    fn encode_events(&mut self, events: &Vec<Event>);
    fn decode_event(&mut self) -> Result<Event, std::io::Error>;


}






#[cfg(test)]
mod tests {
    // use crate::EventStreamHeader;
    // use crate::header::MAGIC_RAW;

    #[test]
    fn encode_raw() {

    }
}