use crate::kernel::TenantId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataDestination {
Storage,
Stream,
Log,
AuditExport,
}
impl DataDestination {
pub fn is_frontend_visible(&self) -> bool {
matches!(self, DataDestination::Stream)
}
pub fn requires_encryption(&self) -> bool {
matches!(self, DataDestination::Storage)
}
}
#[derive(Debug, Clone)]
pub struct ProtectionContext {
pub destination: DataDestination,
pub tenant_id: Option<TenantId>,
pub is_internal_audit: bool,
}
impl ProtectionContext {
pub fn new(destination: DataDestination) -> Self {
Self {
destination,
tenant_id: None,
is_internal_audit: false,
}
}
pub fn for_stream() -> Self {
Self::new(DataDestination::Stream)
}
pub fn for_storage() -> Self {
Self::new(DataDestination::Storage)
}
pub fn for_log() -> Self {
Self::new(DataDestination::Log)
}
pub fn for_audit(is_internal: bool) -> Self {
Self {
destination: DataDestination::AuditExport,
tenant_id: None,
is_internal_audit: is_internal,
}
}
pub fn with_tenant(mut self, tenant_id: TenantId) -> Self {
self.tenant_id = Some(tenant_id);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_data_destination_frontend_visible() {
assert!(DataDestination::Stream.is_frontend_visible());
assert!(!DataDestination::Storage.is_frontend_visible());
assert!(!DataDestination::Log.is_frontend_visible());
assert!(!DataDestination::AuditExport.is_frontend_visible());
}
#[test]
fn test_data_destination_requires_encryption() {
assert!(DataDestination::Storage.requires_encryption());
assert!(!DataDestination::Stream.requires_encryption());
assert!(!DataDestination::Log.requires_encryption());
assert!(!DataDestination::AuditExport.requires_encryption());
}
#[test]
fn test_protection_context_factories() {
let stream_ctx = ProtectionContext::for_stream();
assert_eq!(stream_ctx.destination, DataDestination::Stream);
let storage_ctx = ProtectionContext::for_storage();
assert_eq!(storage_ctx.destination, DataDestination::Storage);
let log_ctx = ProtectionContext::for_log();
assert_eq!(log_ctx.destination, DataDestination::Log);
let audit_ctx = ProtectionContext::for_audit(true);
assert_eq!(audit_ctx.destination, DataDestination::AuditExport);
assert!(audit_ctx.is_internal_audit);
}
#[test]
fn test_protection_context_with_tenant() {
let tenant_id = TenantId::from_string("tenant_123");
let ctx = ProtectionContext::for_stream().with_tenant(tenant_id);
assert!(ctx.tenant_id.is_some());
assert_eq!(ctx.tenant_id.unwrap().as_str(), "tenant_123");
}
}