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 registered authoritative query or command request. Hosts that do
61 /// not expose `<mount>/operations` keep the default fail-loud behavior.
62 fn remote_operation(&mut self, request: &[u8]) -> Result<Vec<u8>, TransportError> {
63 let _ = request;
64 Err(TransportError::new(
65 "client.remote_operations_unconfigured",
66 "this transport has no remote operation endpoint",
67 ))
68 }
69 /// One combined push+pull round over the realtime socket (§8.7). The
70 /// host owns the WS-binding mechanics — channel tags, chunk assembly
71 /// to the response's END — and returns the assembled response bytes.
72 /// The client calls this instead of `sync` whenever realtime is
73 /// connected (the socket IS the sync-round
74 /// transport, not a fallback pair); the server registers the round's
75 /// subscriptions on the connection at round end, so no reconnect is
76 /// needed after subscription changes.
77 fn realtime_sync(&mut self, request: &[u8]) -> Result<Vec<u8>, TransportError>;
78 /// Segment download via the direct endpoint (§5.5).
79 fn download_segment(&mut self, request: &SegmentRequest) -> Result<Vec<u8>, TransportError>;
80 /// §5.4 direct URL fetch capability: `true` makes the client
81 /// advertise accept bit 3 (capability negotiation, §4.2). Default:
82 /// not capable.
83 fn supports_url_fetch(&self) -> bool {
84 false
85 }
86 /// Plain GET of a signed URL (§5.4). The URL is the entire grant —
87 /// implementations MUST NOT attach sync-server authentication or the
88 /// `X-Syncular-Scopes` header. Only called when `supports_url_fetch`
89 /// returned `true`.
90 fn fetch_url(&mut self, url: &str) -> Result<Vec<u8>, TransportError> {
91 let _ = url;
92 Err(TransportError::new(
93 "sync.invalid_request",
94 "this transport has no direct URL fetch (§5.4)",
95 ))
96 }
97 /// §5.9.3 blob upload: host-authenticated `PUT <mount>/blobs/{blobId}`.
98 /// The server verifies the content address. Default: unsupported.
99 fn blob_upload(
100 &mut self,
101 blob_id: &str,
102 bytes: &[u8],
103 media_type: Option<&str>,
104 ) -> Result<(), TransportError> {
105 let _ = (blob_id, bytes, media_type);
106 Err(TransportError::new(
107 "sync.invalid_request",
108 "this transport has no blob upload (§5.9)",
109 ))
110 }
111 /// §5.9.5 blob download: host-authenticated `GET <mount>/blobs/{blobId}`,
112 /// re-authorized server-side against referencing rows. Returns inline
113 /// bytes OR (always-issue, presign configured) a signed url the client
114 /// fetches directly. Default: none.
115 fn blob_download(&mut self, blob_id: &str) -> Result<BlobDownload, TransportError> {
116 let _ = blob_id;
117 Err(TransportError::new(
118 "blob.not_found",
119 "this transport has no blob download (§5.9)",
120 ))
121 }
122 /// §5.9.5 presigned-download fetch: a bare GET of the signed url. MUST
123 /// attach NO host authentication — the url is the entire grant (§5.4).
124 /// Only called when `blob_download` returned a `Url` arm.
125 fn fetch_blob_url(&mut self, url: &str) -> Result<Vec<u8>, TransportError> {
126 let _ = url;
127 Err(TransportError::new(
128 "sync.invalid_request",
129 "this transport has no blob url fetch (§5.9.5)",
130 ))
131 }
132 /// §5.9.3 presigned-upload grant: `POST /blobs/{blobId}/upload-grant` with
133 /// the declared size. Absent (`None`) ⇒ the client always streams through
134 /// `blob_upload`. A `Url` grant is PUT via `blob_put_url`.
135 fn blob_upload_grant(
136 &mut self,
137 blob_id: &str,
138 byte_length: u64,
139 media_type: Option<&str>,
140 ) -> Result<BlobUploadGrant, TransportError> {
141 let _ = (blob_id, byte_length, media_type);
142 Ok(BlobUploadGrant::None)
143 }
144 /// §5.9.3 direct-to-storage PUT of the granted url. MUST attach NO host
145 /// authentication — the presigned url is the entire grant (§5.4). Only
146 /// called when `blob_upload_grant` returned a `Url` arm.
147 fn blob_put_url(
148 &mut self,
149 url: &str,
150 bytes: &[u8],
151 media_type: Option<&str>,
152 ) -> Result<(), TransportError> {
153 let _ = (url, bytes, media_type);
154 Err(TransportError::new(
155 "sync.invalid_request",
156 "this transport has no blob put url (§5.9.3)",
157 ))
158 }
159 /// Realtime attach (§8.1). Inbound traffic is delivered by the host via
160 /// `SyncClient::on_realtime_text` / `on_realtime_binary`.
161 /// Legacy host-configured realtime connection. Implementations which do
162 /// not bind identity at the URL layer may implement only this method.
163 fn realtime_connect(&mut self) -> Result<(), TransportError>;
164 /// Open realtime for this exact persisted client identity. Native socket
165 /// servers bind registrations/cursors to the URL-level client id before
166 /// any protocol frame is exchanged, so client-owned hosts override this.
167 /// The default preserves compatibility with connectors that already close
168 /// over their identity.
169 fn realtime_connect_for_client(&mut self, _client_id: &str) -> Result<(), TransportError> {
170 self.realtime_connect()
171 }
172 /// Client → server JSON control message (acks, §8.2).
173 fn realtime_send(&mut self, text: &str) -> Result<(), TransportError>;
174 fn realtime_close(&mut self) -> Result<(), TransportError>;
175}