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
use crate::app::parse::traits::{FixedSize, FixedSizeVariation, Index};

use scursor::*;

#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct Prefix<I, V>
where
    I: Index,
    V: FixedSizeVariation,
{
    pub(crate) index: I,
    pub(crate) value: V,
}

impl<I, V> Prefix<I, V>
where
    I: Index,
    V: FixedSizeVariation,
{
    pub(crate) fn equals(&self, other: &(V, I)) -> bool {
        self.index == other.1 && self.value == other.0
    }
}

impl<I, V> FixedSize for Prefix<I, V>
where
    I: Index,
    V: FixedSizeVariation,
{
    const SIZE: u8 = I::SIZE + V::SIZE;

    fn read(cursor: &mut ReadCursor) -> Result<Self, ReadError> {
        Ok(Prefix {
            index: I::read(cursor)?,
            value: V::read(cursor)?,
        })
    }

    fn write(&self, cursor: &mut WriteCursor) -> Result<(), WriteError> {
        self.index.write(cursor)?;
        self.value.write(cursor)
    }
}