Extract, (de-)serialize and format id
This commit is contained in:
parent
70bb6f31cf
commit
cfaafba77f
4 changed files with 80 additions and 5 deletions
|
|
@ -4,4 +4,6 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
hex = { version = "0.4.3", features = ["serde"] }
|
||||
serde = { version = "1.0.136", features = ["derive"] }
|
||||
sha2 = "0.10.1"
|
||||
|
|
|
|||
24
cove-core/src/id.rs
Normal file
24
cove-core/src/id.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::fmt;
|
||||
|
||||
use hex::ToHex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
// TODO Use base64 representation instead
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Id(#[serde(with = "hex")] [u8; 32]);
|
||||
|
||||
impl Id {
|
||||
pub fn of(str: &str) -> Self {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(str);
|
||||
Self(hasher.finalize().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Id {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0.encode_hex::<String>())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
mod id;
|
||||
mod macros;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use self::id::*;
|
||||
use self::macros::packets;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Id(pub u128);
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct HelloCmd {
|
||||
pub id: Id,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue