Skip to main content

syncular_client/
transport.rs

1//! The transport seam handed TO the client by its host (the conformance
2//! harness, an app shell, …). Bytes and strings only — mirroring the
3//! `ClientEndpoints` inversion of the conformance driver contract. The
4//! client is synchronous: the driver protocol is request/response.
5
6/// A transport-level or request-level failure (§1.1 HTTP-JSON surface).
7#[derive(Debug, Clone)]
8pub struct TransportError {
9    pub code: String,
10    pub message: String,
11}
12
13impl TransportError {
14    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
15        TransportError {
16            code: code.into(),
17            message: message.into(),
18        }
19    }
20}
21
22/// Direct-endpoint segment fetch (§5.5). Signed-URL descriptors never
23/// reach this call: the §5.4 resolution lives in the client core, which
24/// routes url-carrying descriptors through `fetch_url` instead.
25#[derive(Debug, Clone)]
26pub struct SegmentRequest {
27    pub segment_id: String,
28    pub table: String,
29    /// Canonical JSON (§11.2) of the requested scope map (§5.5).
30    pub requested_scopes_json: String,
31}
32
33/// A §5.9.5 blob download result: inline bytes, or a presigned url the client
34/// fetches directly (always-issue). `url_expires_at_ms` is present iff `url`.
35#[derive(Debug, Clone)]
36pub enum BlobDownload {
37    Bytes(Vec<u8>),
38    Url {
39        url: String,
40        url_expires_at_ms: Option<i64>,
41    },
42}
43
44/// A §5.9.3 presigned-upload grant: a single PUT url, an already-present
45/// marker (skip the PUT), or none (stream through the direct endpoint).
46#[derive(Debug, Clone)]
47pub enum BlobUploadGrant {
48    Url {
49        url: String,
50        url_expires_at_ms: Option<i64>,
51    },
52    Present,
53    None,
54}
55
56pub trait Transport {
57    /// One combined push+pull round trip (§1.5) over the request/response
58    /// binding (`POST /sync`, loopback, …).
59    fn sync(&mut self, request: &[u8]) -> Result<Vec<u8>, TransportError>;
60    /// One combined push+pull round over the realtime socket (§8.7). The
61    /// host owns the WS-binding mechanics — channel tags, chunk assembly
62    /// to the response's END — and returns the assembled response bytes.
63    /// The client calls this instead of `sync` whenever realtime is
64    /// connected (Direction decision 1: the socket IS the sync-round
65    /// transport, not a fallback pair); the server registers the round's
66    /// subscriptions on the connection at round end, so no reconnect is
67    /// needed after subscription changes.
68    fn realtime_sync(&mut self, request: &[u8]) -> Result<Vec<u8>, TransportError>;
69    /// Segment download via the direct endpoint (§5.5).
70    fn download_segment(&mut self, request: &SegmentRequest) -> Result<Vec<u8>, TransportError>;
71    /// §5.4 direct URL fetch capability: `true` makes the client
72    /// advertise accept bit 3 (capability negotiation, §4.2). Default:
73    /// not capable.
74    fn supports_url_fetch(&self) -> bool {
75        false
76    }
77    /// Plain GET of a signed URL (§5.4). The URL is the entire grant —
78    /// implementations MUST NOT attach sync-server authentication or the
79    /// `X-Syncular-Scopes` header. Only called when `supports_url_fetch`
80    /// returned `true`.
81    fn fetch_url(&mut self, url: &str) -> Result<Vec<u8>, TransportError> {
82        let _ = url;
83        Err(TransportError::new(
84            "sync.invalid_request",
85            "this transport has no direct URL fetch (§5.4)",
86        ))
87    }
88    /// §5.9.3 blob upload: host-authenticated `PUT <mount>/blobs/{blobId}`.
89    /// The server verifies the content address. Default: unsupported.
90    fn blob_upload(
91        &mut self,
92        blob_id: &str,
93        bytes: &[u8],
94        media_type: Option<&str>,
95    ) -> Result<(), TransportError> {
96        let _ = (blob_id, bytes, media_type);
97        Err(TransportError::new(
98            "sync.invalid_request",
99            "this transport has no blob upload (§5.9)",
100        ))
101    }
102    /// §5.9.5 blob download: host-authenticated `GET <mount>/blobs/{blobId}`,
103    /// re-authorized server-side against referencing rows. Returns inline
104    /// bytes OR (always-issue, presign configured) a signed url the client
105    /// fetches directly. Default: none.
106    fn blob_download(&mut self, blob_id: &str) -> Result<BlobDownload, TransportError> {
107        let _ = blob_id;
108        Err(TransportError::new(
109            "blob.not_found",
110            "this transport has no blob download (§5.9)",
111        ))
112    }
113    /// §5.9.5 presigned-download fetch: a bare GET of the signed url. MUST
114    /// attach NO host authentication — the url is the entire grant (§5.4).
115    /// Only called when `blob_download` returned a `Url` arm.
116    fn fetch_blob_url(&mut self, url: &str) -> Result<Vec<u8>, TransportError> {
117        let _ = url;
118        Err(TransportError::new(
119            "sync.invalid_request",
120            "this transport has no blob url fetch (§5.9.5)",
121        ))
122    }
123    /// §5.9.3 presigned-upload grant: `POST /blobs/{blobId}/upload-grant` with
124    /// the declared size. Absent (`None`) ⇒ the client always streams through
125    /// `blob_upload`. A `Url` grant is PUT via `blob_put_url`.
126    fn blob_upload_grant(
127        &mut self,
128        blob_id: &str,
129        byte_length: u64,
130        media_type: Option<&str>,
131    ) -> Result<BlobUploadGrant, TransportError> {
132        let _ = (blob_id, byte_length, media_type);
133        Ok(BlobUploadGrant::None)
134    }
135    /// §5.9.3 direct-to-storage PUT of the granted url. MUST attach NO host
136    /// authentication — the presigned url is the entire grant (§5.4). Only
137    /// called when `blob_upload_grant` returned a `Url` arm.
138    fn blob_put_url(
139        &mut self,
140        url: &str,
141        bytes: &[u8],
142        media_type: Option<&str>,
143    ) -> Result<(), TransportError> {
144        let _ = (url, bytes, media_type);
145        Err(TransportError::new(
146            "sync.invalid_request",
147            "this transport has no blob put url (§5.9.3)",
148        ))
149    }
150    /// Realtime attach (§8.1). Inbound traffic is delivered by the host via
151    /// `SyncClient::on_realtime_text` / `on_realtime_binary`.
152    fn realtime_connect(&mut self) -> Result<(), TransportError>;
153    /// Client → server JSON control message (acks, §8.2).
154    fn realtime_send(&mut self, text: &str) -> Result<(), TransportError>;
155    fn realtime_close(&mut self) -> Result<(), TransportError>;
156}