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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use core::hash::Hash;
#[cfg(feature = "wa_proto")]
use core::slice::Iter;
use std::{fmt::Debug, marker::PhantomData, num::NonZeroU32};

use harsh::Harsh;
use rapira::{Rapira, RapiraError};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "ts-types")]
use specta::{DataType, PrimitiveType, Type};
#[cfg(feature = "ts-types")]
use typescript_type_def::{
    type_expr::{Ident, NativeTypeInfo, TypeExpr, TypeInfo},
    TypeDef,
};
#[cfg(feature = "wa_proto")]
use wa_proto::{Incoming, Outcoming};

use super::ArmourError;
use crate::{collections::Result, GetType, Typ};

// default: b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
#[cfg(feature = "std")]
const HARSH_ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz1234567890";
// default: b"cfhistuCFHISTU"
#[cfg(feature = "std")]
const SEPARATORS: &[u8] = b"cfhistu";

#[cfg(feature = "std")]
pub fn create_harsh(salt: &str) -> Harsh {
    Harsh::builder()
        .salt(salt)
        .length(2)
        .alphabet(HARSH_ALPHABET)
        .separators(SEPARATORS)
        .build()
        .unwrap()
}

pub trait HarshIdent {
    #[cfg(feature = "std")]
    fn get_hasher() -> &'static Harsh;

    #[cfg(feature = "std")]
    fn get_name() -> &'static str;
}

#[repr(transparent)]
pub struct Hashid<T>(NonZeroU32, PhantomData<T>);

impl<T> Hashid<T> {
    #[inline]
    pub fn get(self) -> u32 {
        self.0.get()
    }

    #[inline]
    pub fn get_non_zero_u32(self) -> NonZeroU32 {
        self.0
    }

    #[inline]
    pub fn new(id: NonZeroU32) -> Self {
        Hashid(id, PhantomData)
    }

    #[inline]
    pub fn new_unsafe(id: u32) -> Self {
        let id = NonZeroU32::new(id).unwrap();
        Hashid(id, PhantomData)
    }

    #[inline]
    pub fn to_le_bytes(self) -> [u8; 4] {
        self.0.get().to_le_bytes()
    }

    #[inline]
    pub fn to_be_bytes(self) -> [u8; 4] {
        self.0.get().to_be_bytes()
    }

    #[inline]
    pub fn from_le_bytes(b: [u8; 4]) -> Self {
        let id = u32::from_le_bytes(b);
        Self::new_unsafe(id)
    }

    #[inline]
    pub fn from_be_bytes(b: [u8; 4]) -> Self {
        let id = u32::from_be_bytes(b);
        Self::new_unsafe(id)
    }

    pub fn increment(self) -> Self {
        Self::new_unsafe(self.get() + 1)
    }
}

impl<T: HarshIdent> Hashid<T> {
    #[cfg(feature = "std")]
    #[inline]
    pub fn get_name() -> &'static str {
        T::get_name()
    }

    #[inline]
    pub fn deser(s: &str) -> Result<Hashid<T>> {
        let v = T::get_hasher()
            .decode(s)
            .map_err(|_| ArmourError::UnknownError)
            .and_then(|vec| {
                vec.first()
                    .map(|u| *u as u32)
                    .ok_or(ArmourError::UnknownError)
            })?;
        // let v = T::deser(s)?;
        let v = NonZeroU32::new(v).ok_or(ArmourError::NonZeroError)?;
        Ok(Self::new(v))
    }

    #[inline]
    pub fn ser(self) -> String {
        T::get_hasher().encode(&[self.get() as u64])
    }
}

impl<T> Clone for Hashid<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Hashid<T> {}

impl<T> PartialOrd for Hashid<T> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<T> Ord for Hashid<T> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl<T> PartialEq for Hashid<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T> Eq for Hashid<T> {}

impl<T> TryFrom<u32> for Hashid<T> {
    type Error = ArmourError;
    fn try_from(val: u32) -> Result<Hashid<T>, Self::Error> {
        Ok(Hashid::new(val.try_into()?))
    }
}

impl<T> From<Hashid<T>> for u32 {
    fn from(id: Hashid<T>) -> Self {
        id.get()
    }
}

impl<T: HarshIdent> std::fmt::Display for Hashid<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.ser())
    }
}

impl<T> Debug for Hashid<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ID").field(&self.0).finish()
    }
}

impl<T> Hash for Hashid<T> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

#[cfg(feature = "std")]
impl<T: HarshIdent> Serialize for Hashid<T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let s = self.ser();
        serializer.serialize_str(&s)
    }
}

#[cfg(feature = "std")]
impl<'de, T: HarshIdent> Deserialize<'de> for Hashid<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        let s: &str = Deserialize::deserialize(deserializer)?;
        let a = Hashid::<T>::deser(s).map_err(|_| D::Error::custom("id value error"))?;
        Ok(a)
    }
}

#[cfg(feature = "bincode")]
impl<T> Encode for Hashid<T> {
    fn encode<E: bincode::enc::Encoder>(
        &self,
        encoder: &mut E,
    ) -> core::result::Result<(), bincode::error::EncodeError> {
        bincode::Encode::encode(&self.0, encoder)
    }
}

#[cfg(feature = "bincode")]
impl<T> Decode for Hashid<T> {
    fn decode<D: bincode::de::Decoder>(
        decoder: &mut D,
    ) -> Result<Self, bincode::error::DecodeError> {
        let id: NonZeroU32 = bincode::de::Decode::decode(decoder)?;
        Ok(Hashid::new(id))
    }
}

impl<T> Rapira for Hashid<T> {
    const STATIC_SIZE: Option<usize> = Some(4);

    #[inline]
    fn size(&self) -> usize {
        4
    }

    #[inline]
    fn from_slice(slice: &mut &[u8]) -> Result<Self, rapira::RapiraError>
    where
        Self: Sized,
    {
        let u = u32::from_slice(slice)?;
        let id = NonZeroU32::new(u).ok_or(RapiraError::NonZeroError)?;
        Ok(Hashid::<T>::new(id))
    }

    #[inline]
    fn check_bytes(slice: &mut &[u8]) -> Result<(), rapira::RapiraError>
    where
        Self: Sized,
    {
        let bytes: &[u8] = slice.get(..4).ok_or(RapiraError::SliceLenError)?;

        if bytes == [0, 0, 0, 0] {
            return Err(RapiraError::NonZeroError);
        }

        *slice = unsafe { slice.get_unchecked(4..) };
        Ok(())
    }

    #[inline]
    fn from_slice_unchecked(slice: &mut &[u8]) -> Result<Self, rapira::RapiraError>
    where
        Self: Sized,
    {
        let u = u32::from_slice_unchecked(slice)?;
        Ok(Hashid::<T>::new_unsafe(u))
    }

    #[inline]
    unsafe fn from_slice_unsafe(slice: &mut &[u8]) -> Result<Self, rapira::RapiraError>
    where
        Self: Sized,
    {
        let u = u32::from_slice_unsafe(slice)?;
        Ok(Hashid::<T>::new_unsafe(u))
    }

    #[inline]
    fn convert_to_bytes(&self, slice: &mut [u8], cursor: &mut usize) {
        self.get().convert_to_bytes(slice, cursor);
    }
}

#[cfg(feature = "wa_proto")]
impl<T: HarshIdent> Incoming for Hashid<T> {
    #[cfg(not(feature = "std"))]
    fn init(args: &mut IterMut<u32>) -> Result<(u32, Self), ProtocolError>
    where
        Self: Sized,
    {
        let el: u32 = *args.next().ok_or(ProtocolError(ARGS_NEXT_ERROR))?;
        let id = Hashid::<T>::new_unsafe(el);
        Ok((id, el))
    }
    #[cfg(feature = "std")]
    fn args(&self, args: &mut Vec<u32>) -> Result<(), wa_proto::ProtocolError> {
        args.push(self.get());
        Ok(())
    }

    fn fill(
        &self,
        _: &mut std::cell::RefMut<[u8]>,
        args: &mut std::slice::Iter<u32>,
    ) -> Result<(), wa_proto::ProtocolError> {
        args.next()
            .ok_or_else(|| wa_proto::ProtocolError("args is end".to_owned()))?;
        Ok(())
    }
}

#[cfg(feature = "wa_proto")]
impl<T: HarshIdent> Outcoming for Hashid<T> {
    #[cfg(not(feature = "std"))]
    fn args(&self, args: &mut Vec<u32>) -> Result<(), ProtocolError> {
        args.push(self.get());
        Ok(())
    }

    #[cfg(feature = "std")]
    fn read(_: &[u8], args: &mut Iter<u32>) -> Result<Self, wa_proto::ProtocolError> {
        let u = *args
            .next()
            .ok_or_else(|| wa_proto::ProtocolError("args is end".to_owned()))?;
        let id = Hashid::<T>::new_unsafe(u);
        Ok(id)
    }
}

// impl<T: HarshIdent> From<Hashid<T>> for crate::Value {
//     fn from(val: Hashid<T>) -> Self {
//         crate::Value::U32(val.get())
//     }
// }

// impl<T: HarshIdent> TryFromValue for Hashid<T> {
//     fn try_from(value: crate::Value) -> Result<Self, crate::dyn_types::FromValueError>
//     where
//         Self: Sized,
//     {
//         match value {
//             crate::Value::U32(u) => {
//                 let id = NonZeroU32::new(u)
//                     .ok_or(crate::dyn_types::FromValueError::NestedTypeFromError)?;
//                 Ok(Hashid::<T>::new(id))
//             }
//             _ => Err(crate::dyn_types::FromValueError::ValueMatchError),
//         }
//     }
// }

impl<T> GetType for Hashid<T> {
    const TYPE: Typ = Typ::U32;
}

#[cfg(feature = "ts-types")]
impl<T> Type for Hashid<T> {
    const NAME: &'static str = "Hashid";
    fn inline(_: specta::DefOpts, _: &[specta::DataType]) -> specta::DataType {
        DataType::Primitive(PrimitiveType::u32)
    }
}

#[cfg(feature = "ts-types")]
impl<T: 'static> TypeDef for Hashid<T> {
    const INFO: TypeInfo = TypeInfo::Native(NativeTypeInfo {
        r#ref: TypeExpr::ident(Ident("Hashid")),
    });
}