use std::path::PathBuf;
use bytes::Bytes;
use iroh::NodeAddr;
use iroh_blobs::{export::ExportProgress, store::ExportMode, Hash};
use nested_enum_utils::enum_conversions;
use quic_rpc::pattern::try_server_streaming::StreamCreated;
use quic_rpc_derive::rpc_requests;
use serde::{Deserialize, Serialize};
use super::{
client::docs::{ImportProgress, ShareMode},
AddrInfoOptions, RpcError, RpcResult,
};
use crate::{
actor::OpenState,
engine::LiveEvent,
store::{DownloadPolicy, Query},
Author, AuthorId, Capability, CapabilityKind, DocTicket, Entry, NamespaceId, PeerIdBytes,
SignedEntry,
};
#[derive(Debug, Clone)]
pub struct RpcService;
impl quic_rpc::Service for RpcService {
type Req = Request;
type Res = Response;
}
#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[enum_conversions]
#[rpc_requests(RpcService)]
pub enum Request {
#[rpc(response = RpcResult<OpenResponse>)]
Open(OpenRequest),
#[rpc(response = RpcResult<CloseResponse>)]
Close(CloseRequest),
#[rpc(response = RpcResult<StatusResponse>)]
Status(StatusRequest),
#[server_streaming(response = RpcResult<ListResponse>)]
List(DocListRequest),
#[rpc(response = RpcResult<CreateResponse>)]
Create(CreateRequest),
#[rpc(response = RpcResult<DropResponse>)]
Drop(DropRequest),
#[rpc(response = RpcResult<ImportResponse>)]
Import(ImportRequest),
#[rpc(response = RpcResult<SetResponse>)]
Set(SetRequest),
#[rpc(response = RpcResult<SetHashResponse>)]
SetHash(SetHashRequest),
#[server_streaming(response = RpcResult<GetManyResponse>)]
Get(GetManyRequest),
#[rpc(response = RpcResult<GetExactResponse>)]
GetExact(GetExactRequest),
#[server_streaming(response = ImportFileResponse)]
ImportFile(ImportFileRequest),
#[server_streaming(response = ExportFileResponse)]
ExportFile(ExportFileRequest),
#[rpc(response = RpcResult<DelResponse>)]
Del(DelRequest),
#[rpc(response = RpcResult<StartSyncResponse>)]
StartSync(StartSyncRequest),
#[rpc(response = RpcResult<LeaveResponse>)]
Leave(LeaveRequest),
#[rpc(response = RpcResult<ShareResponse>)]
Share(ShareRequest),
#[try_server_streaming(create_error = RpcError, item_error = RpcError, item = DocSubscribeResponse)]
Subscribe(DocSubscribeRequest),
#[rpc(response = RpcResult<GetDownloadPolicyResponse>)]
GetDownloadPolicy(GetDownloadPolicyRequest),
#[rpc(response = RpcResult<SetDownloadPolicyResponse>)]
SetDownloadPolicy(SetDownloadPolicyRequest),
#[rpc(response = RpcResult<GetSyncPeersResponse>)]
GetSyncPeers(GetSyncPeersRequest),
#[server_streaming(response = RpcResult<AuthorListResponse>)]
AuthorList(AuthorListRequest),
#[rpc(response = RpcResult<AuthorCreateResponse>)]
AuthorCreate(AuthorCreateRequest),
#[rpc(response = RpcResult<AuthorGetDefaultResponse>)]
AuthorGetDefault(AuthorGetDefaultRequest),
#[rpc(response = RpcResult<AuthorSetDefaultResponse>)]
AuthorSetDefault(AuthorSetDefaultRequest),
#[rpc(response = RpcResult<AuthorImportResponse>)]
AuthorImport(AuthorImportRequest),
#[rpc(response = RpcResult<AuthorExportResponse>)]
AuthorExport(AuthorExportRequest),
#[rpc(response = RpcResult<AuthorDeleteResponse>)]
AuthorDelete(AuthorDeleteRequest),
}
#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[enum_conversions]
pub enum Response {
Open(RpcResult<OpenResponse>),
Close(RpcResult<CloseResponse>),
Status(RpcResult<StatusResponse>),
List(RpcResult<ListResponse>),
Create(RpcResult<CreateResponse>),
Drop(RpcResult<DropResponse>),
Import(RpcResult<ImportResponse>),
Set(RpcResult<SetResponse>),
SetHash(RpcResult<SetHashResponse>),
Get(RpcResult<GetManyResponse>),
GetExact(RpcResult<GetExactResponse>),
ImportFile(ImportFileResponse),
ExportFile(ExportFileResponse),
Del(RpcResult<DelResponse>),
Share(RpcResult<ShareResponse>),
StartSync(RpcResult<StartSyncResponse>),
Leave(RpcResult<LeaveResponse>),
Subscribe(RpcResult<DocSubscribeResponse>),
GetDownloadPolicy(RpcResult<GetDownloadPolicyResponse>),
SetDownloadPolicy(RpcResult<SetDownloadPolicyResponse>),
GetSyncPeers(RpcResult<GetSyncPeersResponse>),
StreamCreated(RpcResult<StreamCreated>),
AuthorList(RpcResult<AuthorListResponse>),
AuthorCreate(RpcResult<AuthorCreateResponse>),
AuthorGetDefault(RpcResult<AuthorGetDefaultResponse>),
AuthorSetDefault(RpcResult<AuthorSetDefaultResponse>),
AuthorImport(RpcResult<AuthorImportResponse>),
AuthorExport(RpcResult<AuthorExportResponse>),
AuthorDelete(RpcResult<AuthorDeleteResponse>),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DocSubscribeRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DocSubscribeResponse {
pub event: LiveEvent,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DocListRequest {}
#[derive(Serialize, Deserialize, Debug)]
pub struct ListResponse {
pub id: NamespaceId,
pub capability: CapabilityKind,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateRequest {}
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateResponse {
pub id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ImportRequest {
pub capability: Capability,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ImportResponse {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ShareRequest {
pub doc_id: NamespaceId,
pub mode: ShareMode,
pub addr_options: AddrInfoOptions,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ShareResponse(pub DocTicket);
#[derive(Serialize, Deserialize, Debug)]
pub struct StatusRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct StatusResponse {
pub status: OpenState,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct OpenRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct OpenResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct CloseRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CloseResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct StartSyncRequest {
pub doc_id: NamespaceId,
pub peers: Vec<NodeAddr>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct StartSyncResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct LeaveRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LeaveResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct DropRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DropResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetRequest {
pub doc_id: NamespaceId,
pub author_id: AuthorId,
pub key: Bytes,
pub value: Bytes,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetResponse {
pub entry: SignedEntry,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ImportFileRequest {
pub doc_id: NamespaceId,
pub author_id: AuthorId,
pub key: Bytes,
pub path: PathBuf,
pub in_place: bool,
}
#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
pub struct ImportFileResponse(pub ImportProgress);
#[derive(Debug, Serialize, Deserialize)]
pub struct ExportFileRequest {
pub entry: Entry,
pub path: PathBuf,
pub mode: ExportMode,
}
#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
pub struct ExportFileResponse(pub ExportProgress);
#[derive(Serialize, Deserialize, Debug)]
pub struct DelRequest {
pub doc_id: NamespaceId,
pub author_id: AuthorId,
pub prefix: Bytes,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DelResponse {
pub removed: usize,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetHashRequest {
pub doc_id: NamespaceId,
pub author_id: AuthorId,
pub key: Bytes,
pub hash: Hash,
pub size: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetHashResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetManyRequest {
pub doc_id: NamespaceId,
pub query: Query,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetManyResponse {
pub entry: SignedEntry,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetExactRequest {
pub doc_id: NamespaceId,
pub key: Bytes,
pub author: AuthorId,
pub include_empty: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetExactResponse {
pub entry: Option<SignedEntry>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetDownloadPolicyRequest {
pub doc_id: NamespaceId,
pub policy: DownloadPolicy,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SetDownloadPolicyResponse {}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetDownloadPolicyRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetDownloadPolicyResponse {
pub policy: DownloadPolicy,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetSyncPeersRequest {
pub doc_id: NamespaceId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetSyncPeersResponse {
pub peers: Option<Vec<PeerIdBytes>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorListRequest {}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorListResponse {
pub author_id: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorCreateRequest;
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorCreateResponse {
pub author_id: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorGetDefaultRequest;
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorGetDefaultResponse {
pub author_id: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorSetDefaultRequest {
pub author_id: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorSetDefaultResponse;
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorDeleteRequest {
pub author: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorDeleteResponse;
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorExportRequest {
pub author: AuthorId,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorExportResponse {
pub author: Option<Author>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorImportRequest {
pub author: Author,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthorImportResponse {
pub author_id: AuthorId,
}