ydb_unofficial/
payload.rs

1
2use crate::generated::ydb::{table, discovery};
3use table::*;
4use discovery::*;
5use thiserror::Error;
6
7#[derive(Error, Debug)]
8pub enum ExtractResultError {
9    #[error("Empty body of response")]
10    Empty,
11    #[error("Cannot decode result: {0}")]
12    Decode(#[from] prost::DecodeError),
13}
14
15/// The trait to invoke payload result from response. See examples in [`crate::client`]
16pub trait YdbResponseWithResult {
17    type Result;
18    fn result(&self) -> Result<Self::Result,ExtractResultError>;
19}
20
21
22macro_rules! payloaded {
23    ($($x:ty : $p:ty,)+) => {$(
24        impl YdbResponseWithResult for $x {
25            type Result = $p;
26            fn result(&self) -> Result<Self::Result, ExtractResultError> {
27                use prost::Message;
28                use ExtractResultError::*;
29                let operation = self.operation.as_ref().ok_or(Empty)?;
30                let bytes = operation
31                    .result.as_ref().ok_or(Empty)?
32                    .value.as_slice();
33                Message::decode(bytes).map_err(|e|Decode(e))
34            }
35        }
36    )+}
37}
38
39pub(crate) use payloaded;
40
41payloaded!(
42    WhoAmIResponse: WhoAmIResult, 
43    ListEndpointsResponse: ListEndpointsResult,
44    CreateSessionResponse: CreateSessionResult,
45    ExecuteDataQueryResponse: ExecuteQueryResult,
46    BeginTransactionResponse: BeginTransactionResult,
47    CommitTransactionResponse: CommitTransactionResult,
48    PrepareDataQueryResponse: PrepareQueryResult,
49    ExplainDataQueryResponse: ExplainQueryResult,
50
51    BulkUpsertResponse: BulkUpsertResult,
52    DescribeTableOptionsResponse: DescribeTableOptionsResult,
53    DescribeTableResponse: DescribeTableResult,
54    KeepAliveResponse: KeepAliveResult,
55);