use super::entity::{EntityId, EntityKeypair};
#[derive(Debug, Clone)]
pub struct OriginStamp {
entity_id: EntityId,
origin_hash: u64,
node_id: u64,
}
impl OriginStamp {
pub fn from_keypair(keypair: &EntityKeypair) -> Self {
Self {
entity_id: keypair.entity_id().clone(),
origin_hash: keypair.origin_hash(),
node_id: keypair.node_id(),
}
}
pub fn from_entity_id(entity_id: EntityId) -> Self {
let origin_hash = entity_id.origin_hash();
let node_id = entity_id.node_id();
Self {
entity_id,
origin_hash,
node_id,
}
}
#[inline]
pub fn origin_hash(&self) -> u64 {
self.origin_hash
}
#[inline]
pub fn node_id(&self) -> u64 {
self.node_id
}
#[inline]
pub fn entity_id(&self) -> &EntityId {
&self.entity_id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_origin_stamp_from_keypair() {
let kp = EntityKeypair::generate();
let stamp = OriginStamp::from_keypair(&kp);
assert_eq!(stamp.origin_hash(), kp.origin_hash());
assert_eq!(stamp.node_id(), kp.node_id());
assert_eq!(stamp.entity_id(), kp.entity_id());
}
#[test]
fn test_origin_stamp_from_entity_id() {
let kp = EntityKeypair::generate();
let stamp = OriginStamp::from_entity_id(kp.entity_id().clone());
assert_eq!(stamp.origin_hash(), kp.origin_hash());
assert_eq!(stamp.node_id(), kp.node_id());
}
}