changeset 11: |
d8f806f1d327 |
author: |
ellis <ellis@rwest.io> |
date: |
Sun, 14 May 2023 21:27:04 -0400 |
permissions: |
-rw-r--r-- |
description: |
obj updates |
1 use std::{fmt, str::FromStr}; 2 use serde::{Serialize, Deserialize}; 6 use crate::hash::{KEY_LEN,OUT_LEN,B3Hasher}; 7 /// a simple Id abstraction 8 #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Deserialize, Hash)] 9 pub struct Id(pub Vec<u8>); 12 pub fn rand() -> Self { 13 let mut rng = rand::thread_rng(); 14 let vals: Vec<u8> = (0..KEY_LEN).map(|_| rng.gen_range(0..u8::MAX)).collect(); 18 pub fn state_hash(&self, state: &mut B3Hasher) -> Self { 19 let mut output = vec![0; OUT_LEN]; 20 state.update(&self.0); 21 let mut res = state.finalize_xof(); 22 res.fill(&mut output); 26 pub fn to_hex(&self) -> String { 33 /// identifies a unique Peer 34 #[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)] 40 pub fn new() -> Self { 44 pub fn rand() -> Self { 45 let pd = rand::thread_rng().gen::<[u8; 32]>(); 49 pub fn from_bytes(data: &[u8]) -> Self { 50 let pd = blake3::hash(data); 51 let hash = pd.as_bytes(); 56 impl Default for PeerId { 57 fn default() -> Self { 58 PeerId { id: [0; 32] } 64 /// Defines Identity-related behaviors 65 pub trait Identity: Sized { 66 /// return the hashed bytes of an ObjectId 70 #[derive(Debug, Copy, Clone, Eq, PartialEq)] 71 pub struct ObjectId(u128); 73 pub struct NameSpace { 74 pub prefix: Option<String>, 85 impl From<Uuid> for ObjectId { 86 fn from(uuid: Uuid) -> Self { 87 ObjectId(uuid.as_u128()) 91 impl From<Ulid> for ObjectId { 92 fn from(ulid: Ulid) -> Self { 93 ObjectId(u128::from(ulid)) 97 impl From<u128> for ObjectId { 98 fn from(src: u128) -> Self { 103 impl FromStr for ObjectId { 105 fn from_str(input: &str) -> std::result::Result<ObjectId, Self::Err> { 107 i => Ok(ObjectId(u128::from(Ulid::from_str(i).unwrap()))), 112 impl fmt::Display for ObjectId { 113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 116 write!(f, "{}", Ulid::from(i))