array_bytes/
lib.rs

1#![deny(clippy::all, missing_docs, unused_crate_dependencies)]
2#![allow(clippy::items_after_test_module, clippy::tabs_in_doc_comments)]
3#![no_std]
4
5//! A collection of Array/Bytes/Hex utilities with full No-STD compatibility.
6//!
7//! Completely optimized for blockchain development.
8//! Especially the Polkadot-SDK.
9
10extern crate alloc;
11
12mod hex;
13pub use hex::*;
14
15mod op;
16pub use op::*;
17
18#[cfg(feature = "serde")] mod serde;
19#[cfg(feature = "serde")] pub use serde::*;
20
21mod prelude {
22	pub use alloc::{string::String, vec::Vec};
23
24	pub use smallvec::SmallVec;
25
26	pub use crate::{Error, Result};
27
28	pub(crate) use crate::op;
29
30	#[cfg(test)]
31	mod test {
32		// Suppress `unused_crate_dependencies` error.
33		use const_hex as _;
34		use criterion as _;
35		use faster_hex as _;
36		use hex_crate as _;
37		use rustc_hex as _;
38		use serde as _;
39		use serde_json as _;
40
41		// self
42		use super::*;
43
44		#[derive(Debug, PartialEq)]
45		pub struct Ljf(pub Vec<u8>);
46		impl From<Vec<u8>> for Ljf {
47			fn from(bytes: Vec<u8>) -> Self {
48				Self(bytes)
49			}
50		}
51
52		#[derive(Debug, PartialEq)]
53		pub struct Ljfn(pub [u8; 17]);
54		impl From<[u8; 17]> for Ljfn {
55			fn from(array: [u8; 17]) -> Self {
56				Self(array)
57			}
58		}
59	}
60	#[cfg(test)] pub use test::*;
61}
62
63#[cfg(feature = "serde")] pub use serde_bytes;
64
65#[allow(missing_docs)]
66pub type Result<T, E = Error> = core::result::Result<T, E>;
67
68#[allow(missing_docs)]
69#[derive(Debug, PartialEq, Eq)]
70pub enum Error {
71	/// The length must not be odd.
72	InvalidLength,
73	/// Found the invalid character at `index`.
74	InvalidCharacter {
75		/// The invalid character.
76		character: char,
77		/// The invalid character's index.
78		index: usize,
79	},
80	/// The data can not fit the array/slice length well.
81	MismatchedLength {
82		/// Expected length.
83		expect: usize,
84	},
85	/// Failed to parse the hex number from hex string.
86	Utf8Error(core::str::Utf8Error),
87	/// Failed to parse the hex number from hex string.
88	ParseIntError(core::num::ParseIntError),
89}