openkache 0.1.3

Rust client for the OpenKache cache server
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#![doc = include_str!("../README.md")]
#![doc(html_root_url = "https://docs.rs/openkache/0.1.3")]

#[path = "internal/core/lib.rs"]
#[allow(
    dead_code,
    rustdoc::private_intra_doc_links,
    unexpected_cfgs,
    unused_imports
)]
mod internal_core;
#[path = "internal/protocol/lib.rs"]
#[allow(dead_code, unexpected_cfgs, unused_imports)]
mod internal_protocol;
#[path = "internal/value/lib.rs"]
#[allow(dead_code, unexpected_cfgs, unused_imports)]
mod internal_value;

mod native;

mod maintained {
    use std::fmt;

    use super::internal_core as core;
    use super::native::{ValueCodec as ValueCodecTrait, from_value, to_value};
    use core::value::{Compression, Encryption};
    use core::{GetOutcome as CoreGetOutcome, Operation, SetOutcome as CoreSetOutcome};
    use serde::Serialize;
    use serde::de::DeserializeOwned;

    pub use super::internal_value::{
        Error as ValueError, Float, FloatWidth, Integer, Limits as ValueLimits, Sign, Value,
        ValueKind,
    };
    pub use super::native::{FunctionCodec, SerdeCodec, ValueCodec};
    pub use core::KeyError;
    pub use core::TypedKey;

    /// Result of an unconditional `set`.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum SetOutcome {
        /// The key was absent and a new item was created.
        Created,
        /// An existing item was replaced.
        Replaced,
    }

    /// A mutation whose response was lost after admission.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum Mutation {
        /// The unknown operation was `set`.
        Set,
        /// The unknown operation was `delete`.
        Delete,
    }

    /// Errors returned by the client.
    #[derive(Debug, thiserror::Error)]
    pub enum Error {
        /// A mutation may have taken effect, but the response could not be
        /// confirmed. The client never retries this operation automatically.
        #[error("{operation:?} result is unknown after request admission")]
        UnknownMutation {
            /// The mutation whose result is unknown.
            operation: Mutation,
        },
        /// A connection, protocol, key, or value failure from the internal
        /// implementation.
        #[error("client operation failed: {0}")]
        Core(String),
        /// The server returned a conditional-set status even though this
        /// client uses unconditional writes.
        #[error("server returned an unsupported set outcome")]
        UnsupportedSetOutcome,
        /// Serde serialization failed before the request was admitted.
        #[error("serde serialization failed: {0}")]
        SerdeSerialize(String),
        /// Serde deserialization failed after the value was retrieved.
        #[error("serde deserialization failed: {0}")]
        SerdeDeserialize(String),
        /// A custom [`ValueCodec`] failed before a write was admitted.
        #[error("value codec encoding failed: {0}")]
        CodecEncode(String),
        /// A custom [`ValueCodec`] failed after a value was retrieved.
        #[error("value codec decoding failed: {0}")]
        CodecDecode(String),
    }

    /// Stable high-level category for a client error.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum ErrorKind {
        /// A mutation may have taken effect, but its result is unknown.
        UnknownMutation,
        /// A connection, protocol, key, or value operation failed.
        Core,
        /// The server returned an unsupported set outcome.
        UnsupportedSetOutcome,
        /// Serde serialization failed before a request was admitted.
        SerdeSerialize,
        /// Serde deserialization failed after a value was retrieved.
        SerdeDeserialize,
        /// A value codec failed before a write was admitted.
        CodecEncode,
        /// A value codec failed after a value was retrieved.
        CodecDecode,
    }

    impl Error {
        /// Returns the stable category for this error.
        pub const fn kind(&self) -> ErrorKind {
            match self {
                Self::UnknownMutation { .. } => ErrorKind::UnknownMutation,
                Self::Core(_) => ErrorKind::Core,
                Self::UnsupportedSetOutcome => ErrorKind::UnsupportedSetOutcome,
                Self::SerdeSerialize(_) => ErrorKind::SerdeSerialize,
                Self::SerdeDeserialize(_) => ErrorKind::SerdeDeserialize,
                Self::CodecEncode(_) => ErrorKind::CodecEncode,
                Self::CodecDecode(_) => ErrorKind::CodecDecode,
            }
        }

        /// Returns whether this error reports an unknown mutation outcome.
        pub const fn is_unknown_mutation(&self) -> bool {
            matches!(self, Self::UnknownMutation { .. })
        }

        /// Returns the mutation whose outcome is unknown, if any.
        pub const fn mutation(&self) -> Option<Mutation> {
            match self {
                Self::UnknownMutation { operation } => Some(*operation),
                _ => None,
            }
        }
    }

    /// Result alias for client operations.
    pub type Result<T> = std::result::Result<T, Error>;

    impl SetOutcome {
        /// Returns whether this outcome created a new item.
        pub const fn is_created(self) -> bool {
            matches!(self, Self::Created)
        }

        /// Returns whether this outcome replaced an existing item.
        pub const fn is_replaced(self) -> bool {
            matches!(self, Self::Replaced)
        }
    }

    impl fmt::Display for Mutation {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str(match self {
                Self::Set => "set",
                Self::Delete => "delete",
            })
        }
    }

    fn map_core_error(error: core::Error) -> Error {
        match error {
            core::Error::AmbiguousOutcome { operation, .. } => {
                let operation = match operation {
                    Operation::Delete => Mutation::Delete,
                    _ => Mutation::Set,
                };
                Error::UnknownMutation { operation }
            }
            other => Error::Core(other.to_string()),
        }
    }

    use core::ProtectedClient;

    /// A connected OpenKache client.
    ///
    /// Values use the OpenKache structured value format. Connections use the
    /// local development TLS profile described in [`Client::connect`].
    #[cfg(feature = "quic-quinn")]
    #[derive(Clone)]
    pub struct Client {
        inner: ProtectedClient,
    }

    #[cfg(feature = "quic-quinn")]
    impl Client {
        async fn gate0_client(&self) -> Result<&ProtectedClient> {
            let namespace_id = self
                .inner
                .raw()
                .ensure_namespace_id()
                .await
                .map_err(map_core_error)?;
            if namespace_id != core::contract::GATE0_NAMESPACE_ID {
                return Err(Error::Core(format!(
                    "server selected namespace {namespace_id}, expected Gate 0 namespace {}",
                    core::contract::GATE0_NAMESPACE_ID,
                )));
            }
            Ok(&self.inner)
        }

        /// Connects to an OpenKache server using the local development profile.
        ///
        /// The profile intentionally disables certificate verification for
        /// local development. It still requires a TLS 1.3 handshake and never
        /// falls back to plaintext. Do not use this trust profile in production.
        ///
        /// # Arguments
        ///
        /// * `endpoint` - A `host:port` endpoint. IPv6 endpoints use
        ///   `[host]:port`.
        ///
        /// # Returns
        ///
        /// A connected [`Client`].
        ///
        /// # Errors
        ///
        /// Returns [`Error::Core`] when parsing the endpoint, connecting, or
        /// completing the TLS handshake fails.
        pub async fn connect(endpoint: impl AsRef<str>) -> Result<Self> {
            let endpoint = endpoint.as_ref().parse().map_err(map_core_error)?;
            let inner = ProtectedClient::builder(
                endpoint,
                core::DataProtectionKey::from_bytes(core::contract::GATE0_ITEM_ID_ROOT),
            )
            .server_trust(core::ServerTrust::Insecure)
            .compression(Compression::Disabled)
            .encryption(Encryption::Unprotected)
            .connect()
            .await
            .map_err(map_core_error)?;
            Ok(Self { inner })
        }

        /// Retrieves one structured value.
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        ///
        /// # Returns
        ///
        /// `Ok(Some(value))` when the item exists, or `Ok(None)` when it does
        /// not. A stored `Value::Null` is still `Some(Value::Null)`.
        ///
        /// # Errors
        ///
        /// Returns [`Error`] when the connection, protocol, key, or value
        /// operation fails.
        pub async fn get(&self, key: impl Into<TypedKey>) -> Result<Option<Value>> {
            self.gate0_client()
                .await?
                .get_structured(key)
                .await
                .map(|outcome| match outcome {
                    CoreGetOutcome::Found(value) => Some(value),
                    CoreGetOutcome::NotFound => None,
                })
                .map_err(map_core_error)
        }

        /// Stores one structured value using an unconditional write.
        ///
        /// Native Rust strings, byte vectors, booleans, integers, and floats
        /// convert directly to [`Value`]. Use an explicit [`Value`] variant
        /// when the exact model representation matters.
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        /// * `value` - A native value or [`Value`].
        ///
        /// # Returns
        ///
        /// [`SetOutcome::Created`] for a new item or
        /// [`SetOutcome::Replaced`] when an existing item is overwritten.
        ///
        /// # Errors
        ///
        /// Returns [`Error`] when the connection, protocol, value, or storage
        /// operation fails.
        pub async fn set(
            &self,
            key: impl Into<TypedKey>,
            value: impl Into<Value>,
        ) -> Result<SetOutcome> {
            let value = value.into();
            self.gate0_client()
                .await?
                .set_structured(key, value, core::SetOptions::new())
                .await
                .map_err(map_core_error)
                .and_then(|outcome| match outcome {
                    CoreSetOutcome::Created => Ok(SetOutcome::Created),
                    CoreSetOutcome::Replaced => Ok(SetOutcome::Replaced),
                    CoreSetOutcome::NotStored => Err(Error::UnsupportedSetOutcome),
                })
        }

        /// Retrieves one value and decodes it into a Serde type.
        ///
        /// Deserialization is performed against the same lossless structured
        /// value model as [`Client::set`]. A missing key returns `None`. A
        /// stored null decodes as `Some(None)` when `T` is an `Option<U>`.
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        ///
        /// # Returns
        ///
        /// `Ok(Some(value))` for a value that decodes into `T`, or `Ok(None)`
        /// when the key does not exist.
        ///
        /// # Errors
        ///
        /// Returns [`Error::SerdeDeserialize`] when the stored value cannot
        /// be represented by `T`, and [`Error::Core`] for transport,
        /// protocol, key, or value failures.
        pub async fn get_serde<T: DeserializeOwned>(
            &self,
            key: impl Into<TypedKey>,
        ) -> Result<Option<T>> {
            match self.get(key).await? {
                None => Ok(None),
                Some(value) => from_value(value)
                    .map(Some)
                    .map_err(|error| Error::SerdeDeserialize(error.to_string())),
            }
        }

        /// Serializes a Serde value and stores it with an unconditional
        /// write.
        ///
        /// Serde serialization completes before the request is admitted, so
        /// a serialization error cannot produce [`Error::UnknownMutation`].
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        /// * `value` - The value to serialize.
        ///
        /// # Returns
        ///
        /// [`SetOutcome::Created`] for a new item or
        /// [`SetOutcome::Replaced`] when an existing item is overwritten.
        ///
        /// # Errors
        ///
        /// Returns [`Error::SerdeSerialize`] when `value` cannot be represented
        /// by the structured value model. Transport and protocol failures use
        /// the same errors as [`Client::set`].
        pub async fn set_serde<T: Serialize>(
            &self,
            key: impl Into<TypedKey>,
            value: T,
        ) -> Result<SetOutcome> {
            let value =
                to_value(&value).map_err(|error| Error::SerdeSerialize(error.to_string()))?;
            self.set(key, value).await
        }

        /// Retrieves one value and decodes it with an application codec.
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        /// * `codec` - The application codec used to decode the stored
        ///   structured value.
        ///
        /// # Returns
        ///
        /// `Ok(Some(value))` when the item exists and decodes successfully, or
        /// `Ok(None)` when it does not.
        ///
        /// # Errors
        ///
        /// Returns [`Error::CodecDecode`] when `codec` rejects the stored
        /// value, or the same transport and protocol errors as [`Client::get`].
        pub async fn get_with<T, C>(&self, key: impl Into<TypedKey>, codec: &C) -> Result<Option<T>>
        where
            C: ValueCodecTrait<T>,
        {
            match self.get(key).await? {
                None => Ok(None),
                Some(value) => codec
                    .decode(value)
                    .map(Some)
                    .map_err(|error| Error::CodecDecode(error.to_string())),
            }
        }

        /// Encodes a native value with an application codec and stores it with
        /// an unconditional write.
        ///
        /// Encoding completes before request admission, so a codec failure
        /// cannot produce [`Error::UnknownMutation`].
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        /// * `value` - The value to encode.
        /// * `codec` - The application codec used to encode `value`.
        ///
        /// # Returns
        ///
        /// [`SetOutcome::Created`] for a new item or
        /// [`SetOutcome::Replaced`] when an existing item is overwritten.
        ///
        /// # Errors
        ///
        /// Returns [`Error::CodecEncode`] when `codec` rejects `value`, or the
        /// same transport and mutation errors as [`Client::set`].
        pub async fn set_with<T, C>(
            &self,
            key: impl Into<TypedKey>,
            value: &T,
            codec: &C,
        ) -> Result<SetOutcome>
        where
            C: ValueCodecTrait<T>,
        {
            let value = codec
                .encode(value)
                .map_err(|error| Error::CodecEncode(error.to_string()))?;
            self.set(key, value).await
        }

        /// Deletes one key and reports whether an item was removed.
        ///
        /// # Arguments
        ///
        /// * `key` - A text, byte, or signed integer key convertible to
        ///   [`TypedKey`].
        ///
        /// # Returns
        ///
        /// `Ok(true)` when an item was removed, or `Ok(false)` when the key did
        /// not exist.
        ///
        /// # Errors
        ///
        /// Returns [`Error`] when the connection, protocol, key, or mutation
        /// operation fails.
        pub async fn delete(&self, key: impl Into<TypedKey>) -> Result<bool> {
            self.gate0_client()
                .await?
                .delete(key)
                .await
                .map_err(map_core_error)
                .map(|outcome| match outcome {
                    core::DeleteOutcome::Deleted => true,
                    core::DeleteOutcome::NotFound => false,
                })
        }

        /// Idempotently closes the client and waits for admitted work to
        /// settle before releasing the transport.
        ///
        /// # Returns
        ///
        /// `Ok(())` after the connection is closed.
        ///
        /// # Errors
        ///
        /// Returns [`Error::Core`] when closing the connection fails.
        pub async fn close(&self) -> Result<()> {
            self.inner.close().await.map_err(map_core_error)
        }
    }
}

pub use maintained::*;