refactor: breaking refactor of domo_proto and domo_node

This commit is contained in:
Strix 2023-10-15 18:06:33 +02:00
parent be4425aff9
commit 15971aa4fa
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
31 changed files with 902 additions and 393 deletions

View file

@ -0,0 +1,75 @@
use std::fmt::{Display, Formatter, LowerHex};
use rand::random;
/// Helper struct for the identifiers used in `dest`, `src`, `packet_id` and `reply_to`.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub struct Identifier {
pub bytes: [u8; 4],
}
impl Identifier {
pub fn random() -> Identifier {
Identifier::from(random::<u32>())
}
}
impl Default for Identifier {
fn default() -> Self {
Identifier::from(0x00000000)
}
}
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"0x{:x}{:x}{:x}{:x}",
self.bytes[0], self.bytes[1], self.bytes[2], self.bytes[3]
)
}
}
impl LowerHex for Identifier {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{:x}{:x}{:x}{:x}",
self.bytes[0], self.bytes[1], self.bytes[2], self.bytes[3]
)
}
}
impl From<&[u8]> for Identifier {
fn from(value: &[u8]) -> Self {
let mut bytes = [0_u8; 4];
bytes[..4].copy_from_slice(value[..4].as_ref());
Identifier {
bytes
}
}
}
impl From<Vec<u8>> for Identifier {
fn from(value: Vec<u8>) -> Self {
Identifier::from(value.as_slice())
}
}
impl From<u32> for Identifier {
fn from(value: u32) -> Self {
Identifier {
bytes: [
(value >> 24) as u8,
(value >> 16) as u8,
(value >> 8) as u8,
(value >> 0) as u8,
],
}
}
}
impl Into<u32> for Identifier {
fn into(self) -> u32 {
u32::from_be_bytes(self.bytes)
}
}