1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::borrow::Cow;

use async_trait::async_trait;
use rmpv::Value;
use serde::de::DeserializeOwned;

use super::{Stream, Transaction, TransactionBuilder};
use crate::{
    codec::{
        request::{Call, Delete, Eval, Insert, Ping, Replace, RequestBody, Select, Update, Upsert},
        utils::deserialize_non_sql_response,
    },
    errors::Error,
    IteratorType,
};

#[async_trait(?Send)]
pub trait ConnectionLike: private::Sealed {
    /// Send request, receiving raw response body.
    ///
    /// It is not recommended to use this method directly, since some requests
    /// should be only sent in specific situations and might break connection.
    async fn send_request(&self, body: impl RequestBody) -> Result<Value, Error>;

    /// Get new [`Stream`].
    ///
    /// It is safe to create `Stream` from any type, implementing current trait.
    fn stream(&self) -> Stream;

    /// Prepare [`TransactionBuilder`], which can be used to override parameters and create
    /// [`Transaction`].
    ///
    /// It is safe to create `TransactionBuilder` from any type.
    fn transaction_builder(&self) -> TransactionBuilder;

    /// Create [`Transaction`] with parameters from builder.
    ///
    /// It is safe to create `Transaction` from any type, implementing current trait.
    async fn transaction(&self) -> Result<Transaction, Error>;

    /// Send PING request ([docs](https://www.tarantool.io/en/doc/latest/dev_guide/internals/box_protocol/#iproto-ping-0x40)).
    async fn ping(&self) -> Result<(), Error> {
        self.send_request(Ping {}).await.map(drop)
    }

    // TODO: docs
    async fn eval<I, T>(&self, expr: I, args: Vec<Value>) -> Result<T, Error>
    where
        I: Into<Cow<'static, str>> + Send,
        T: DeserializeOwned,
    {
        let body = self.send_request(Eval::new(expr, args)).await?;
        deserialize_non_sql_response(body).map_err(Into::into)
    }

    // TODO: docs
    async fn call<I, T>(&self, function_name: I, args: Vec<Value>) -> Result<T, Error>
    where
        I: Into<Cow<'static, str>> + Send,
        T: DeserializeOwned,
    {
        let body = self.send_request(Call::new(function_name, args)).await?;
        deserialize_non_sql_response(body).map_err(Into::into)
    }

    // TODO: docs
    async fn select<T>(
        &self,
        space_id: u32,
        index_id: u32,
        limit: Option<u32>,
        offset: Option<u32>,
        iterator: Option<IteratorType>,
        keys: Vec<Value>,
    ) -> Result<Vec<T>, Error>
    where
        T: DeserializeOwned,
    {
        let body = self
            .send_request(Select::new(
                space_id, index_id, limit, offset, iterator, keys,
            ))
            .await?;
        deserialize_non_sql_response(body).map_err(Into::into)
    }

    // TODO: docs
    // TODO: decode response
    async fn insert(&self, space_id: u32, tuple: Vec<Value>) -> Result<(), Error> {
        let _ = self.send_request(Insert::new(space_id, tuple)).await?;
        Ok(())
    }

    // TODO: structured tuple
    // TODO: decode response
    async fn update(
        &self,
        space_id: u32,
        index_id: u32,
        index_base: Option<u32>,
        keys: Vec<Value>,
        tuple: Vec<Value>,
    ) -> Result<(), Error> {
        let _ = self
            .send_request(Update::new(space_id, index_id, index_base, keys, tuple))
            .await?;
        Ok(())
    }

    // TODO: structured tuple
    // TODO: decode response
    // TODO: maybe set index base to 1 always?
    async fn upsert(
        &self,
        space_id: u32,
        index_base: u32,
        ops: Vec<Value>,
        tuple: Vec<Value>,
    ) -> Result<(), Error> {
        let _ = self
            .send_request(Upsert::new(space_id, index_base, ops, tuple))
            .await?;
        Ok(())
    }

    // TODO: structured tuple
    // TODO: decode response
    async fn replace(&self, space_id: u32, keys: Vec<Value>) -> Result<(), Error> {
        let _ = self.send_request(Replace::new(space_id, keys)).await?;
        Ok(())
    }

    // TODO: structured tuple
    // TODO: decode response
    async fn delete(&self, space_id: u32, index_id: u32, keys: Vec<Value>) -> Result<(), Error> {
        let _ = self
            .send_request(Delete::new(space_id, index_id, keys))
            .await?;
        Ok(())
    }
}

#[async_trait(?Send)]
impl<C: ConnectionLike + private::Sealed + Sync> ConnectionLike for &C {
    async fn send_request(&self, body: impl RequestBody) -> Result<Value, Error> {
        (*self).send_request(body).await
    }

    fn stream(&self) -> Stream {
        (*self).stream()
    }

    fn transaction_builder(&self) -> TransactionBuilder {
        (*self).transaction_builder()
    }

    async fn transaction(&self) -> Result<Transaction, Error> {
        (*self).transaction().await
    }
}

mod private {
    use crate::client::{Connection, Stream, Transaction};

    #[doc(hidden)]
    pub trait Sealed {}

    impl Sealed for Connection {}
    impl Sealed for Stream {}
    impl Sealed for Transaction {}
    impl<S: Sealed> Sealed for &S {}
    impl<S: Sealed> Sealed for &mut S {}
}