karo_common_rpc/
request.rs1use bson::Bson;
2use serde::Serialize;
3use tokio::net::UnixStream;
4
5use super::writer::RpcWriter;
6
7pub enum Body {
9 Call(Bson),
11 Subscription,
13 Fd(String, UnixStream),
15}
16
17pub struct RpcRequest {
19 message_id: i64,
21 writer: RpcWriter,
23 endpoint: String,
25 body: Option<Body>,
27}
28
29impl RpcRequest {
30 pub(crate) fn new(message_id: i64, writer: RpcWriter, endpoint: String, body: Body) -> Self {
31 Self {
32 message_id,
33 writer,
34 endpoint,
35 body: Some(body),
36 }
37 }
38
39 pub fn writer(&self) -> &RpcWriter {
40 &self.writer
41 }
42
43 pub fn take_body(&mut self) -> Option<Body> {
46 self.body.take()
47 }
48
49 pub fn endpoint(&self) -> &String {
51 &self.endpoint
52 }
53
54 pub async fn respond<T: Serialize>(&self, data: Result<T, crate::Error>) -> bool {
56 self.writer.respond(self.message_id, data).await
57 }
58
59 pub async fn respond_with_fd<T: Serialize>(
61 &self,
62 data: Result<T, crate::Error>,
63 stream: UnixStream,
64 ) -> bool {
65 self.writer
66 .respond_with_fd(self.message_id, data, stream)
67 .await
68 }
69}