capsule_core/import/scope.rs
1//! The **scope grammar** — the canonical identity of an import source (SSoT:
2//! [Organization — Scope Grammar]).
3//!
4//! How a local folder, camera roll, or watched directory on each platform maps to a remote
5//! album is a formal contract, not per-client improvisation. A [`Scope`] names the source;
6//! its [`scope_id`](Scope::scope_id) is a domain-separated canonical-CBOR hash, so two
7//! devices of the same platform looking at the same source compute the **same** id with no
8//! coordination protocol.
9//!
10//! The `scope_overrides` / `source_kind_defaults` mapping table these ids key is deferred
11//! post-v1 with the library-settings document; v1 carries the scope on the import so the
12//! destination is explainable, and the override rows plug in later without changing this
13//! grammar (see [`resolve_default_album`](super::default_album::resolve_default_album)).
14//!
15//! [Organization — Scope Grammar]: https://docs/design/organization/#scope-grammar-local-source--album-mapping
16
17use ciborium::value::Value;
18
19use crate::cbor;
20use crate::cohort::PlatformTag;
21use crate::crypto::hash::{Hash32, hash_bytes};
22
23/// Domain-separation label for [`Scope::scope_id`] (versioned; a construction change bumps
24/// the suffix, exactly as for the device-cohort hash).
25pub const IMPORT_SCOPE_V1: &str = "capsule-import-scope/v1";
26
27/// What kind of source an import came from (closed enum; the wire value is the lowercase
28/// snake-case name, and a new kind is an additive protocol change).
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub enum SourceKind {
31 /// The platform's primary photo library (iOS camera roll, Android `DCIM/Camera`).
32 CameraRoll,
33 /// The platform's screenshots collection.
34 Screenshots,
35 /// An app-owned collection (an iOS user collection, an Android app bucket).
36 AppCollection,
37 /// A plain directory the user pointed at once.
38 Folder,
39 /// A directory watched for new files.
40 WatchedDir,
41 /// A mounted card or external disk, identified by volume UUID.
42 RemovableVolume,
43}
44
45impl SourceKind {
46 /// The canonical wire string — the value that enters the [`scope_id`](Scope::scope_id)
47 /// preimage. Stable: changing one changes every derived id.
48 #[must_use]
49 pub fn as_str(self) -> &'static str {
50 match self {
51 SourceKind::CameraRoll => "camera_roll",
52 SourceKind::Screenshots => "screenshots",
53 SourceKind::AppCollection => "app_collection",
54 SourceKind::Folder => "folder",
55 SourceKind::WatchedDir => "watched_dir",
56 SourceKind::RemovableVolume => "removable_volume",
57 }
58 }
59}
60
61/// The canonical identity of an import source.
62///
63/// `locator` is the platform's canonical, reinstall-stable locator per the owner doc's
64/// table — the iOS smart-album subtype name or collection `localIdentifier`, the Android
65/// MediaStore **relative path** (never `BUCKET_ID`), the desktop canonicalized path, or a
66/// removable volume's UUID plus relative path. Producing it is the platform adapter's job;
67/// this type only fixes how the three parts become an id.
68#[derive(Debug, Clone, PartialEq, Eq, Hash)]
69pub struct Scope {
70 /// The platform the source lives on (shared closed enum with device cohorts).
71 pub platform: PlatformTag,
72 /// What kind of source it is.
73 pub source_kind: SourceKind,
74 /// The platform's canonical locator for this source.
75 pub locator: String,
76}
77
78impl Scope {
79 /// Build a scope from its three parts.
80 pub fn new(platform: PlatformTag, source_kind: SourceKind, locator: impl Into<String>) -> Self {
81 Self {
82 platform,
83 source_kind,
84 locator: locator.into(),
85 }
86 }
87
88 /// The scope id:
89 /// `SHA-256( canonical-CBOR([ "capsule-import-scope/v1", platform, source_kind, locator ]) )`.
90 ///
91 /// Domain-separated canonical CBOR — never naive concatenation — so a locator that
92 /// happens to contain a separator can never collide with a different scope, and the id
93 /// is byte-reproducible on any device and any platform implementation.
94 #[must_use]
95 pub fn scope_id(&self) -> Hash32 {
96 let array = Value::Array(vec![
97 Value::Text(IMPORT_SCOPE_V1.to_string()),
98 Value::Text(self.platform.as_str().to_string()),
99 Value::Text(self.source_kind.as_str().to_string()),
100 Value::Text(self.locator.clone()),
101 ]);
102 hash_bytes(&cbor::value_to_canonical_vec(&array))
103 }
104}
105
106#[cfg(test)]
107mod tests;