Use String for PayloadId

This commit is contained in:
Youngjoon Lee 2024-11-08 11:44:22 +07:00
parent b3019b0cae
commit 7ff5b0300d
No known key found for this signature in database
GPG Key ID: 25CA11F37F095E5D

View File

@ -1,20 +1,20 @@
use uuid::Uuid;
pub type PayloadId = [u8; 16];
pub type PayloadId = String;
pub struct Payload(PayloadId);
pub struct Payload(Uuid);
impl Payload {
pub fn new() -> Self {
Self(Uuid::new_v4().into_bytes())
Self(Uuid::new_v4())
}
pub fn id(&self) -> PayloadId {
self.0
self.0.to_string()
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
self.0.as_bytes()
}
pub fn load(data: Vec<u8>) -> Self {
@ -22,3 +22,18 @@ impl Payload {
Self(data.try_into().unwrap())
}
}
#[cfg(test)]
mod tests {
use super::Payload;
#[test]
fn payload() {
let payload = Payload::new();
println!("{}", payload.id());
let bytes = payload.as_bytes();
assert_eq!(bytes.len(), 16);
let loaded_payload = Payload::load(bytes.to_vec());
assert_eq!(bytes, loaded_payload.as_bytes());
}
}