Skip to main content

capsule_core/import/
upload.rs

1//! Staged-upload contract types — the **one canonical** `UploadPolicy`/`UploadTier`
2//! set (SSoT:
3//! [Transfer — Upload: Upload Tiering (Staged Uploads)](https://docs/design/transfer/upload/#upload-tiering-staged-uploads)).
4//!
5//! Uploads used to be all-or-nothing; **staged uploads** add the upload-direction
6//! ladder for low-data situations (a metered plan, weeks from Wi-Fi) where what
7//! matters most is that the *index* of what exists escapes the device. This module
8//! owns the two closed contract enums and the one pure invariant they carry — the
9//! **staged × streaming exclusion**. Everything network-facing (the scheduler that
10//! opens sessions in tier order, gated by the connection class) lives in the SDK
11//! half (`capsule_sdk::staged`); `capsule-core` stays network-free.
12//!
13//! The policy is **client-side session ordering only** — the server has zero mode
14//! branches (transfer upload doc). Under [`UploadPolicy::Staged`] the scheduler opens
15//! each asset's sessions in [`UploadTier`] order, gating each tier on the sync
16//! connection criteria; under [`UploadPolicy::Full`] all sessions open eagerly.
17
18/// The per-device upload policy: a closed enum with exactly two members.
19///
20/// The choice is *ordering*, not a distinct code path — the same `POST /upload`
21/// sessions, bundle mechanics, and finalization run under both; `staged` simply
22/// hasn't opened the higher-tier sessions yet.
23#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
24pub enum UploadPolicy {
25    /// Every session of an asset's bundle opens eagerly, in any order (today's
26    /// behavior, and the default).
27    #[default]
28    Full,
29    /// Sessions open in tier order (index → preview → original) per asset, each
30    /// tier gated by the connection class. Mutually exclusive with streaming
31    /// import ([`ensure_streaming_compatible`]).
32    Staged,
33}
34
35impl UploadPolicy {
36    /// Whether this policy stages sessions in tier order (vs. opening eagerly).
37    #[must_use]
38    pub fn is_staged(self) -> bool {
39        matches!(self, Self::Staged)
40    }
41}
42
43/// The upload tier ladder, mirroring the download ladder. Tiers map directly onto
44/// existing blob roles — **no new blob kind exists for staging**. The derived
45/// `Ord` is the ladder order (`Index < Preview < Original`), so a scheduler emits
46/// sessions strictly T0 → T1 → T2 by sorting.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
48pub enum UploadTier {
49    /// T0: signed manifest + metadata blob (embedded LQIP) — the index that makes
50    /// the asset visible (`awaiting-original`) on other devices. A few KB per
51    /// asset; escapes on any usable connection.
52    Index,
53    /// T1: thumbnail + preview derivative blobs. Needs a non-metered link
54    /// (small-reconciliation rule).
55    Preview,
56    /// T2: the original blob; its finalization flips `original_held` on the sync
57    /// feed and unlocks every release path (verify-before-destroy). Needs unmetered
58    /// Wi-Fi (large-reconciliation rule) or explicit force-sync.
59    Original,
60}
61
62impl UploadTier {
63    /// The tiers in strict ladder order (T0 → T1 → T2) — the ordering input a
64    /// staged scheduler iterates.
65    pub const LADDER: [UploadTier; 3] = [Self::Index, Self::Preview, Self::Original];
66}
67
68/// Raised when a staged upload policy is combined with a storage-constrained
69/// streaming import for the same run — the combination the planner rejects
70/// outright (transfer upload doc: "Staged and streaming import are mutually
71/// exclusive per import").
72#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
73#[error(
74    "staged upload policy is mutually exclusive with streaming import: streaming releases local \
75     bytes quickly, staged defers exactly the upload release depends on"
76)]
77pub struct StagedStreamingConflict;
78
79/// The pure staged × streaming exclusion invariant.
80///
81/// Streaming exists to release local bytes as quickly as possible; staged defers
82/// exactly the T2 (original) upload that release depends on. Combining them is a
83/// contradiction, so it is refused at confirmation and — belt and braces — the
84/// streaming executor refuses a staged policy by construction. Returns
85/// [`StagedStreamingConflict`] iff `policy` is [`UploadPolicy::Staged`] and
86/// `streaming` is set.
87pub fn ensure_streaming_compatible(
88    policy: UploadPolicy,
89    streaming: bool,
90) -> Result<(), StagedStreamingConflict> {
91    if policy.is_staged() && streaming {
92        Err(StagedStreamingConflict)
93    } else {
94        Ok(())
95    }
96}
97
98#[cfg(test)]
99mod tests;