use crate::core::scope::ScopeIdentifiers;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChangeType {
Created,
Updated,
Deleted,
Consolidated,
}
impl ChangeType {
pub fn as_str(&self) -> &'static str {
match self {
Self::Created => "created",
Self::Updated => "updated",
Self::Deleted => "deleted",
Self::Consolidated => "consolidated",
}
}
}
impl std::fmt::Display for ChangeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeLog {
pub fact_id: String,
pub change_type: ChangeType,
pub timestamp: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub previous_content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub new_content: Option<String>,
pub scope: ScopeIdentifiers,
#[serde(skip_serializing_if = "Option::is_none")]
pub actor: Option<String>,
}
impl ChangeLog {
pub fn new(
fact_id: impl Into<String>,
change_type: ChangeType,
scope: ScopeIdentifiers,
) -> Self {
Self {
fact_id: fact_id.into(),
change_type,
timestamp: Utc::now(),
previous_content: None,
new_content: None,
scope,
actor: None,
}
}
pub fn with_previous_content(mut self, content: impl Into<String>) -> Self {
self.previous_content = Some(content.into());
self
}
pub fn with_new_content(mut self, content: impl Into<String>) -> Self {
self.new_content = Some(content.into());
self
}
pub fn with_actor(mut self, actor: impl Into<String>) -> Self {
self.actor = Some(actor.into());
self
}
}