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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! Stable core configuration types independent of the TLS and QUIC implementations.

use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use std::time::Duration;

use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};

use crate::internal_core::protocol::{EvictionMode, ExpirationMode, SetCondition, SetWireOptions};
use crate::internal_core::{Error, Result};

/// The sole key-exchange group accepted by the OpenKache transport profile.
pub(crate) const PQ_GROUP: rustls::NamedGroup = rustls::NamedGroup::X25519MLKEM768;

/// Builds the provider used by every client backend.
///
/// Rustls' normal provider keeps classical groups available for compatibility.
/// The OpenKache profile intentionally narrows the list to the approved hybrid
/// group so no backend can silently fall back to classical-only TLS.
pub(crate) fn strict_pq_provider() -> std::sync::Arc<rustls::crypto::CryptoProvider> {
    let mut provider = rustls::crypto::aws_lc_rs::default_provider();
    provider.kx_groups.retain(|group| group.name() == PQ_GROUP);
    debug_assert_eq!(provider.kx_groups.len(), 1);
    provider.into()
}

/// Network destination and TLS identity of one OpenKache server.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Endpoint {
    host: String,
    port: u16,
    address: Option<SocketAddr>,
    server_name: String,
}

impl Endpoint {
    /// Creates an endpoint whose hostname is used for both DNS and TLS verification.
    ///
    /// # Arguments
    ///
    /// * `host` - DNS hostname or IP address without a port.
    /// * `port` - Server transport port.
    ///
    /// # Returns
    ///
    /// A validated endpoint.
    ///
    /// # Errors
    ///
    /// Returns an error when `host` is not a valid TLS DNS name or IP address, or `port` is zero.
    pub fn new(host: impl Into<String>, port: u16) -> Result<Self> {
        let host = host.into();
        validate_server_name("endpoint.host", &host)?;
        if port == 0 {
            return Err(Error::configuration(
                "endpoint.port",
                "must be greater than zero",
            ));
        }
        let address = host
            .parse::<IpAddr>()
            .ok()
            .map(|address| SocketAddr::new(address, port));
        Ok(Self {
            server_name: host.clone(),
            host,
            port,
            address,
        })
    }

    /// Creates an endpoint with a resolved address and independent TLS server name.
    ///
    /// # Arguments
    ///
    /// * `address` - Resolved transport destination.
    /// * `server_name` - DNS name or IP address expected in the server certificate.
    ///
    /// # Returns
    ///
    /// A validated endpoint that performs no DNS lookup.
    ///
    /// # Errors
    ///
    /// Returns an error when the address port is zero or `server_name` is not a valid TLS DNS name
    /// or IP address.
    pub fn from_socket_addr(address: SocketAddr, server_name: impl Into<String>) -> Result<Self> {
        let server_name = server_name.into();
        if address.port() == 0 {
            return Err(Error::configuration(
                "endpoint.port",
                "must be greater than zero",
            ));
        }
        validate_server_name("endpoint.server_name", &server_name)?;
        Ok(Self {
            host: address.ip().to_string(),
            port: address.port(),
            address: Some(address),
            server_name,
        })
    }

    /// Replaces the TLS server name while preserving this endpoint's network destination.
    ///
    /// Native adapters receive a destination and a separate certificate identity. Keeping this
    /// operation on the shared endpoint type avoids duplicating validation in each binding.
    #[cfg(feature = "ffi")]
    pub(crate) fn with_server_name(mut self, server_name: impl Into<String>) -> Result<Self> {
        let server_name = server_name.into();
        validate_server_name("endpoint.server_name", &server_name)?;
        self.server_name = server_name;
        Ok(self)
    }

    pub(crate) fn host(&self) -> &str {
        &self.host
    }

    pub(crate) const fn port(&self) -> u16 {
        self.port
    }

    pub(crate) const fn resolved_address(&self) -> Option<SocketAddr> {
        self.address
    }

    pub(crate) fn server_name(&self) -> &str {
        &self.server_name
    }
}

fn validate_server_name(field: &'static str, value: &str) -> Result<()> {
    rustls::pki_types::ServerName::try_from(value.to_owned())
        .map(|_| ())
        .map_err(|error| Error::configuration(field, error.to_string()))
}

impl FromStr for Endpoint {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        let authority = value
            .parse::<http::uri::Authority>()
            .map_err(|error| Error::configuration("endpoint", error.to_string()))?;
        if authority.as_str().contains('@') {
            return Err(Error::configuration(
                "endpoint",
                "must not contain user information",
            ));
        }
        let port = authority
            .port_u16()
            .ok_or_else(|| Error::configuration("endpoint.port", "is required"))?;
        let host = authority
            .host()
            .strip_prefix('[')
            .and_then(|host| host.strip_suffix(']'))
            .unwrap_or_else(|| authority.host());
        Self::new(host, port)
    }
}

/// X.509 certificate accepted by the stable client API.
#[derive(Clone, Debug)]
pub struct Certificate(CertificateDer<'static>);

impl Certificate {
    /// Wraps one non-empty DER-encoded X.509 certificate.
    ///
    /// # Arguments
    ///
    /// * `bytes` - Complete DER certificate bytes.
    ///
    /// # Returns
    ///
    /// Owned certificate bytes. Certificate validation occurs when the TLS
    /// configuration is built.
    ///
    /// # Errors
    ///
    /// Returns an error when the input is empty.
    pub fn from_der(bytes: impl Into<Vec<u8>>) -> Result<Self> {
        let bytes = bytes.into();
        if bytes.is_empty() {
            return Err(Error::configuration(
                "certificate",
                "DER input must not be empty",
            ));
        }
        Ok(Self(CertificateDer::from(bytes)))
    }

    /// Parses exactly one PEM-encoded X.509 certificate.
    ///
    /// # Arguments
    ///
    /// * `bytes` - PEM certificate bytes.
    ///
    /// # Returns
    ///
    /// An owned certificate.
    ///
    /// # Errors
    ///
    /// Returns an error when PEM decoding fails or contains other than one certificate.
    pub fn from_pem(bytes: &[u8]) -> Result<Self> {
        let certificates = decode_pem_certificates(pem_bytes(bytes).ok_or_else(|| {
            Error::configuration(
                "certificate",
                "PEM input must start with a certificate block",
            )
        })?)?;
        let [certificate] =
            <[_; 1]>::try_from(certificates).map_err(|certificates: Vec<Self>| {
                Error::configuration(
                    "certificate",
                    format!(
                        "PEM input must contain exactly one certificate, got {}",
                        certificates.len()
                    ),
                )
            })?;
        Ok(certificate)
    }

    /// Wraps one DER certificate or parses a non-empty PEM certificate chain.
    ///
    /// # Arguments
    ///
    /// * `bytes` - One DER certificate or one or more PEM certificates.
    ///
    /// # Returns
    ///
    /// A certificate chain in input order. DER certificate validation occurs
    /// when the TLS configuration is built.
    ///
    /// # Errors
    ///
    /// Returns an error when the input is empty, PEM decoding fails, or a PEM input contains no
    /// certificates.
    pub fn from_der_or_pem_chain(bytes: &[u8]) -> Result<Vec<Self>> {
        if bytes.is_empty() {
            return Err(Error::configuration(
                "certificate",
                "input must not be empty",
            ));
        }
        if bytes.iter().all(u8::is_ascii_whitespace) {
            return Err(Error::configuration(
                "certificate",
                "input must contain DER bytes or a PEM certificate block",
            ));
        }
        if let Some(pem) = pem_bytes(bytes) {
            decode_pem_certificates(pem)
        } else {
            Self::from_der(bytes.to_vec()).map(|certificate| vec![certificate])
        }
    }

    pub(crate) fn into_der(self) -> CertificateDer<'static> {
        self.0
    }
}

fn decode_pem_certificates(bytes: &[u8]) -> Result<Vec<Certificate>> {
    let certificates = CertificateDer::pem_slice_iter(bytes)
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|error| Error::configuration("certificate", error.to_string()))?
        .into_iter()
        .map(Certificate)
        .collect::<Vec<_>>();
    if certificates.is_empty() {
        return Err(Error::configuration(
            "certificate",
            "PEM input contains no certificates",
        ));
    }
    Ok(certificates)
}

fn pem_bytes(bytes: &[u8]) -> Option<&[u8]> {
    let start = bytes.iter().position(|byte| !byte.is_ascii_whitespace())?;
    let bytes = &bytes[start..];
    bytes.starts_with(b"-----BEGIN ").then_some(bytes)
}

/// Private key accepted by the stable client API.
#[derive(Debug)]
pub struct PrivateKey(PrivateKeyDer<'static>);

impl PrivateKey {
    /// Parses one DER-encoded private key.
    ///
    /// # Arguments
    ///
    /// * `bytes` - DER PKCS#1, PKCS#8, or SEC1 private-key bytes.
    ///
    /// # Returns
    ///
    /// An owned private key.
    ///
    /// # Errors
    ///
    /// Returns an error when the DER key format is unsupported.
    pub fn from_der(bytes: impl Into<Vec<u8>>) -> Result<Self> {
        PrivateKeyDer::try_from(bytes.into())
            .map(Self)
            .map_err(|_| Error::configuration("private_key", "unsupported DER key format"))
    }

    /// Parses one PEM-encoded private key.
    ///
    /// # Arguments
    ///
    /// * `bytes` - PEM private-key bytes.
    ///
    /// # Returns
    ///
    /// An owned private key.
    ///
    /// # Errors
    ///
    /// Returns an error when PEM decoding fails or the key format is unsupported.
    pub fn from_pem(bytes: &[u8]) -> Result<Self> {
        let bytes = pem_bytes(bytes).ok_or_else(|| {
            Error::configuration("private_key", "PEM input must start with a key block")
        })?;
        PrivateKeyDer::from_pem_slice(bytes)
            .map(Self)
            .map_err(|error| Error::configuration("private_key", error.to_string()))
    }

    /// Parses one DER- or PEM-encoded private key.
    ///
    /// # Arguments
    ///
    /// * `bytes` - DER or PEM PKCS#1, PKCS#8, or SEC1 private-key bytes.
    ///
    /// # Returns
    ///
    /// An owned private key.
    ///
    /// # Errors
    ///
    /// Returns an error when the input is empty, PEM decoding fails, or the key format is
    /// unsupported.
    pub fn from_der_or_pem(bytes: &[u8]) -> Result<Self> {
        if bytes.is_empty() {
            return Err(Error::configuration(
                "private_key",
                "input must not be empty",
            ));
        }
        if bytes.iter().all(u8::is_ascii_whitespace) {
            return Err(Error::configuration(
                "private_key",
                "input must contain DER bytes or a PEM key block",
            ));
        }
        if pem_bytes(bytes).is_some() {
            Self::from_pem(bytes)
        } else {
            Self::from_der(bytes.to_vec())
        }
    }

    pub(crate) fn into_der(self) -> PrivateKeyDer<'static> {
        self.0
    }
}

/// Certificate chain and private key presented during mutual TLS authentication.
#[derive(Debug)]
pub struct ClientIdentity {
    certificate_chain: Vec<Certificate>,
    private_key: PrivateKey,
}

impl ClientIdentity {
    /// Creates a mutual TLS identity.
    ///
    /// # Arguments
    ///
    /// * `certificate_chain` - Leaf certificate followed by intermediate certificates.
    /// * `private_key` - Private key corresponding to the leaf certificate.
    ///
    /// # Returns
    ///
    /// An owned client identity. Remaining certificate and private-key
    /// validation occurs while building TLS configuration or during the
    /// handshake.
    ///
    /// # Errors
    ///
    /// Returns an error when the certificate chain is empty.
    pub fn new(certificate_chain: Vec<Certificate>, private_key: PrivateKey) -> Result<Self> {
        if certificate_chain.is_empty() {
            return Err(Error::configuration(
                "client_identity.certificate_chain",
                "must not be empty",
            ));
        }
        Ok(Self {
            certificate_chain,
            private_key,
        })
    }

    pub(crate) fn into_rustls(self) -> (Vec<CertificateDer<'static>>, PrivateKeyDer<'static>) {
        (
            self.certificate_chain
                .into_iter()
                .map(Certificate::into_der)
                .collect(),
            self.private_key.into_der(),
        )
    }
}

/// Trust roots used to authenticate the server.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub enum ServerTrust {
    /// Load the operating system's native root certificates.
    #[default]
    System,
    /// Trust only the supplied certificates, including an explicitly trusted self-signed leaf.
    Custom(Vec<Certificate>),
    /// Encrypt the connection without authenticating the server certificate or identity.
    ///
    /// This is an explicit insecure opt-out intended for local development and
    /// controlled test environments. It must never be selected implicitly.
    Insecure,
}

/// ALPN identifier offered during QUIC/TLS negotiation.
///
/// The current wire implementation supports only `openkache/1`. The
/// constructor retains the versioned shape of this API for compatibility, but
/// rejects every other protocol version before a connection is attempted.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AlpnPolicy {
    protocols: Vec<Vec<u8>>,
    minimum_version: u32,
}

impl AlpnPolicy {
    /// Creates an ALPN policy for the supported wire protocol.
    ///
    /// # Arguments
    ///
    /// * `protocols` - A single `openkache/1` ALPN name.
    /// * `minimum_version` - Must be `1`, the only protocol version currently
    ///   implemented by this client.
    ///
    /// # Returns
    ///
    /// A validated policy retained by the client builder.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Configuration`] when an identifier is malformed, more
    /// than one protocol is supplied, the minimum is zero, or an unsupported
    /// protocol version is requested.
    pub fn new(protocols: Vec<Vec<u8>>, minimum_version: u32) -> Result<Self> {
        if protocols.is_empty() {
            return Err(Error::configuration(
                "alpn.protocols",
                "must contain at least one protocol",
            ));
        }
        if minimum_version == 0 {
            return Err(Error::configuration(
                "alpn.minimum_version",
                "must be greater than zero",
            ));
        }
        if minimum_version != 1 {
            return Err(Error::configuration(
                "alpn.minimum_version",
                "only protocol version 1 is supported",
            ));
        }

        if protocols.len() != 1 {
            return Err(Error::configuration(
                "alpn.protocols",
                "only openkache/1 is supported",
            ));
        }
        let protocol = &protocols[0];
        parse_alpn_version(protocol).map_err(|message| {
            Error::configuration("alpn.protocols", format!("entry 0 is invalid: {message}"))
        })?;
        if protocol.as_slice() != crate::internal_protocol::ALPN {
            return Err(Error::configuration(
                "alpn.protocols",
                format!(
                    "protocol {} is unsupported; only openkache/1 is supported",
                    String::from_utf8_lossy(protocol)
                ),
            ));
        }
        Ok(Self {
            protocols,
            minimum_version,
        })
    }

    /// Creates a policy from positive protocol version numbers.
    ///
    /// # Arguments
    ///
    /// * `versions` - A single-element list containing version `1`.
    /// * `minimum_version` - Must be `1`.
    ///
    /// # Returns
    ///
    /// A validated policy with canonical `openkache/<version>` identifiers.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Configuration`] when the version list violates the
    /// same constraints as [`Self::new`].
    pub fn from_versions(versions: Vec<u32>, minimum_version: u32) -> Result<Self> {
        let protocols = versions
            .into_iter()
            .map(|version| format!("openkache/{version}").into_bytes())
            .collect();
        Self::new(protocols, minimum_version)
    }

    /// Returns the offered ALPN identifier.
    pub fn protocols(&self) -> &[Vec<u8>] {
        &self.protocols
    }

    /// Returns the minimum negotiated protocol version accepted by the client.
    pub const fn minimum_version(&self) -> u32 {
        self.minimum_version
    }

    pub(crate) fn validate_negotiated(&self, protocol: &[u8]) -> Result<()> {
        if !self
            .protocols
            .iter()
            .any(|offered| offered.as_slice() == protocol)
        {
            return Err(Error::Connection(
                "server negotiated an ALPN protocol that was not offered".into(),
            ));
        }
        let version = parse_alpn_version(protocol).map_err(|message| {
            Error::Connection(format!(
                "server negotiated an invalid ALPN protocol: {message}"
            ))
        })?;
        if version < self.minimum_version {
            return Err(Error::Connection(format!(
                "server negotiated ALPN version {version}, below client minimum {}",
                self.minimum_version
            )));
        }
        if protocol != crate::internal_protocol::ALPN {
            return Err(Error::Connection(format!(
                "client does not implement negotiated ALPN {}",
                String::from_utf8_lossy(protocol)
            )));
        }
        Ok(())
    }
}

impl Default for AlpnPolicy {
    fn default() -> Self {
        let minimum_version = parse_alpn_version(crate::internal_protocol::ALPN)
            .expect("the generated OpenKache ALPN must contain a positive version");
        Self {
            protocols: vec![crate::internal_protocol::ALPN.to_vec()],
            minimum_version,
        }
    }
}

fn parse_alpn_version(protocol: &[u8]) -> std::result::Result<u32, &'static str> {
    const PREFIX: &[u8] = b"openkache/";
    let suffix = protocol
        .strip_prefix(PREFIX)
        .ok_or("must start with openkache/")?;
    if suffix.is_empty() {
        return Err("version is missing");
    }
    if suffix.len() > 1 && suffix[0] == b'0' {
        return Err("version must not contain leading zeroes");
    }
    if !suffix.iter().all(u8::is_ascii_digit) {
        return Err("version must contain only decimal digits");
    }
    let version = std::str::from_utf8(suffix)
        .ok()
        .and_then(|value| value.parse::<u32>().ok())
        .ok_or("version is outside the positive u32 range")?;
    (version > 0)
        .then_some(version)
        .ok_or("version must be greater than zero")
}

/// Deadlines applied to connection setup and complete request exchanges.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ClientTimeouts {
    /// DNS resolution, endpoint initialization, and QUIC/TLS handshake deadline.
    pub connect: Duration,
    /// Lane acquisition, request transmission, and response receipt deadline.
    pub request: Duration,
}

impl Default for ClientTimeouts {
    fn default() -> Self {
        Self {
            connect: Duration::from_millis(
                crate::internal_core::contract::DEFAULT_CONNECT_TIMEOUT_MILLISECONDS,
            ),
            request: Duration::from_millis(
                crate::internal_core::contract::DEFAULT_REQUEST_TIMEOUT_MILLISECONDS,
            ),
        }
    }
}

/// Retry policy for operations whose responses can be requested again safely.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RetryPolicy {
    /// Maximum total attempts, including the initial request.
    pub max_attempts: usize,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: crate::internal_core::contract::DEFAULT_RETRY_MAX_ATTEMPTS,
        }
    }
}

/// Optional behavior for one set operation.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SetOptions {
    condition: SetCondition,
    expiration_mode: ExpirationMode,
    time_to_live_ms: Option<u64>,
    eviction_mode: EvictionMode,
}

impl SetOptions {
    /// Creates unconditional set behavior inheriting namespace expiration and eviction defaults.
    pub const fn new() -> Self {
        Self {
            condition: SetCondition::Any,
            expiration_mode: ExpirationMode::Inherit,
            time_to_live_ms: None,
            eviction_mode: EvictionMode::Inherit,
        }
    }

    /// Stores only when the key does not exist.
    pub const fn if_absent(mut self) -> Self {
        self.condition = SetCondition::IfAbsent;
        self
    }

    /// Stores only when the key already exists.
    pub const fn if_present(mut self) -> Self {
        self.condition = SetCondition::IfPresent;
        self
    }

    /// Applies a relative expiration in milliseconds.
    pub const fn expires_after_millis(mut self, milliseconds: u64) -> Self {
        self.expiration_mode = ExpirationMode::ExplicitTtl;
        self.time_to_live_ms = Some(milliseconds);
        self
    }

    /// Stores the item without a TTL-based expiration.
    pub const fn no_expiry(mut self) -> Self {
        self.expiration_mode = ExpirationMode::NoExpiry;
        self.time_to_live_ms = None;
        self
    }

    /// Resolves expiration from the selected namespace's current default.
    pub const fn inherit_expiration(mut self) -> Self {
        self.expiration_mode = ExpirationMode::Inherit;
        self.time_to_live_ms = None;
        self
    }

    /// Allows the namespace's capacity eviction algorithm to select this item.
    pub const fn evictable(mut self) -> Self {
        self.eviction_mode = EvictionMode::Evictable;
        self
    }

    /// Protects this item from capacity eviction.
    pub const fn eviction_protected(mut self) -> Self {
        self.eviction_mode = EvictionMode::EvictionProtected;
        self
    }

    /// Resolves eviction eligibility from the selected namespace's default.
    pub const fn inherit_eviction(mut self) -> Self {
        self.eviction_mode = EvictionMode::Inherit;
        self
    }

    /// Returns the configured existence condition.
    pub const fn condition(self) -> SetCondition {
        self.condition
    }

    /// Returns the configured relative expiration in milliseconds.
    pub const fn time_to_live_millis(self) -> Option<u64> {
        self.time_to_live_ms
    }

    pub(crate) fn into_wire_options(self) -> Result<SetWireOptions> {
        if self.time_to_live_ms == Some(0) {
            return Err(Error::configuration(
                "set.time_to_live_ms",
                "must be greater than zero",
            ));
        }
        Ok(SetWireOptions::with_policies(
            self.condition,
            self.expiration_mode,
            self.time_to_live_ms,
            self.eviction_mode,
        ))
    }

    #[cfg(feature = "ffi")]
    pub(crate) fn from_wire_options(options: SetWireOptions) -> Result<Self> {
        let mut converted = match options.condition {
            SetCondition::Any => Self::new(),
            SetCondition::IfAbsent => Self::new().if_absent(),
            SetCondition::IfPresent => Self::new().if_present(),
        };
        converted = match options.expiration_mode {
            ExpirationMode::Inherit => converted.inherit_expiration(),
            ExpirationMode::NoExpiry => converted.no_expiry(),
            ExpirationMode::ExplicitTtl => {
                converted.expires_after_millis(options.ttl_ms.ok_or_else(|| {
                    Error::configuration(
                        "set.time_to_live_ms",
                        "must be present with explicit TTL mode",
                    )
                })?)
            }
        };
        Ok(match options.eviction_mode {
            EvictionMode::Inherit => converted.inherit_eviction(),
            EvictionMode::Evictable => converted.evictable(),
            EvictionMode::EvictionProtected => converted.eviction_protected(),
        })
    }
}