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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::{Ecc, Error, Result};
use std::ops::{Deref, DerefMut};
use uuid::Uuid;

/// Identifier type as specified in the hff header.
/// This has no impact on behavior at all, it is only a
/// hint to the end user about how to use/view the ID's.
#[repr(u32)]
pub enum IdType {
    /// A simple u128.
    Id = 0,
    /// Dual eight character codes.
    Ecc2 = 1,
    /// A UUID.
    Uuid = 2,
    /// An array of u8.
    Au8 = 3,
    /// An eight character code and a u64.
    EccU64 = 4,
    /// Two u64's.
    U64s = 5,
}

impl Deref for IdType {
    type Target = u32;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Id => &0,
            Self::Ecc2 => &1,
            Self::Uuid => &2,
            Self::Au8 => &3,
            Self::EccU64 => &4,
            Self::U64s => &5,
        }
    }
}

impl From<u32> for IdType {
    fn from(value: u32) -> Self {
        match value {
            0 => Self::Id,
            1 => Self::Ecc2,
            2 => Self::Uuid,
            3 => Self::Au8,
            4 => Self::EccU64,
            5 => Self::U64s,
            _ => panic!("Invalid identifier type in header."),
        }
    }
}

/// An identifier for the tables and chunks.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Identifier(u128);

impl Identifier {
    /// An invalid identifier.
    pub const INVALID: Self = Self(0);

    /// Create a new instance.
    pub fn new(id: u128) -> Self {
        Self(id)
    }

    // Conversions back to specific identifier types.

    /// Convert to a pair of u64's.
    pub fn as_u64s(self) -> (u64, u64) {
        ((self.0 >> 64) as u64, self.0 as u64)
    }

    /// Convert to dual ecc's.
    pub fn as_ecc2(self) -> (Ecc, Ecc) {
        let (l, r) = self.as_u64s();
        (l.into(), r.into())
    }

    /// Convert to an Uuid.
    pub fn as_uuid(self) -> Uuid {
        Uuid::from_u128(self.0)
    }

    /// Convert to an array of u8.
    pub fn as_au8(self) -> [u8; 16] {
        #[cfg(target_endian = "little")]
        {
            self.0.to_le_bytes()
        }

        #[cfg(target_endian = "big")]
        {
            self.0.to_be_bytes()
        }
    }

    /// Convert to an Ecc and u64.
    pub fn as_eccu64(self) -> (Ecc, u64) {
        let (ecc, value) = self.as_u64s();
        (Ecc::from(ecc), value)
    }
}

impl Deref for Identifier {
    type Target = u128;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Identifier {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Into<u128> for Identifier {
    fn into(self) -> u128 {
        self.0
    }
}

impl Into<(Ecc, Ecc)> for Identifier {
    fn into(self) -> (Ecc, Ecc) {
        self.as_ecc2()
    }
}

impl Into<Uuid> for Identifier {
    fn into(self) -> Uuid {
        self.as_uuid()
    }
}

impl Into<[u8; 16]> for Identifier {
    fn into(self) -> [u8; 16] {
        self.as_au8()
    }
}

impl Into<(Ecc, u64)> for Identifier {
    fn into(self) -> (Ecc, u64) {
        self.as_eccu64()
    }
}

impl Into<(u64, u64)> for Identifier {
    fn into(self) -> (u64, u64) {
        self.as_u64s()
    }
}

// Several infallible conversions first.

// For IdType::Id
impl From<u128> for Identifier {
    fn from(value: u128) -> Self {
        Self(value)
    }
}

// For IdType::Ecc2
impl From<(Ecc, Ecc)> for Identifier {
    fn from(value: (Ecc, Ecc)) -> Self {
        let primary: u64 = *value.0;
        let secondary: u64 = *value.1;
        let value = (primary as u128) << 64 | (secondary as u128);
        Self(value)
    }
}

// For IdType::Uuid
impl From<Uuid> for Identifier {
    fn from(value: Uuid) -> Self {
        Self(value.as_u128())
    }
}

// For IdType::Scc
impl From<[u8; 16]> for Identifier {
    fn from(value: [u8; 16]) -> Self {
        #[cfg(target_endian = "little")]
        let value = u128::from_le_bytes(value);
        #[cfg(target_endian = "big")]
        let value = u128::from_be_bytes(value);

        Self(value)
    }
}

// For IdType::EccU64
impl From<(Ecc, u64)> for Identifier {
    fn from(value: (Ecc, u64)) -> Self {
        let ecc: u64 = *value.0;
        let value = (ecc as u128) << 64 | (value.1 as u128);
        Self(value)
    }
}

// For IdType::U64s
impl From<(u64, u64)> for Identifier {
    fn from(value: (u64, u64)) -> Self {
        let value = (value.0 as u128) << 64 | (value.1 as u128);
        Self(value)
    }
}

impl TryFrom<(&str, &str)> for Identifier {
    type Error = Error;

    fn try_from(value: (&str, &str)) -> Result<Self> {
        let primary: Ecc = value.0.try_into()?;
        let secondary: Ecc = value.1.try_into()?;
        Ok((primary, secondary).try_into()?)
    }
}

impl TryFrom<(&str, u64)> for Identifier {
    type Error = Error;

    fn try_from(value: (&str, u64)) -> Result<Self> {
        let ecc: Ecc = value.0.try_into()?;
        Ok((ecc, value.1).try_into()?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ecc2() {
        let identifier: Identifier = (123, 456).try_into().unwrap();
        let (_123, _456) = identifier.as_u64s();
        assert_eq!(_123, 123);
        assert_eq!(_456, 456);
    }
}