1use std::path::PathBuf;
4
5use bytes::Bytes;
6use iroh::NodeAddr;
7use iroh_blobs::{export::ExportProgress, store::ExportMode, Hash};
8use nested_enum_utils::enum_conversions;
9use quic_rpc::pattern::try_server_streaming::StreamCreated;
10use quic_rpc_derive::rpc_requests;
11use serde::{Deserialize, Serialize};
12
13use super::{
14 client::docs::{ImportProgress, ShareMode},
15 AddrInfoOptions, RpcError, RpcResult,
16};
17use crate::{
18 actor::OpenState,
19 engine::LiveEvent,
20 store::{DownloadPolicy, Query},
21 Author, AuthorId, Capability, CapabilityKind, DocTicket, Entry, NamespaceId, PeerIdBytes,
22 SignedEntry,
23};
24
25#[derive(Debug, Clone)]
27pub struct RpcService;
28
29impl quic_rpc::Service for RpcService {
30 type Req = Request;
31 type Res = Response;
32}
33
34#[allow(missing_docs)]
35#[derive(strum::Display, Debug, Serialize, Deserialize)]
36#[enum_conversions]
37#[rpc_requests(RpcService)]
38pub enum Request {
39 #[rpc(response = RpcResult<OpenResponse>)]
40 Open(OpenRequest),
41 #[rpc(response = RpcResult<CloseResponse>)]
42 Close(CloseRequest),
43 #[rpc(response = RpcResult<StatusResponse>)]
44 Status(StatusRequest),
45 #[server_streaming(response = RpcResult<ListResponse>)]
46 List(DocListRequest),
47 #[rpc(response = RpcResult<CreateResponse>)]
48 Create(CreateRequest),
49 #[rpc(response = RpcResult<DropResponse>)]
50 Drop(DropRequest),
51 #[rpc(response = RpcResult<ImportResponse>)]
52 Import(ImportRequest),
53 #[rpc(response = RpcResult<SetResponse>)]
54 Set(SetRequest),
55 #[rpc(response = RpcResult<SetHashResponse>)]
56 SetHash(SetHashRequest),
57 #[server_streaming(response = RpcResult<GetManyResponse>)]
58 Get(GetManyRequest),
59 #[rpc(response = RpcResult<GetExactResponse>)]
60 GetExact(GetExactRequest),
61 #[server_streaming(response = ImportFileResponse)]
62 ImportFile(ImportFileRequest),
63 #[server_streaming(response = ExportFileResponse)]
64 ExportFile(ExportFileRequest),
65 #[rpc(response = RpcResult<DelResponse>)]
66 Del(DelRequest),
67 #[rpc(response = RpcResult<StartSyncResponse>)]
68 StartSync(StartSyncRequest),
69 #[rpc(response = RpcResult<LeaveResponse>)]
70 Leave(LeaveRequest),
71 #[rpc(response = RpcResult<ShareResponse>)]
72 Share(ShareRequest),
73 #[try_server_streaming(create_error = RpcError, item_error = RpcError, item = DocSubscribeResponse)]
74 Subscribe(DocSubscribeRequest),
75 #[rpc(response = RpcResult<GetDownloadPolicyResponse>)]
76 GetDownloadPolicy(GetDownloadPolicyRequest),
77 #[rpc(response = RpcResult<SetDownloadPolicyResponse>)]
78 SetDownloadPolicy(SetDownloadPolicyRequest),
79 #[rpc(response = RpcResult<GetSyncPeersResponse>)]
80 GetSyncPeers(GetSyncPeersRequest),
81 #[server_streaming(response = RpcResult<AuthorListResponse>)]
82 AuthorList(AuthorListRequest),
83 #[rpc(response = RpcResult<AuthorCreateResponse>)]
84 AuthorCreate(AuthorCreateRequest),
85 #[rpc(response = RpcResult<AuthorGetDefaultResponse>)]
86 AuthorGetDefault(AuthorGetDefaultRequest),
87 #[rpc(response = RpcResult<AuthorSetDefaultResponse>)]
88 AuthorSetDefault(AuthorSetDefaultRequest),
89 #[rpc(response = RpcResult<AuthorImportResponse>)]
90 AuthorImport(AuthorImportRequest),
91 #[rpc(response = RpcResult<AuthorExportResponse>)]
92 AuthorExport(AuthorExportRequest),
93 #[rpc(response = RpcResult<AuthorDeleteResponse>)]
94 AuthorDelete(AuthorDeleteRequest),
95}
96
97#[allow(missing_docs)]
98#[derive(strum::Display, Debug, Serialize, Deserialize)]
99#[enum_conversions]
100pub enum Response {
101 Open(RpcResult<OpenResponse>),
102 Close(RpcResult<CloseResponse>),
103 Status(RpcResult<StatusResponse>),
104 List(RpcResult<ListResponse>),
105 Create(RpcResult<CreateResponse>),
106 Drop(RpcResult<DropResponse>),
107 Import(RpcResult<ImportResponse>),
108 Set(RpcResult<SetResponse>),
109 SetHash(RpcResult<SetHashResponse>),
110 Get(RpcResult<GetManyResponse>),
111 GetExact(RpcResult<GetExactResponse>),
112 ImportFile(ImportFileResponse),
113 ExportFile(ExportFileResponse),
114 Del(RpcResult<DelResponse>),
115 Share(RpcResult<ShareResponse>),
116 StartSync(RpcResult<StartSyncResponse>),
117 Leave(RpcResult<LeaveResponse>),
118 Subscribe(RpcResult<DocSubscribeResponse>),
119 GetDownloadPolicy(RpcResult<GetDownloadPolicyResponse>),
120 SetDownloadPolicy(RpcResult<SetDownloadPolicyResponse>),
121 GetSyncPeers(RpcResult<GetSyncPeersResponse>),
122 StreamCreated(RpcResult<StreamCreated>),
123 AuthorList(RpcResult<AuthorListResponse>),
124 AuthorCreate(RpcResult<AuthorCreateResponse>),
125 AuthorGetDefault(RpcResult<AuthorGetDefaultResponse>),
126 AuthorSetDefault(RpcResult<AuthorSetDefaultResponse>),
127 AuthorImport(RpcResult<AuthorImportResponse>),
128 AuthorExport(RpcResult<AuthorExportResponse>),
129 AuthorDelete(RpcResult<AuthorDeleteResponse>),
130}
131
132#[derive(Serialize, Deserialize, Debug)]
134pub struct DocSubscribeRequest {
135 pub doc_id: NamespaceId,
137}
138
139#[derive(Serialize, Deserialize, Debug)]
141pub struct DocSubscribeResponse {
142 pub event: LiveEvent,
144}
145
146#[derive(Serialize, Deserialize, Debug)]
148pub struct DocListRequest {}
149
150#[derive(Serialize, Deserialize, Debug)]
152pub struct ListResponse {
153 pub id: NamespaceId,
155 pub capability: CapabilityKind,
157}
158
159#[derive(Serialize, Deserialize, Debug)]
161pub struct CreateRequest {}
162
163#[derive(Serialize, Deserialize, Debug)]
165pub struct CreateResponse {
166 pub id: NamespaceId,
168}
169
170#[derive(Serialize, Deserialize, Debug)]
172pub struct ImportRequest {
173 pub capability: Capability,
175}
176
177#[derive(Serialize, Deserialize, Debug)]
179pub struct ImportResponse {
180 pub doc_id: NamespaceId,
182}
183
184#[derive(Serialize, Deserialize, Debug)]
186pub struct ShareRequest {
187 pub doc_id: NamespaceId,
189 pub mode: ShareMode,
191 pub addr_options: AddrInfoOptions,
193}
194
195#[derive(Serialize, Deserialize, Debug)]
197pub struct ShareResponse(pub DocTicket);
198
199#[derive(Serialize, Deserialize, Debug)]
201pub struct StatusRequest {
202 pub doc_id: NamespaceId,
204}
205
206#[derive(Serialize, Deserialize, Debug)]
209pub struct StatusResponse {
210 pub status: OpenState,
212}
213
214#[derive(Serialize, Deserialize, Debug)]
216pub struct OpenRequest {
217 pub doc_id: NamespaceId,
219}
220
221#[derive(Serialize, Deserialize, Debug)]
223pub struct OpenResponse {}
224
225#[derive(Serialize, Deserialize, Debug)]
227pub struct CloseRequest {
228 pub doc_id: NamespaceId,
230}
231
232#[derive(Serialize, Deserialize, Debug)]
234pub struct CloseResponse {}
235
236#[derive(Serialize, Deserialize, Debug)]
238pub struct StartSyncRequest {
239 pub doc_id: NamespaceId,
241 pub peers: Vec<NodeAddr>,
243}
244
245#[derive(Serialize, Deserialize, Debug)]
247pub struct StartSyncResponse {}
248
249#[derive(Serialize, Deserialize, Debug)]
251pub struct LeaveRequest {
252 pub doc_id: NamespaceId,
254}
255
256#[derive(Serialize, Deserialize, Debug)]
258pub struct LeaveResponse {}
259
260#[derive(Serialize, Deserialize, Debug)]
262pub struct DropRequest {
263 pub doc_id: NamespaceId,
265}
266
267#[derive(Serialize, Deserialize, Debug)]
269pub struct DropResponse {}
270
271#[derive(Serialize, Deserialize, Debug)]
273pub struct SetRequest {
274 pub doc_id: NamespaceId,
276 pub author_id: AuthorId,
278 pub key: Bytes,
280 pub value: Bytes,
284}
285
286#[derive(Serialize, Deserialize, Debug)]
288pub struct SetResponse {
289 pub entry: SignedEntry,
291}
292
293#[derive(Debug, Serialize, Deserialize)]
297pub struct ImportFileRequest {
298 pub doc_id: NamespaceId,
300 pub author_id: AuthorId,
302 pub key: Bytes,
304 pub path: PathBuf,
310 pub in_place: bool,
313}
314
315#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
317pub struct ImportFileResponse(pub ImportProgress);
318
319#[derive(Debug, Serialize, Deserialize)]
323pub struct ExportFileRequest {
324 pub entry: Entry,
326 pub path: PathBuf,
332 pub mode: ExportMode,
335}
336
337#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
342pub struct ExportFileResponse(pub ExportProgress);
343
344#[derive(Serialize, Deserialize, Debug)]
346pub struct DelRequest {
347 pub doc_id: NamespaceId,
349 pub author_id: AuthorId,
351 pub prefix: Bytes,
353}
354
355#[derive(Serialize, Deserialize, Debug)]
357pub struct DelResponse {
358 pub removed: usize,
360}
361
362#[derive(Serialize, Deserialize, Debug)]
364pub struct SetHashRequest {
365 pub doc_id: NamespaceId,
367 pub author_id: AuthorId,
369 pub key: Bytes,
371 pub hash: Hash,
373 pub size: u64,
375}
376
377#[derive(Serialize, Deserialize, Debug)]
379pub struct SetHashResponse {}
380
381#[derive(Serialize, Deserialize, Debug)]
383pub struct GetManyRequest {
384 pub doc_id: NamespaceId,
386 pub query: Query,
388}
389
390#[derive(Serialize, Deserialize, Debug)]
392pub struct GetManyResponse {
393 pub entry: SignedEntry,
395}
396
397#[derive(Serialize, Deserialize, Debug)]
399pub struct GetExactRequest {
400 pub doc_id: NamespaceId,
402 pub key: Bytes,
404 pub author: AuthorId,
406 pub include_empty: bool,
408}
409
410#[derive(Serialize, Deserialize, Debug)]
412pub struct GetExactResponse {
413 pub entry: Option<SignedEntry>,
415}
416
417#[derive(Serialize, Deserialize, Debug)]
419pub struct SetDownloadPolicyRequest {
420 pub doc_id: NamespaceId,
422 pub policy: DownloadPolicy,
424}
425
426#[derive(Serialize, Deserialize, Debug)]
428pub struct SetDownloadPolicyResponse {}
429
430#[derive(Serialize, Deserialize, Debug)]
432pub struct GetDownloadPolicyRequest {
433 pub doc_id: NamespaceId,
435}
436
437#[derive(Serialize, Deserialize, Debug)]
439pub struct GetDownloadPolicyResponse {
440 pub policy: DownloadPolicy,
442}
443
444#[derive(Serialize, Deserialize, Debug)]
446pub struct GetSyncPeersRequest {
447 pub doc_id: NamespaceId,
449}
450
451#[derive(Serialize, Deserialize, Debug)]
453pub struct GetSyncPeersResponse {
454 pub peers: Option<Vec<PeerIdBytes>>,
456}
457
458#[derive(Serialize, Deserialize, Debug)]
460pub struct AuthorListRequest {}
461
462#[derive(Serialize, Deserialize, Debug)]
464pub struct AuthorListResponse {
465 pub author_id: AuthorId,
467}
468
469#[derive(Serialize, Deserialize, Debug)]
471pub struct AuthorCreateRequest;
472
473#[derive(Serialize, Deserialize, Debug)]
475pub struct AuthorCreateResponse {
476 pub author_id: AuthorId,
478}
479
480#[derive(Serialize, Deserialize, Debug)]
482pub struct AuthorGetDefaultRequest;
483
484#[derive(Serialize, Deserialize, Debug)]
486pub struct AuthorGetDefaultResponse {
487 pub author_id: AuthorId,
489}
490
491#[derive(Serialize, Deserialize, Debug)]
493pub struct AuthorSetDefaultRequest {
494 pub author_id: AuthorId,
496}
497
498#[derive(Serialize, Deserialize, Debug)]
500pub struct AuthorSetDefaultResponse;
501
502#[derive(Serialize, Deserialize, Debug)]
504pub struct AuthorDeleteRequest {
505 pub author: AuthorId,
507}
508
509#[derive(Serialize, Deserialize, Debug)]
511pub struct AuthorDeleteResponse;
512
513#[derive(Serialize, Deserialize, Debug)]
515pub struct AuthorExportRequest {
516 pub author: AuthorId,
518}
519
520#[derive(Serialize, Deserialize, Debug)]
522pub struct AuthorExportResponse {
523 pub author: Option<Author>,
525}
526
527#[derive(Serialize, Deserialize, Debug)]
529pub struct AuthorImportRequest {
530 pub author: Author,
532}
533
534#[derive(Serialize, Deserialize, Debug)]
536pub struct AuthorImportResponse {
537 pub author_id: AuthorId,
539}