1use uuid::Uuid;
2use std::mem;
3use std::fmt::{Display, Formatter};
4use crate::id::BoxId;
5
6#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub enum BoxType {
8 Id(BoxId),
9 UUID(Uuid)
10}
11
12impl Display for BoxType {
13 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14 match self {
15 BoxType::Id(id) => Display::fmt(id, f),
16 BoxType::UUID(uuid) => Display::fmt(uuid, f)
17 }
18 }
19}
20
21impl PartialEq<BoxId> for BoxType {
22 fn eq(&self, other: &BoxId) -> bool {
23 match self {
24 BoxType::Id(id) => id == other,
25 BoxType::UUID(_) => false
26 }
27 }
28}
29
30impl PartialEq<Uuid> for BoxType {
31 fn eq(&self, other: &Uuid) -> bool {
32 match self {
33 BoxType::Id(_) => false,
34 BoxType::UUID(id) => id == other
35 }
36 }
37}
38
39impl PartialEq<&[u8;4]> for BoxType {
40 fn eq(&self, other: &&[u8;4]) -> bool {
41 match self {
42 BoxType::Id(id) => id == other,
43 BoxType::UUID(_) => false
44 }
45 }
46}
47
48impl From<BoxId> for BoxType {
49 fn from(id: BoxId) -> Self {
50 Self::Id(id)
51 }
52}
53
54impl From<[u8;4]> for BoxType {
55 fn from(id: [u8; 4]) -> Self {
56 Self::Id(id.into())
57 }
58}
59
60impl From<&[u8;4]> for BoxType {
61 fn from(id: &[u8; 4]) -> Self {
62 Self::Id((*id).into())
63 }
64}
65
66impl From<Uuid> for BoxType {
67 fn from(id: Uuid) -> Self {
68 Self::UUID(id)
69 }
70}
71
72impl PartialEq<uuid::Bytes> for BoxType {
73 fn eq(&self, other: &uuid::Bytes) -> bool {
74 match self {
75 BoxType::Id(_) => false,
76 BoxType::UUID(id) => id.as_bytes() == other
77 }
78 }
79}
80
81impl BoxType {
82 pub fn byte_size(&self) -> usize {
83 match self {
84 BoxType::Id(_) => BoxId::size(),
85 BoxType::UUID(_) => BoxId::size() + mem::size_of::<uuid::Bytes>()
86 }
87 }
88}