use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IdError {
kind: &'static str,
}
impl IdError {
const fn empty(kind: &'static str) -> Self {
Self { kind }
}
}
impl fmt::Display for IdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} cannot be empty", self.kind)
}
}
impl std::error::Error for IdError {}
macro_rules! id_type {
($name:ident) => {
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct $name(String);
impl $name {
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
let value = value.into();
if value.trim().is_empty() {
return Err(IdError::empty(stringify!($name)));
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
};
}
id_type!(ActorId);
id_type!(FactEventId);
id_type!(FactId);
id_type!(EvidenceId);
id_type!(SourceId);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct TimestampMillis(i64);
impl TimestampMillis {
#[must_use]
pub const fn from_unix_millis(value: i64) -> Self {
Self(value)
}
#[must_use]
pub const fn as_unix_millis(self) -> i64 {
self.0
}
}