use std::{cmp::max, sync::Arc, time::SystemTime};
use instant::Instant;
#[derive(Clone, PartialEq, Eq)]
pub struct ObjectState<T> {
pub data: T,
pub format: DataFormat,
pub version: DataVersion,
pub synchronized: DataSynchronized,
}
pub type DataVersion = Vec<i64>;
pub type DataFormat = Vec<String>;
pub type DataBytes = Arc<Vec<u8>>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DataSynchronized {
Now,
LastAt(Instant),
}
impl DataSynchronized {
pub fn micros_ago(&self) -> u64 {
match self {
DataSynchronized::LastAt(timestamp) => max(1, (Instant::now().duration_since(*timestamp)).as_micros() as u64),
DataSynchronized::Now => 0,
}
}
}
impl<T: 'static> ObjectState<T> {
pub fn new(data: T) -> ObjectState<T> {
let timestamp = SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
ObjectState {
data,
format: vec![],
version: vec![0, (timestamp / 1000000000) as i64, (timestamp % 1000000000) as i64],
synchronized: DataSynchronized::Now,
}
}
pub fn with_data<U>(self, data: U) -> ObjectState<U> {
ObjectState { data, version: self.version, format: self.format, synchronized: self.synchronized }
}
pub fn with_version(mut self, version: impl Into<DataVersion>) -> ObjectState<T> {
self.version = version.into();
self
}
pub fn with_format(mut self, format: impl Into<DataFormat>) -> ObjectState<T> {
self.format = format.into();
self
}
pub fn with_synchronized(mut self, synchronized: impl Into<DataSynchronized>) -> ObjectState<T> {
self.synchronized = synchronized.into();
self
}
}