use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use crate::{
assertion::{Assertion, AssertionBase, AssertionCbor},
assertions::labels,
error::Result,
};
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq)]
pub struct TimeStamp(HashMap<String, ByteBuf>);
#[allow(dead_code)]
impl TimeStamp {
pub const LABEL: &'static str = labels::TIMESTAMP;
pub fn new(label: &str, timestamp: &[u8]) -> Self {
let mut ts = TimeStamp(HashMap::new());
ts.0.insert(label.to_string(), ByteBuf::from(timestamp.to_vec()));
ts
}
pub fn add_timestamp(&mut self, manifest_id: &str, timestamp: &[u8]) {
self.0
.insert(manifest_id.to_string(), ByteBuf::from(timestamp.to_vec()));
}
pub fn get_timestamp(&self, manifest_id: &str) -> Option<&[u8]> {
self.0.get(manifest_id).map(|buf| buf.as_ref())
}
}
impl AsRef<HashMap<String, ByteBuf>> for TimeStamp {
fn as_ref(&self) -> &HashMap<String, ByteBuf> {
&self.0
}
}
impl AssertionCbor for TimeStamp {}
impl AssertionBase for TimeStamp {
const LABEL: &'static str = Self::LABEL;
fn to_assertion(&self) -> Result<Assertion> {
Self::to_cbor_assertion(self)
}
fn from_assertion(assertion: &Assertion) -> Result<Self> {
Self::from_cbor_assertion(assertion)
}
}