Skip to main content

adk_kernel/pairing/
attachments.rs

1//! What a surface keeps beside a paired device or a person (spec 3.3 §5.4, §8.2): a phone's
2//! push subscription, a passkey, the claim that sends one wake-up once across replicas.
3
4use chrono::{DateTime, Utc};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8/// Who an attachment belongs to.
9///
10/// # Example
11///
12/// ```rust
13/// use adk_kernel::pairing::AttachmentHolder;
14///
15/// let holder = AttachmentHolder::Device("0a1b2c3d4e5f6071".into());
16/// assert_eq!(holder.key(), "device:0a1b2c3d4e5f6071");
17/// ```
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
19#[serde(tag = "type", content = "id", rename_all = "camelCase")]
20#[non_exhaustive]
21pub enum AttachmentHolder {
22    /// A paired device; revoking it removes its attachments.
23    Device(String),
24    /// A person, such as the operator's passkey for the web home.
25    User(String),
26}
27
28impl AttachmentHolder {
29    /// The holder as one string, `device:<id>` or `user:<id>`, which stores key rows by.
30    pub fn key(&self) -> String {
31        match self {
32            Self::Device(id) => format!("device:{id}"),
33            Self::User(id) => format!("user:{id}"),
34        }
35    }
36}
37
38/// One thing kept beside a device or a person (spec 3.3 §5.4, §8.2): a record of `kind`
39/// under `key`, which the surface that wrote it reads back. The store never reads `record`.
40///
41/// # Example
42///
43/// ```rust
44/// use adk_kernel::pairing::{AttachmentHolder, DeviceAttachment};
45///
46/// let subscription = DeviceAttachment::new(
47///     AttachmentHolder::Device("0a1b2c3d4e5f6071".into()),
48///     "push",
49///     "subscription",
50///     serde_json::json!({ "endpoint": "https://web.push.apple.com/…" }),
51///     chrono::Utc::now(),
52/// );
53/// assert_eq!(subscription.kind, "push");
54/// ```
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56#[serde(rename_all = "camelCase")]
57#[non_exhaustive]
58pub struct DeviceAttachment {
59    /// Whose it is.
60    pub holder: AttachmentHolder,
61    /// What it is, such as `push` or `passkey`.
62    pub kind: String,
63    /// Which one of its kind.
64    pub key: String,
65    /// What the surface keeps.
66    pub record: Value,
67    /// When it was last written.
68    pub updated_at: DateTime<Utc>,
69}
70
71impl DeviceAttachment {
72    /// An attachment of `holder`.
73    pub fn new(
74        holder: AttachmentHolder,
75        kind: impl Into<String>,
76        key: impl Into<String>,
77        record: Value,
78        updated_at: DateTime<Utc>,
79    ) -> Self {
80        Self { holder, kind: kind.into(), key: key.into(), record, updated_at }
81    }
82}