imbibe_domain/
lib.rs

1pub mod block;
2pub mod tx;
3
4use core::fmt::{Debug, Formatter, Result};
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use cosmrs::tendermint::account::Id;
10
11#[derive(Clone)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13pub struct Sha256([u8; Self::LEN]);
14
15#[derive(Debug, Clone)]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17pub struct Address(Id);
18
19#[derive(Debug, Clone)]
20#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21pub struct NonEmptyBz<T>(T);
22
23impl Sha256 {
24	pub const LEN: usize = 32;
25
26	pub const fn new(hash: [u8; Self::LEN]) -> Self {
27		Self(hash)
28	}
29
30	pub const fn get(&self) -> &[u8; Self::LEN] {
31		&self.0
32	}
33}
34
35impl<T> From<T> for Sha256
36where
37	[u8; Self::LEN]: From<T>,
38{
39	fn from(hash: T) -> Self {
40		Self::new(hash.into())
41	}
42}
43
44impl Debug for Sha256 {
45	fn fmt(&self, f: &mut Formatter<'_>) -> Result {
46		write!(
47			f,
48			"Sha256({})",
49			const_hex::const_encode::<32, false>(self.get()).as_str()
50		)
51	}
52}
53
54impl Address {
55	const ACCOUNT_LEN: usize = 20;
56
57	pub fn new(bz: [u8; Self::ACCOUNT_LEN]) -> Self {
58		Self(Id::new(bz))
59	}
60
61	pub fn as_bytes(&self) -> &[u8] {
62		self.0.as_bytes()
63	}
64}
65
66impl<T> NonEmptyBz<T>
67where
68	T: AsRef<[u8]>,
69{
70	pub fn new(bz: T) -> Option<Self> {
71		(!bz.as_ref().is_empty()).then_some(bz).map(Self)
72	}
73}
74
75impl<T> NonEmptyBz<T> {
76	pub fn get(&self) -> &T {
77		&self.0
78	}
79
80	pub fn into_inner(self) -> T {
81		self.0
82	}
83}
84
85impl<T> AsRef<[u8]> for NonEmptyBz<T>
86where
87	T: AsRef<[u8]>,
88{
89	fn as_ref(&self) -> &[u8] {
90		self.0.as_ref()
91	}
92}