hexstring 0.1.5

An utility library for handling hexadecimal string
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
//! # hexstring
//!
//! The `hexstring` crate provide a convenient hexadecimal string wrapper.
//! It allows all the common conversion expected from a hexadecimal string :
//! - Contains a structured representation of uppercase or lowercase hexadecimal string
//! - Construct from both string and string literal
//! - Convert from and into array of bytes
//!
//! The [`HexString`] type is a tiny immutable wrapper around string and insure it
//! always contains a valid hexadecimal string.
//!
//! ## Feature flags
//!
//! The following are a list of [Cargo features][cargo-features] that can be enabled or disabled:
//! - **serde**: Enable [serde][serde] support.
//!
//! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/features.html#the-features-section
//! [serde]: https://serde.rs

#![deny(missing_docs)]

use std::borrow::Cow;
use std::convert::{From, TryFrom};
use std::marker::PhantomData;
use std::str::{self, FromStr};

use derive_more::Display;
use hex::FromHexError;

/// Errors than can occurs during [`HexString`] construction.
///
/// Refers to [`FromHexError`] for more details.
pub type Error = FromHexError;

/// Convenient alias type to represent uppercase hexadecimal string.
pub type UpperHexString = HexString<Uppercase>;

/// Convenient alias type to represent lowercase hexadecimal string.
pub type LowerHexString = HexString<Lowercase>;

mod private {
  /// A sealed trait to prevent external implementations of [`Case`].
  pub trait Sealed {}
}

/// Provides encoding and validation for hexadecimal strings according to the case.
///
/// This trait is sealed to prevent external implementations, ensuring that only `Lowercase` and
/// `Uppercase` can be used as cases.
pub trait Case: private::Sealed {
  /// Encodes the given bytes into a hexadecimal string according to the case.
  fn encode(bytes: &[u8]) -> String;
  /// Checks if the given character is a valid hexadecimal character according to the case.
  fn is_valid(c: char) -> bool;
}

/// Lowercase hexadecimal string representation.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Lowercase;

impl Case for Lowercase {
  fn encode(bytes: &[u8]) -> String {
    hex::encode(bytes)
  }

  fn is_valid(c: char) -> bool {
    matches!(c, '0'..='9' | 'a'..='f')
  }
}

impl private::Sealed for Lowercase {}

/// Uppercase hexadecimal string representation.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Uppercase;

impl Case for Uppercase {
  fn encode(bytes: &[u8]) -> String {
    hex::encode_upper(bytes)
  }

  fn is_valid(c: char) -> bool {
    matches!(c, '0'..='9' | 'A'..='F')
  }
}

impl private::Sealed for Uppercase {}

/// Provides a structured representation of a hexadecimal string.
///
/// It is guaranteed to be a valid hexadecimal string, whether initialized from a string
/// or from bytes.
/// A valid [`HexString`] should contain only alphanumerical characters such as :
/// - ff04ad992c
/// - FF04AD99C
///
/// And must not mix upper and lower alphabetic characters.
///
/// # Examples
///
/// The idiomatic way to construct a [`HexString`] is to call [`HexString::new`] method with a
/// string.
///
/// ```
/// use hexstring::{HexString, Uppercase};
///
/// let hex = HexString::<Uppercase>::new("ABCDEF").unwrap();
/// ```
///
/// As the example shown, creating a hexadecimal string is a bit convoluted due to the usage of
/// generic type.
/// Two convenient type aliases must be used instead of the raw [`HexString`] type :
///
/// ```
/// use hexstring::{UpperHexString, LowerHexString};
///
/// let lowercase_hex = LowerHexString::new("abcdef").unwrap();
/// let uppercase_hex = UpperHexString::new("ABCDEF").unwrap();
/// ```
///
/// [`HexString`] has support for conversion from and into array of bytes.
///
/// ```
/// use hexstring::LowerHexString;
///
/// let expected_bytes = [41, 24, 42];
/// let hex = LowerHexString::from(expected_bytes);
/// let bytes = Vec::from(hex);
///
/// assert_eq!(expected_bytes, &bytes[..]);
/// ```
#[cfg_attr(
  feature = "serde",
  derive(serde::Deserialize, serde::Serialize),
  serde(try_from = "String")
)]
#[derive(Clone, Debug, Default, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[display("{}", _0)]
#[repr(transparent)]
pub struct HexString<C: Case>(Cow<'static, str>, PhantomData<C>);

impl<C: Case> HexString<C> {
  /// Constructs a new [`HexString`] from a string.
  ///
  /// # Errors
  /// This method fails if the given string is not a valid hexadecimal, i.e. if it has an odd length
  /// or contains invalid characters.
  pub fn new(s: impl Into<Cow<'static, str>>) -> Result<Self, Error> {
    let s = s.into();

    if s.len() & 1 != 0 {
      return Err(Error::OddLength);
    }

    if let Some((index, c)) = s.chars().enumerate().find(|(_, c)| !C::is_valid(*c)) {
      return Err(Error::InvalidHexCharacter { c, index });
    }

    Ok(Self(s, PhantomData))
  }

  /// Creates a new [`HexString`] without checking the string.
  ///
  /// # Safety
  /// The string should be a valid hexadecimal string.
  pub unsafe fn new_unchecked(s: impl Into<Cow<'static, str>>) -> Self {
    Self(s.into(), PhantomData)
  }
}

impl HexString<Lowercase> {
  /// Constructs an [`HexString<Uppercase>`] from a [`HexString<Lowercase>`].
  ///
  /// This method performs a copy if the internal string is a string literal.
  pub fn to_uppercase(self) -> HexString<Uppercase> {
    let mut s = self.0.into_owned();

    s.make_ascii_uppercase();

    unsafe { HexString::new_unchecked(s) }
  }
}

impl HexString<Uppercase> {
  /// Constructs a [`HexString<Lowercase>`] from an [`HexString<Uppercase>`].
  ///
  /// This method performs a copy if the internal string is a string literal.
  pub fn to_lowercase(self) -> HexString<Lowercase> {
    let mut s = self.0.into_owned();

    s.make_ascii_lowercase();

    unsafe { HexString::new_unchecked(s) }
  }
}

impl<C: Case> From<&[u8]> for HexString<C> {
  fn from(bytes: &[u8]) -> Self {
    let s = C::encode(bytes);

    unsafe { Self::new_unchecked(s) }
  }
}

impl<C: Case> From<Vec<u8>> for HexString<C> {
  fn from(bytes: Vec<u8>) -> Self {
    Self::from(&bytes[..])
  }
}

impl<C: Case, const N: usize> From<[u8; N]> for HexString<C> {
  fn from(bytes: [u8; N]) -> Self {
    Self::from(&bytes[..])
  }
}

impl<C: Case> From<HexString<C>> for Vec<u8> {
  fn from(s: HexString<C>) -> Self {
    // since `HexString` always represents a valid hexadecimal string, the result of `hex::decode`
    // can be safely unwrapped.
    //
    // Note that this call may panic if the `HexString` has been constructed from `new_unchecked`
    // method.
    hex::decode(s.0.as_ref()).unwrap()
  }
}

impl<C: Case> FromStr for HexString<C> {
  type Err = Error;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    Self::new(s.to_owned())
  }
}

impl<C: Case, const N: usize> TryFrom<HexString<C>> for [u8; N] {
  type Error = Error;

  fn try_from(s: HexString<C>) -> Result<Self, Self::Error> {
    let mut bytes = [0u8; N];

    hex::decode_to_slice(s.0.as_ref(), &mut bytes).map(|_| bytes)
  }
}

// Hide `std::convert::TryFrom` conversion implementation from string used only by
// `serde::Deserialize` mechanism.
//
// It constraints user to use [`HexString::new`] to construct a hexadecimal string.
#[cfg(feature = "serde")]
mod seal {
  use super::*;
  use std::convert::TryFrom;

  #[doc(hidden)]
  impl<C: Case> TryFrom<String> for HexString<C> {
    type Error = Error;

    fn try_from(s: String) -> Result<Self, Self::Error> {
      Self::new(s)
    }
  }
}

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

  #[test]
  fn it_constructs_from_owned_str() {
    assert_eq!(
      LowerHexString::new("ab04ff".to_string()),
      Ok(HexString(Cow::Owned("ab04ff".to_string()), PhantomData))
    );
    assert_eq!(
      UpperHexString::new("AB04FF".to_string()),
      Ok(HexString(Cow::Owned("AB04FF".to_string()), PhantomData))
    );
  }

  #[test]
  fn it_constructs_from_borrowed_str() {
    assert_eq!(
      LowerHexString::new("ab04ff"),
      Ok(HexString(Cow::Borrowed("ab04ff"), PhantomData))
    );
    assert_eq!(
      UpperHexString::new("AB04FF"),
      Ok(HexString(Cow::Borrowed("AB04FF"), PhantomData))
    );
  }

  #[test]
  fn it_constructs_from_empty_str() {
    assert!(LowerHexString::new("").is_ok());
    assert!(UpperHexString::new("").is_ok());
  }

  #[test]
  fn it_constructs_from_bytes() {
    assert_eq!(
      LowerHexString::from([42, 15, 5]),
      HexString::<Lowercase>(Cow::Borrowed("2a0f05"), PhantomData)
    );
    assert_eq!(
      UpperHexString::from([42, 15, 5]),
      HexString::<Uppercase>(Cow::Borrowed("2A0F05"), PhantomData)
    );
    assert_eq!(
      LowerHexString::from(vec![1, 2, 3, 4, 5]),
      HexString::<Lowercase>(Cow::Borrowed("0102030405"), PhantomData)
    );
    assert_eq!(
      UpperHexString::from(vec![1, 2, 3, 4, 5]),
      HexString::<Uppercase>(Cow::Borrowed("0102030405"), PhantomData)
    );
  }

  #[test]
  fn it_rejects_str_with_odd_length() {
    assert_eq!(LowerHexString::new("abc"), Err(Error::OddLength));
    assert_eq!(UpperHexString::new("abcde"), Err(Error::OddLength));
  }

  #[test]
  fn it_rejects_str_with_invalid_chars() {
    assert_eq!(
      LowerHexString::new("abcdZ109"),
      Err(Error::InvalidHexCharacter { c: 'Z', index: 4 })
    );
    assert_eq!(
      UpperHexString::new("ABVCD109"),
      Err(Error::InvalidHexCharacter { c: 'V', index: 2 })
    );
  }

  #[test]
  fn it_constructs_from_unchecked_str() {
    let hex = unsafe { LowerHexString::new_unchecked("0a0b0c0d0e") };
    let bytes = Vec::from(hex);

    assert_eq!(&bytes[..], [10, 11, 12, 13, 14]);
  }

  #[test]
  #[should_panic]
  fn it_fails_to_convert_into_bytes_from_invalid_unchecked_str() {
    let hex = unsafe { LowerHexString::new_unchecked("thisisnotvalid") };
    let _ = Vec::from(hex);
  }

  #[test]
  fn it_converts_into_bytes() {
    let hex = LowerHexString::new("2a1a02").unwrap();
    let bytes = Vec::from(hex);

    assert_eq!(&bytes[..], [42, 26, 2]);

    let hex = UpperHexString::new("2A1A02").unwrap();
    let bytes = Vec::from(hex);

    assert_eq!(&bytes[..], [42, 26, 2]);
  }

  #[test]
  fn it_converts_into_fixed_array_of_bytes() {
    use std::convert::TryInto;

    let bytes: [u8; 4] = LowerHexString::new("142a020a").unwrap().try_into().unwrap();

    assert_eq!(bytes, [20, 42, 2, 10]);

    let bytes: [u8; 5] = UpperHexString::new("142A020A0F")
      .unwrap()
      .try_into()
      .unwrap();

    assert_eq!(bytes, [20, 42, 2, 10, 15]);
  }

  #[test]
  fn it_converts_from_str() {
    let hex = "aabbccddee".parse::<LowerHexString>().unwrap();
    let expected_hex = HexString::<Lowercase>(Cow::Owned("aabbccddee".to_string()), PhantomData);

    assert_eq!(hex, expected_hex);
  }

  #[test]
  fn it_creates_upper_hex_str_from_lower_hex_str() {
    let s = "aabbccddee";
    let hex = LowerHexString::new(s).unwrap().to_uppercase();
    let expected_hex = HexString::<Uppercase>(Cow::Owned("AABBCCDDEE".to_string()), PhantomData);

    assert_ne!(s, hex.0.as_ref());
    assert_eq!(hex, expected_hex);

    let hex = LowerHexString::new(s.to_string()).unwrap().to_uppercase();

    assert_eq!(hex, expected_hex);
  }

  #[test]
  fn it_creates_lower_hex_str_from_upper_str() {
    let s = "AABBCCDDEE";
    let hex = UpperHexString::new(s).unwrap().to_lowercase();
    let expected_hex = HexString::<Lowercase>(Cow::Owned("aabbccddee".to_string()), PhantomData);

    assert_ne!(s, hex.0.as_ref());
    assert_eq!(hex, expected_hex);

    let hex = UpperHexString::new(s.to_string()).unwrap().to_lowercase();

    assert_eq!(hex, expected_hex);
  }

  #[cfg(feature = "serde")]
  mod serde {
    use super::*;
    use serde_json::error::Category;

    #[test]
    fn it_deser_hex_str() {
      let result: Result<LowerHexString, _> = serde_json::from_str("\"abcd09\"");

      assert!(result.is_ok());

      let result: Result<UpperHexString, _> = serde_json::from_str("\"ABCD09\"");

      assert!(result.is_ok());
    }

    #[test]
    fn it_fails_to_deser_invalid_hex_str() {
      let result: Result<LowerHexString, serde_json::Error> =
        serde_json::from_str("\"invalid hex str\"");

      assert_eq!(result.unwrap_err().classify(), Category::Data);

      let result: Result<UpperHexString, serde_json::Error> =
        serde_json::from_str("\"INVALID HEX STR\"");

      assert_eq!(result.unwrap_err().classify(), Category::Data);
    }
  }
}