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
use uuid::Uuid;
use std::mem;
use std::fmt::{Display, Formatter};
use crate::id::BoxId;

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum BoxType {
    Id(BoxId),
    UUID(Uuid)
}

impl Display for BoxType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            BoxType::Id(id) => Display::fmt(id, f),
            BoxType::UUID(uuid) => Display::fmt(uuid, f)
        }
    }
}

impl PartialEq<BoxId> for BoxType {
    fn eq(&self, other: &BoxId) -> bool {
        match self {
            BoxType::Id(id) => id == other,
            BoxType::UUID(_) => false
        }
    }
}

impl PartialEq<Uuid> for BoxType {
    fn eq(&self, other: &Uuid) -> bool {
        match self {
            BoxType::Id(_) => false,
            BoxType::UUID(id) => id == other
        }
    }
}

impl PartialEq<&[u8;4]> for BoxType {
    fn eq(&self, other: &&[u8;4]) -> bool {
        match self {
            BoxType::Id(id) => id == other,
            BoxType::UUID(_) => false
        }
    }
}

impl From<BoxId> for BoxType {
    fn from(id: BoxId) -> Self {
        Self::Id(id)
    }
}

impl From<[u8;4]> for BoxType {
    fn from(id: [u8; 4]) -> Self {
        Self::Id(id.into())
    }
}

impl From<&[u8;4]> for BoxType {
    fn from(id: &[u8; 4]) -> Self {
        Self::Id((*id).into())
    }
}

impl From<Uuid> for BoxType {
    fn from(id: Uuid) -> Self {
        Self::UUID(id)
    }
}

impl PartialEq<uuid::Bytes> for BoxType {
    fn eq(&self, other: &uuid::Bytes) -> bool {
        match self {
            BoxType::Id(_) => false,
            BoxType::UUID(id) => id.as_bytes() == other
        }
    }
}

impl BoxType {
    pub fn byte_size(&self) -> usize {
        match self {
            BoxType::Id(_) => BoxId::size(),
            BoxType::UUID(_) => BoxId::size() + mem::size_of::<uuid::Bytes>()
        }
    }
}