use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(tag = "type", content = "id", rename_all = "camelCase")]
#[non_exhaustive]
pub enum AttachmentHolder {
Device(String),
User(String),
}
impl AttachmentHolder {
pub fn key(&self) -> String {
match self {
Self::Device(id) => format!("device:{id}"),
Self::User(id) => format!("user:{id}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DeviceAttachment {
pub holder: AttachmentHolder,
pub kind: String,
pub key: String,
pub record: Value,
pub updated_at: DateTime<Utc>,
}
impl DeviceAttachment {
pub fn new(
holder: AttachmentHolder,
kind: impl Into<String>,
key: impl Into<String>,
record: Value,
updated_at: DateTime<Utc>,
) -> Self {
Self { holder, kind: kind.into(), key: key.into(), record, updated_at }
}
}