eternalfest_core 0.18.2

Core crate for Eternalfest
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use arrayvec::ArrayString;
use core::fmt;
#[cfg(feature = "serde")]
use etwin_serde_tools::{Deserialize, Serialize};
#[cfg(feature = "sqlx")]
use sqlx::{database, postgres, Database, Postgres};
use std::str::FromStr;
use thiserror::Error;

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(try_from = "&str", into = "ArrayString<40>")
)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DigestSha1([u8; 20]);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
#[error("invalid sha1 digest size, expected = 20, actual = {0}")]
pub struct DigestSha1FromBytesError(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
pub enum ParseDigestSha1Error {
  #[error("wrong length, expected 40 hex characters as input, actual = {0}")]
  Length(usize),
  #[error("input is not a lowercase hex string: found {character:?} at offset {offset}")]
  InvalidCharacter { character: char, offset: usize },
}

impl DigestSha1 {
  pub fn as_slice(&self) -> &[u8] {
    &self.0
  }

  pub fn from_bytes(bytes: &[u8]) -> Result<Self, DigestSha1FromBytesError> {
    match <[u8; 20]>::try_from(bytes) {
      Ok(a) => Ok(Self(a)),
      Err(_) => Err(DigestSha1FromBytesError(bytes.len())),
    }
  }

  pub fn from_hex(input: &str) -> Result<Self, ParseDigestSha1Error> {
    let mut bytes = [0u8; 20];
    match hex::decode_to_slice(input, &mut bytes) {
      Ok(()) => Ok(Self(bytes)),
      Err(hex::FromHexError::InvalidHexCharacter { c, index }) => Err(ParseDigestSha1Error::InvalidCharacter {
        character: c,
        offset: index,
      }),
      Err(hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength) => {
        Err(ParseDigestSha1Error::Length(input.len()))
      }
    }
  }

  pub fn hex(&self) -> ArrayString<40> {
    let mut out = [0u8; 40];
    hex::encode_to_slice(self.0, &mut out).expect("encoding to hex always succeeds");
    ArrayString::from_byte_string(&out).expect("converting the hex byte string to an array string always succeeds")
  }
}

#[cfg(feature = "sqlx")]
impl sqlx::Type<Postgres> for DigestSha1 {
  fn type_info() -> postgres::PgTypeInfo {
    postgres::PgTypeInfo::with_name("digest_sha1")
  }

  fn compatible(ty: &postgres::PgTypeInfo) -> bool {
    *ty == Self::type_info() || <&[u8] as sqlx::Type<Postgres>>::compatible(ty)
  }
}

#[cfg(feature = "sqlx")]
impl<'r, Db: Database> sqlx::Decode<'r, Db> for DigestSha1
where
  &'r [u8]: sqlx::Decode<'r, Db>,
{
  fn decode(
    value: <Db as database::HasValueRef<'r>>::ValueRef,
  ) -> Result<DigestSha1, Box<dyn std::error::Error + 'static + Send + Sync>> {
    let value: &[u8] = <&[u8] as sqlx::Decode<Db>>::decode(value)?;
    Ok(DigestSha1::from_bytes(value)?)
  }
}

#[cfg(feature = "sqlx")]
impl<'q, Db: Database> sqlx::Encode<'q, Db> for DigestSha1
where
  Vec<u8>: sqlx::Encode<'q, Db>,
{
  fn encode_by_ref(&self, buf: &mut <Db as database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
    self.as_slice().to_vec().encode(buf)
  }
}

impl TryFrom<&[u8]> for DigestSha1 {
  type Error = DigestSha1FromBytesError;

  fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
    Self::from_bytes(value)
  }
}

impl TryFrom<&str> for DigestSha1 {
  type Error = ParseDigestSha1Error;

  fn try_from(value: &str) -> Result<Self, Self::Error> {
    Self::from_hex(value)
  }
}

impl From<DigestSha1> for ArrayString<40> {
  fn from(value: DigestSha1) -> Self {
    value.hex()
  }
}

impl FromStr for DigestSha1 {
  type Err = ParseDigestSha1Error;

  fn from_str(input: &str) -> Result<Self, Self::Err> {
    Self::from_hex(input)
  }
}

impl fmt::Debug for DigestSha1 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_tuple("DigestSha1").field(&self.hex()).finish()
  }
}

impl fmt::Display for DigestSha1 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    fmt::Display::fmt(&self.hex(), f)
  }
}

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(try_from = "&str", into = "ArrayString<64>")
)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DigestSha2([u8; 32]);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
#[error("invalid sha1 digest size, expected = 32, actual = {0}")]
pub struct DigestSha2FromBytesError(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
pub enum ParseDigestSha2Error {
  #[error("wrong length, expected 64 hex characters as input, actual = {0}")]
  Length(usize),
  #[error("input is not a lowercase hex string: found {character:?} at offset {offset}")]
  InvalidCharacter { character: char, offset: usize },
}

impl DigestSha2 {
  pub fn as_slice(&self) -> &[u8] {
    &self.0
  }

  pub fn from_bytes(bytes: &[u8]) -> Result<Self, DigestSha2FromBytesError> {
    match <[u8; 32]>::try_from(bytes) {
      Ok(a) => Ok(Self(a)),
      Err(_) => Err(DigestSha2FromBytesError(bytes.len())),
    }
  }

  pub fn from_hex(input: &str) -> Result<Self, ParseDigestSha2Error> {
    let mut bytes = [0u8; 32];
    match hex::decode_to_slice(input, &mut bytes) {
      Ok(()) => Ok(Self(bytes)),
      Err(hex::FromHexError::InvalidHexCharacter { c, index }) => Err(ParseDigestSha2Error::InvalidCharacter {
        character: c,
        offset: index,
      }),
      Err(hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength) => {
        Err(ParseDigestSha2Error::Length(input.len()))
      }
    }
  }

  pub fn hex(&self) -> ArrayString<64> {
    let mut out = [0u8; 64];
    hex::encode_to_slice(self.0, &mut out).expect("encoding to hex always succeeds");
    ArrayString::from_byte_string(&out).expect("converting the hex byte string to an array string always succeeds")
  }

  pub fn digest(data: &[u8]) -> Self {
    use sha2::{Digest, Sha256};
    Self(Sha256::digest(data).into())
  }
}

#[cfg(feature = "sqlx")]
impl sqlx::Type<Postgres> for DigestSha2 {
  fn type_info() -> postgres::PgTypeInfo {
    postgres::PgTypeInfo::with_name("digest_sha2")
  }

  fn compatible(ty: &postgres::PgTypeInfo) -> bool {
    *ty == Self::type_info() || <&[u8] as sqlx::Type<Postgres>>::compatible(ty)
  }
}

#[cfg(feature = "sqlx")]
impl<'r, Db: Database> sqlx::Decode<'r, Db> for DigestSha2
where
  &'r [u8]: sqlx::Decode<'r, Db>,
{
  fn decode(
    value: <Db as database::HasValueRef<'r>>::ValueRef,
  ) -> Result<DigestSha2, Box<dyn std::error::Error + 'static + Send + Sync>> {
    let value: &[u8] = <&[u8] as sqlx::Decode<Db>>::decode(value)?;
    Ok(DigestSha2::from_bytes(value)?)
  }
}

#[cfg(feature = "sqlx")]
impl<'q, Db: Database> sqlx::Encode<'q, Db> for DigestSha2
where
  Vec<u8>: sqlx::Encode<'q, Db>,
{
  fn encode_by_ref(&self, buf: &mut <Db as database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
    self.as_slice().to_vec().encode(buf)
  }
}

impl TryFrom<&[u8]> for DigestSha2 {
  type Error = DigestSha2FromBytesError;

  fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
    Self::from_bytes(value)
  }
}

impl TryFrom<&str> for DigestSha2 {
  type Error = ParseDigestSha2Error;

  fn try_from(value: &str) -> Result<Self, Self::Error> {
    Self::from_hex(value)
  }
}

impl From<DigestSha2> for ArrayString<64> {
  fn from(value: DigestSha2) -> Self {
    value.hex()
  }
}

impl FromStr for DigestSha2 {
  type Err = ParseDigestSha2Error;

  fn from_str(input: &str) -> Result<Self, Self::Err> {
    Self::from_hex(input)
  }
}

impl fmt::Debug for DigestSha2 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_tuple("DigestSha2").field(&self.hex()).finish()
  }
}

impl fmt::Display for DigestSha2 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    fmt::Display::fmt(&self.hex(), f)
  }
}

#[cfg_attr(
  feature = "serde",
  derive(Serialize, Deserialize),
  serde(try_from = "&str", into = "ArrayString<64>")
)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DigestSha3([u8; 32]);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
#[error("invalid sha1 digest size, expected = 32, actual = {0}")]
pub struct DigestSha3FromBytesError(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
pub enum ParseDigestSha3Error {
  #[error("wrong length, expected 64 hex characters as input, actual = {0}")]
  Length(usize),
  #[error("input is not a lowercase hex string: found {character:?} at offset {offset}")]
  InvalidCharacter { character: char, offset: usize },
}

impl DigestSha3 {
  pub fn as_slice(&self) -> &[u8] {
    &self.0
  }

  pub fn from_bytes(bytes: &[u8]) -> Result<Self, DigestSha3FromBytesError> {
    match <[u8; 32]>::try_from(bytes) {
      Ok(a) => Ok(Self(a)),
      Err(_) => Err(DigestSha3FromBytesError(bytes.len())),
    }
  }

  pub fn from_hex(input: &str) -> Result<Self, ParseDigestSha3Error> {
    let mut bytes = [0u8; 32];
    match hex::decode_to_slice(input, &mut bytes) {
      Ok(()) => Ok(Self(bytes)),
      Err(hex::FromHexError::InvalidHexCharacter { c, index }) => Err(ParseDigestSha3Error::InvalidCharacter {
        character: c,
        offset: index,
      }),
      Err(hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength) => {
        Err(ParseDigestSha3Error::Length(input.len()))
      }
    }
  }

  pub fn hex(&self) -> ArrayString<64> {
    let mut out = [0u8; 64];
    hex::encode_to_slice(self.0, &mut out).expect("encoding to hex always succeeds");
    ArrayString::from_byte_string(&out).expect("converting the hex byte string to an array string always succeeds")
  }

  pub fn digest(data: &[u8]) -> Self {
    use sha3::{Digest, Sha3_256};
    Self(Sha3_256::digest(data).into())
  }
}

#[cfg(feature = "sqlx")]
impl sqlx::Type<Postgres> for DigestSha3 {
  fn type_info() -> postgres::PgTypeInfo {
    postgres::PgTypeInfo::with_name("digest_sha3")
  }

  fn compatible(ty: &postgres::PgTypeInfo) -> bool {
    *ty == Self::type_info() || <&[u8] as sqlx::Type<Postgres>>::compatible(ty)
  }
}

#[cfg(feature = "sqlx")]
impl<'r, Db: Database> sqlx::Decode<'r, Db> for DigestSha3
where
  &'r [u8]: sqlx::Decode<'r, Db>,
{
  fn decode(
    value: <Db as database::HasValueRef<'r>>::ValueRef,
  ) -> Result<DigestSha3, Box<dyn std::error::Error + 'static + Send + Sync>> {
    let value: &[u8] = <&[u8] as sqlx::Decode<Db>>::decode(value)?;
    Ok(DigestSha3::from_bytes(value)?)
  }
}

#[cfg(feature = "sqlx")]
impl<'q, Db: Database> sqlx::Encode<'q, Db> for DigestSha3
where
  Vec<u8>: sqlx::Encode<'q, Db>,
{
  fn encode_by_ref(&self, buf: &mut <Db as database::HasArguments<'q>>::ArgumentBuffer) -> sqlx::encode::IsNull {
    self.as_slice().to_vec().encode(buf)
  }
}

impl TryFrom<&[u8]> for DigestSha3 {
  type Error = DigestSha3FromBytesError;

  fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
    Self::from_bytes(value)
  }
}

impl TryFrom<&str> for DigestSha3 {
  type Error = ParseDigestSha3Error;

  fn try_from(value: &str) -> Result<Self, Self::Error> {
    Self::from_hex(value)
  }
}

impl From<DigestSha3> for ArrayString<64> {
  fn from(value: DigestSha3) -> Self {
    value.hex()
  }
}

impl FromStr for DigestSha3 {
  type Err = ParseDigestSha3Error;

  fn from_str(input: &str) -> Result<Self, Self::Err> {
    Self::from_hex(input)
  }
}

impl fmt::Debug for DigestSha3 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_tuple("DigestSha3").field(&self.hex()).finish()
  }
}

impl fmt::Display for DigestSha3 {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    fmt::Display::fmt(&self.hex(), f)
  }
}

#[cfg(test)]
mod test {
  mod digest_sha1 {
    use crate::digest::{DigestSha1, ParseDigestSha1Error};

    #[test]
    fn from_hex_ok() {
      let actual = DigestSha1::from_hex("27b664227bd281744d1449568fc6edb06361e747");
      #[rustfmt::skip]
        let expected = Ok(DigestSha1([
        0x27, 0xb6, 0x64, 0x22, 0x7b, 0xd2, 0x81, 0x74,
        0x4d, 0x14, 0x49, 0x56, 0x8f, 0xc6, 0xed, 0xb0,
        0x63, 0x61, 0xe7, 0x47,
      ]));
      assert_eq!(actual, expected);
    }

    #[test]
    fn from_hex_empty() {
      let actual = DigestSha1::from_hex("");
      let expected = Err(ParseDigestSha1Error::Length(0));
      assert_eq!(actual, expected);
    }

    #[test]
    fn from_hex_too_long() {
      let actual = DigestSha1::from_hex("27b664227bd281744d1449568fc6edb06361e7470000");
      let expected = Err(ParseDigestSha1Error::Length(44));
      assert_eq!(actual, expected);
    }

    #[test]
    fn from_hex_odd_length() {
      let actual = DigestSha1::from_hex("27b664227bd281744d1449568fc6edb06361e7470");
      let expected = Err(ParseDigestSha1Error::Length(41));
      assert_eq!(actual, expected);
    }

    #[test]
    fn from_hex_invalid_char() {
      let actual = DigestSha1::from_hex("27!664227bd281744d1449568fc6edb06361e747");
      let expected = Err(ParseDigestSha1Error::InvalidCharacter {
        character: '!',
        offset: 2,
      });
      assert_eq!(actual, expected);
    }
  }
}