reddb-io-client 1.17.0

Official Rust client for RedDB — embedded engine, gRPC, HTTP, and RedWire transports behind one connection-string API. Also hosts the workspace-internal connector + REPL used by the `red` and `red_client` binaries.
Documentation
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! RedWire client.
//!
//! Uses the canonical RedWire contracts from `reddb-wire` so the
//! client does not duplicate the engine-facing frame and payload
//! definitions. The framing is a stable wire contract defined by
//! ADR 0001 (`.red/adr/0001-redwire-tcp-protocol.md`).
//!
//! Public surface:
//!   - [`RedWireClient::connect`][]: TCP + handshake + auth
//!   - [`RedWireClient::query`][]: SQL → server result
//!   - [`RedWireClient::ping`][]: keepalive
//!   - [`RedWireClient::close`][]: clean shutdown via Bye

mod handshake;
mod io;
#[cfg(feature = "redwire")]
pub mod scram;
#[cfg(feature = "redwire-tls")]
mod tls;

pub use reddb_wire::redwire::{Flags, Frame, FrameError, MessageKind};
#[cfg(feature = "redwire-tls")]
pub use tls::TlsConfig;

use std::io as std_io;
use std::pin::Pin;

use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;

use reddb_wire::query_with_params::{
    encode_query_with_params, ParamValue as RedWireParamValue, FEATURE_PARAMS,
};

/// Boxed read+write trait object. Lets `RedWireClient` carry
/// either a plain `TcpStream` or a `tokio_rustls::TlsStream`
/// without leaking the type up.
pub(crate) type Stream = Pin<Box<dyn AsyncReadWrite + Send + Unpin>>;

pub(crate) trait AsyncReadWrite: AsyncRead + AsyncWrite {}
impl<T: AsyncRead + AsyncWrite + ?Sized> AsyncReadWrite for T {}

use crate::error::{ClientError, ErrorCode, Result};
use crate::types::{BulkInsertResult, QueryResult};

use handshake::HandshakeOutcome;
use reddb_wire::legacy::WireValue;
use reddb_wire::redwire::{
    build_bulk_insert_binary_frame, build_bulk_insert_frame, build_bye_frame, build_delete_frame,
    build_get_frame, build_ping_frame, build_query_frame, build_query_with_params_frame,
    decode_bulk_ok_count_payload, decode_bulk_ok_payload, decode_delete_ok_affected,
    decode_get_result_payload, decode_query_result_payload, decode_text_payload,
    encode_bulk_binary_payload, encode_bulk_insert_payload, encode_insert_payload,
    encode_key_payload, expect_bulk_ok_or_error, expect_delete_ok_or_error, expect_pong_reply,
    expect_result_or_error, supported_client_preface, BuildError, OperationReplyError,
};

/// Authentication credentials for the RedWire handshake.
#[derive(Clone)]
pub enum Auth {
    /// Server is configured with `auth.enabled = false`.
    Anonymous,
    /// Bearer token (login-derived session token or API key).
    Bearer(String),
    /// Username/password credentials. AuthResponse codec is follow-up work.
    Basic { user: String, pass: String },
    /// API key credentials. AuthResponse codec is follow-up work.
    ApiKey(String),
}

impl std::fmt::Debug for Auth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Anonymous => f.write_str("Anonymous"),
            Self::Bearer(_) => f.debug_tuple("Bearer").field(&"<redacted>").finish(),
            Self::Basic { .. } => f
                .debug_struct("Basic")
                .field("user", &"<redacted>")
                .field("pass", &"<redacted>")
                .finish(),
            Self::ApiKey(_) => f.debug_tuple("ApiKey").field(&"<redacted>").finish(),
        }
    }
}

/// Typed value for the binary bulk-insert fast path. Encoding delegates
/// to `reddb-wire::legacy`, which owns the byte-level value tags.
#[derive(Debug, Clone)]
pub enum BinaryValue {
    I64(i64),
    F64(f64),
    Text(String),
    Bool(bool),
    Null,
}

impl From<&BinaryValue> for WireValue {
    fn from(value: &BinaryValue) -> Self {
        match value {
            BinaryValue::I64(n) => WireValue::I64(*n),
            BinaryValue::F64(n) => WireValue::F64(*n),
            BinaryValue::Text(value) => WireValue::Text(value.clone()),
            BinaryValue::Bool(value) => WireValue::Bool(*value),
            BinaryValue::Null => WireValue::Null,
        }
    }
}

/// Configuration for `RedWireClient::connect`.
pub struct ConnectOptions {
    pub host: String,
    pub port: u16,
    pub auth: Auth,
    pub client_name: Option<String>,
    #[cfg(feature = "redwire-tls")]
    pub tls: Option<TlsConfig>,
}

impl std::fmt::Debug for ConnectOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("ConnectOptions");
        s.field("host", &self.host)
            .field("port", &self.port)
            .field("auth", &self.auth)
            .field("client_name", &self.client_name);
        #[cfg(feature = "redwire-tls")]
        s.field("tls", &self.tls.is_some());
        s.finish()
    }
}

impl Clone for ConnectOptions {
    fn clone(&self) -> Self {
        Self {
            host: self.host.clone(),
            port: self.port,
            auth: self.auth.clone(),
            client_name: self.client_name.clone(),
            #[cfg(feature = "redwire-tls")]
            tls: self.tls.clone(),
        }
    }
}

impl ConnectOptions {
    pub fn new(host: impl Into<String>, port: u16) -> Self {
        Self {
            host: host.into(),
            port,
            auth: Auth::Anonymous,
            client_name: Some(format!("reddb-rs/{}", env!("CARGO_PKG_VERSION"))),
            #[cfg(feature = "redwire-tls")]
            tls: None,
        }
    }

    pub fn with_auth(mut self, auth: Auth) -> Self {
        self.auth = auth;
        self
    }

    pub fn with_client_name(mut self, name: impl Into<String>) -> Self {
        self.client_name = Some(name.into());
        self
    }

    /// Wrap the TCP socket in TLS using the supplied config.
    #[cfg(feature = "redwire-tls")]
    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
        self.tls = Some(tls);
        self
    }
}

pub struct RedWireClient {
    stream: Stream,
    next_correlation_id: u64,
    #[allow(dead_code)]
    session_id: String,
    #[allow(dead_code)]
    server_features: u32,
}

impl std::fmt::Debug for RedWireClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedWireClient")
            .field("session_id", &self.session_id)
            .field("server_features", &self.server_features)
            .finish()
    }
}

impl RedWireClient {
    pub async fn connect(opts: ConnectOptions) -> Result<Self> {
        let addr = format!("{}:{}", opts.host, opts.port);
        let tcp = TcpStream::connect(&addr)
            .await
            .map_err(|e| ClientError::new(ErrorCode::Network, format!("{addr}: {e}")))?;

        let mut stream: Stream = match () {
            #[cfg(feature = "redwire-tls")]
            _ if opts.tls.is_some() => {
                let tls_cfg = opts.tls.as_ref().unwrap();
                let tls_stream = tls::wrap_client(tcp, &opts.host, tls_cfg).await?;
                Box::pin(tls_stream)
            }
            _ => Box::pin(tcp),
        };

        // Discriminator + minor-version byte.
        stream
            .write_all(&supported_client_preface())
            .await
            .map_err(io_err)?;

        let outcome = handshake::run(&mut stream, &opts).await?;
        match outcome {
            HandshakeOutcome::Authenticated {
                session_id,
                server_features,
            } => Ok(Self {
                stream,
                next_correlation_id: 1,
                session_id,
                server_features,
            }),
            HandshakeOutcome::Refused(reason) => Err(ClientError::new(
                ErrorCode::AuthRefused,
                format!("redwire auth refused: {reason}"),
            )),
        }
    }

    pub async fn query(&mut self, sql: &str) -> Result<QueryResult> {
        let raw = self.query_raw(sql).await?;
        let value = decode_query_result_payload(raw.as_bytes())
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("decode result: {e}")))?;
        Ok(QueryResult::from_envelope(value))
    }

    /// Send a query and return the raw `Result` payload string. This is
    /// used by the legacy `red_client` compatibility shim so the CLI
    /// output stays byte-for-byte aligned with its old RedWire path.
    pub async fn query_raw(&mut self, sql: &str) -> Result<String> {
        let corr = self.next_corr();
        let req = build_query_frame(corr, sql).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_result_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        Ok(decode_text_payload(payload))
    }

    pub async fn query_with(
        &mut self,
        sql: &str,
        params: &[crate::params::Value],
    ) -> Result<QueryResult> {
        if params.is_empty() {
            return self.query(sql).await;
        }
        if self.server_features & FEATURE_PARAMS == 0 {
            return Err(ClientError::new(
                ErrorCode::ParamsUnsupported,
                "server did not advertise RedWire parameter support",
            ));
        }

        let wire_params = params.iter().map(param_to_redwire).collect::<Vec<_>>();
        let payload = encode_query_with_params(sql, &wire_params)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("encode params: {e}")))?;
        let corr = self.next_corr();
        let req = build_query_with_params_frame(corr, payload).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_result_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        let value = decode_query_result_payload(payload)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("decode result: {e}")))?;
        Ok(QueryResult::from_envelope(value))
    }

    /// Insert a single row. `payload` is a JSON object with column
    /// → value pairs. Returns the engine's affected-rows count.
    pub async fn insert(&mut self, collection: &str, payload: serde_json::Value) -> Result<u64> {
        self.send_insert_frame(encode_insert_payload(collection, payload))
            .await
            .map(|result| result.affected)
    }

    /// Bulk insert. Each entry in `payloads` is a JSON object.
    pub async fn bulk_insert(
        &mut self,
        collection: &str,
        payloads: Vec<serde_json::Value>,
    ) -> Result<BulkInsertResult> {
        self.send_insert_frame(encode_bulk_insert_payload(collection, payloads))
            .await
    }

    async fn send_insert_frame(&mut self, bytes: Vec<u8>) -> Result<BulkInsertResult> {
        let corr = self.next_corr();
        let req = build_bulk_insert_frame(corr, bytes).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_bulk_ok_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        let payload = decode_bulk_ok_payload(payload)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("decode bulk_ok: {e}")))?;
        Ok(BulkInsertResult {
            affected: payload.affected,
            rids: payload.rids,
            ids: payload.ids,
        })
    }

    /// Fetch one row by primary id. Returns the JSON envelope the
    /// server emits on a `Get` frame: `{ ok, found, ... }`.
    pub async fn get(&mut self, collection: &str, id: &str) -> Result<serde_json::Value> {
        let corr = self.next_corr();
        let req =
            build_get_frame(corr, encode_key_payload(collection, id)).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_result_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        decode_get_result_payload(payload)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("decode get: {e}")))
    }

    /// Delete by primary id. Returns the affected count.
    pub async fn delete(&mut self, collection: &str, id: &str) -> Result<u64> {
        let corr = self.next_corr();
        let req = build_delete_frame(corr, encode_key_payload(collection, id))
            .map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_delete_ok_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        decode_delete_ok_affected(payload)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, format!("decode delete_ok: {e}")))
    }

    /// Bulk-insert via the binary fast path. Same wire shape as
    /// `MSG_BULK_INSERT_BINARY` (0x06): typed values, no JSON
    /// encode/decode. Use this for hot inserts where the column
    /// types are known up front.
    ///
    /// `columns`: column names in fixed order.
    /// `rows`: each inner Vec must have one entry per column, in
    /// column order. Mixed types are encoded by the value writer.
    pub async fn bulk_insert_binary(
        &mut self,
        collection: &str,
        columns: &[&str],
        rows: &[Vec<BinaryValue>],
    ) -> Result<u64> {
        let wire_rows = rows
            .iter()
            .map(|row| row.iter().map(WireValue::from).collect::<Vec<_>>())
            .collect::<Vec<_>>();
        let payload = encode_bulk_binary_payload(collection, columns, &wire_rows)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, e.to_string()))?;

        let corr = self.next_corr();
        let req = build_bulk_insert_binary_frame(corr, payload).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        let payload = expect_bulk_ok_or_error(resp.kind, &resp.payload).map_err(reply_err)?;
        decode_bulk_ok_count_payload(payload)
            .map_err(|e| ClientError::new(ErrorCode::Protocol, e.to_string()))
    }

    pub async fn ping(&mut self) -> Result<()> {
        let corr = self.next_corr();
        let req = build_ping_frame(corr).map_err(frame_build_err)?;
        io::write_frame(&mut self.stream, &req).await?;
        let resp = self.read_frame().await?;
        expect_pong_reply(resp.kind).map_err(reply_err)
    }

    pub async fn close(mut self) -> Result<()> {
        let corr = self.next_corr();
        let bye = build_bye_frame(corr).map_err(frame_build_err)?;
        let _ = io::write_frame(&mut self.stream, &bye).await;
        Ok(())
    }

    fn next_corr(&mut self) -> u64 {
        let c = self.next_correlation_id;
        self.next_correlation_id = self.next_correlation_id.wrapping_add(1);
        c
    }

    async fn read_frame(&mut self) -> Result<Frame> {
        io::read_frame(&mut self.stream).await
    }
}

fn param_to_redwire(value: &crate::params::Value) -> RedWireParamValue {
    match value {
        crate::params::Value::Null => RedWireParamValue::Null,
        crate::params::Value::Bool(value) => RedWireParamValue::Bool(*value),
        crate::params::Value::Int(value) => RedWireParamValue::Int(*value),
        crate::params::Value::Float(value) => RedWireParamValue::Float(*value),
        crate::params::Value::Text(value) => RedWireParamValue::Text(value.clone()),
        crate::params::Value::Bytes(value) => RedWireParamValue::Bytes(value.clone()),
        crate::params::Value::Vector(value) => RedWireParamValue::Vector(value.clone()),
        crate::params::Value::Json(value) => {
            RedWireParamValue::Json(value.to_json_string().into_bytes())
        }
        crate::params::Value::Timestamp(value) => RedWireParamValue::Timestamp(*value),
        crate::params::Value::Uuid(value) => RedWireParamValue::Uuid(*value),
    }
}

fn io_err(err: std_io::Error) -> ClientError {
    ClientError::new(ErrorCode::Network, err.to_string())
}

fn frame_build_err(err: BuildError) -> ClientError {
    ClientError::new(ErrorCode::Protocol, format!("build redwire frame: {err}"))
}

fn reply_err(err: OperationReplyError) -> ClientError {
    match err {
        OperationReplyError::Engine(message) => ClientError::new(ErrorCode::Engine, message),
        OperationReplyError::UnexpectedKind { .. } => {
            ClientError::new(ErrorCode::Protocol, err.to_string())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::param_to_redwire;
    use crate::{JsonValue, Value};
    use reddb_wire::query_with_params::ParamValue as WireValue;

    #[test]
    fn param_to_redwire_preserves_all_wire_variants() {
        let uuid = [0x11; 16];
        let cases = vec![
            (Value::Null, WireValue::Null),
            (Value::Bool(true), WireValue::Bool(true)),
            (Value::Int64(42), WireValue::Int(42)),
            (Value::Float(1.5), WireValue::Float(1.5)),
            (Value::Text("Ada".into()), WireValue::Text("Ada".into())),
            (
                Value::Bytes(vec![0xde, 0xad]),
                WireValue::Bytes(vec![0xde, 0xad]),
            ),
            (
                Value::Vector(vec![0.25, 0.5]),
                WireValue::Vector(vec![0.25, 0.5]),
            ),
            (
                Value::Json(JsonValue::object([("role", JsonValue::string("admin"))])),
                WireValue::Json(br#"{"role":"admin"}"#.to_vec()),
            ),
            (
                Value::Timestamp(1_700_000_000),
                WireValue::Timestamp(1_700_000_000),
            ),
            (Value::Uuid(uuid), WireValue::Uuid(uuid)),
        ];

        for (input, expected) in cases {
            assert_eq!(param_to_redwire(&input), expected);
        }
    }
}