Extract, (de-)serialize and format id
This commit is contained in:
parent
70bb6f31cf
commit
cfaafba77f
4 changed files with 80 additions and 5 deletions
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>())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue