From 533baf955f4b0f607f9204521695e23bd601391a Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 6 Dec 2024 12:04:11 +0100 Subject: [PATCH] experiment: align rpc and regular api --- src/actor.rs | 28 +++++++++++++++++++++++----- src/rpc/client/authors.rs | 5 +++-- src/rpc/docs_handle_request.rs | 17 +++++++++-------- src/rpc/proto.rs | 11 ++--------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 98d0225..f86090d 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -32,11 +32,18 @@ use crate::{ const ACTION_CAP: usize = 1024; pub(crate) const MAX_COMMIT_DELAY: Duration = Duration::from_millis(500); +/// Import an author action. +#[derive(Debug, Serialize, Deserialize)] +pub struct ImportAuthorAction { + /// The author to import. + pub author: Author, +} + #[derive(derive_more::Debug, derive_more::Display)] enum Action { #[display("NewAuthor")] ImportAuthor { - author: Author, + action: ImportAuthorAction, #[debug("reply")] reply: oneshot::Sender>, }, @@ -500,9 +507,20 @@ impl SyncHandle { self.send(Action::ListReplicas { reply }).await } + /// Imports the given author. + /// + /// Warning: The [`Author`] struct contains sensitive data. pub async fn import_author(&self, author: Author) -> Result { + self.import_author_action(ImportAuthorAction { author }) + .await + } + + pub(crate) async fn import_author_action( + &self, + action: ImportAuthorAction, + ) -> Result { let (reply, rx) = oneshot::channel(); - self.send(Action::ImportAuthor { author, reply }).await?; + self.send(Action::ImportAuthor { action, reply }).await?; rx.await? } @@ -663,9 +681,9 @@ impl Actor { Action::Shutdown { .. } => { unreachable!("Shutdown is handled in run()") } - Action::ImportAuthor { author, reply } => { - let id = author.id(); - send_reply(reply, self.store.import_author(author).map(|_| id)) + Action::ImportAuthor { action, reply } => { + let id = action.author.id(); + send_reply(reply, self.store.import_author(action.author).map(|_| id)) } Action::ExportAuthor { author, reply } => { send_reply(reply, self.store.get_author(&author)) diff --git a/src/rpc/client/authors.rs b/src/rpc/client/authors.rs index 18a154f..3f363dc 100644 --- a/src/rpc/client/authors.rs +++ b/src/rpc/client/authors.rs @@ -10,9 +10,10 @@ use super::flatten; #[doc(inline)] pub use crate::engine::{Origin, SyncEvent, SyncReason}; use crate::{ + actor::ImportAuthorAction, rpc::proto::{ AuthorCreateRequest, AuthorDeleteRequest, AuthorExportRequest, AuthorGetDefaultRequest, - AuthorImportRequest, AuthorListRequest, AuthorSetDefaultRequest, RpcService, + AuthorListRequest, AuthorSetDefaultRequest, RpcService, }, Author, AuthorId, }; @@ -85,7 +86,7 @@ impl> Client { /// /// Warning: The [`Author`] struct contains sensitive data. pub async fn import(&self, author: Author) -> Result<()> { - self.rpc.rpc(AuthorImportRequest { author }).await??; + self.rpc.rpc(ImportAuthorAction { author }).await??; Ok(()) } diff --git a/src/rpc/docs_handle_request.rs b/src/rpc/docs_handle_request.rs index 2c147ff..a8bb23f 100644 --- a/src/rpc/docs_handle_request.rs +++ b/src/rpc/docs_handle_request.rs @@ -14,11 +14,11 @@ use super::{ proto::{ AuthorCreateRequest, AuthorCreateResponse, AuthorDeleteRequest, AuthorDeleteResponse, AuthorExportRequest, AuthorExportResponse, AuthorGetDefaultRequest, - AuthorGetDefaultResponse, AuthorImportRequest, AuthorImportResponse, AuthorListRequest, - AuthorListResponse, AuthorSetDefaultRequest, AuthorSetDefaultResponse, CloseRequest, - CloseResponse, CreateRequest as DocCreateRequest, CreateResponse as DocCreateResponse, - DelRequest, DelResponse, DocListRequest, DocSubscribeRequest, DocSubscribeResponse, - DropRequest, DropResponse, ExportFileRequest, ExportFileResponse, GetDownloadPolicyRequest, + AuthorGetDefaultResponse, AuthorImportResponse, AuthorListRequest, AuthorListResponse, + AuthorSetDefaultRequest, AuthorSetDefaultResponse, CloseRequest, CloseResponse, + CreateRequest as DocCreateRequest, CreateResponse as DocCreateResponse, DelRequest, + DelResponse, DocListRequest, DocSubscribeRequest, DocSubscribeResponse, DropRequest, + DropResponse, ExportFileRequest, ExportFileResponse, GetDownloadPolicyRequest, GetDownloadPolicyResponse, GetExactRequest, GetExactResponse, GetManyRequest, GetManyResponse, GetSyncPeersRequest, GetSyncPeersResponse, ImportFileRequest, ImportFileResponse, ImportRequest as DocImportRequest, ImportResponse as DocImportResponse, @@ -29,7 +29,7 @@ use super::{ }, RpcError, RpcResult, }; -use crate::{engine::Engine, Author, DocTicket, NamespaceSecret}; +use crate::{actor::ImportAuthorAction, engine::Engine, Author, DocTicket, NamespaceSecret}; /// Capacity for the flume channels to forward sync store iterators to async RPC streams. const ITER_CHANNEL_CAP: usize = 64; @@ -91,13 +91,14 @@ impl Engine { pub(super) async fn author_import( self, - req: AuthorImportRequest, + req: ImportAuthorAction, ) -> RpcResult { let author_id = self .sync - .import_author(req.author) + .import_author_action(req) .await .map_err(|e| RpcError::new(&*e))?; + Ok(AuthorImportResponse { author_id }) } diff --git a/src/rpc/proto.rs b/src/rpc/proto.rs index b1f4fcb..6f40578 100644 --- a/src/rpc/proto.rs +++ b/src/rpc/proto.rs @@ -15,7 +15,7 @@ use super::{ RpcError, RpcResult, }; use crate::{ - actor::OpenState, + actor::{ImportAuthorAction, OpenState}, engine::LiveEvent, store::{DownloadPolicy, Query}, Author, AuthorId, Capability, CapabilityKind, DocTicket, Entry, NamespaceId, PeerIdBytes, @@ -87,7 +87,7 @@ pub enum Request { #[rpc(response = RpcResult)] AuthorSetDefault(AuthorSetDefaultRequest), #[rpc(response = RpcResult)] - AuthorImport(AuthorImportRequest), + AuthorImport(ImportAuthorAction), #[rpc(response = RpcResult)] AuthorExport(AuthorExportRequest), #[rpc(response = RpcResult)] @@ -524,13 +524,6 @@ pub struct AuthorExportResponse { pub author: Option, } -/// Import author from secret key -#[derive(Serialize, Deserialize, Debug)] -pub struct AuthorImportRequest { - /// The author to import - pub author: Author, -} - /// Response to [`ImportRequest`] #[derive(Serialize, Deserialize, Debug)] pub struct AuthorImportResponse {