hydracache-client-protocol 0.55.0

Stable external client protocol primitives for HydraCache.
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
//! Java/Spring migration contract for legacy Hazelcast consumers.
//!
//! The real Java artifacts live outside the Cargo workspace, but their public
//! behavior is anchored here so the Rust protocol gate can enforce stable error
//! mapping, safe codec registration, and fail-loud unsupported Hazelcast APIs.

use std::collections::BTreeMap;

use thiserror::Error;

use crate::{ClientErrorCode, ClientErrorEnvelope};

/// Current Java migration contract version.
pub const JAVA_MIGRATION_CONTRACT_VERSION: u16 = 2;

/// Checked-in manifest of Hazelcast APIs that HydraCache refuses to emulate.
pub const UNSUPPORTED_HAZELCAST_APIS_MANIFEST: &str =
    include_str!("../manifests/unsupported_hazelcast_apis.txt");

/// Supported Spring Boot generations for the 0.49 Java migration contract.
pub const SUPPORTED_SPRING_BOOT_GENERATIONS: &[u8] = &[2, 3, 4];

/// Java exception kind documented for the Java client and Spring toolkit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaExceptionKind {
    /// No common protocol or an unsupported protocol feature was used.
    Protocol,
    /// The request did not carry an accepted identity.
    Authentication,
    /// The identity is known but not allowed to perform the operation.
    Authorization,
    /// Tenant quota was exceeded.
    QuotaExceeded,
    /// The caller is over its rate or fairness budget.
    RateLimited,
    /// Data-residency governance refused the operation.
    ResidencyDenied,
    /// Request frame or value bytes exceeded a configured bound.
    PayloadTooLarge,
    /// Request deadline expired.
    Timeout,
    /// Conditional or optimistic operation conflicted.
    Conflict,
    /// The backend is temporarily unavailable.
    BackendUnavailable,
    /// The client sent a malformed binary frame.
    MalformedFrame,
}

impl JavaExceptionKind {
    /// Stable Java class name for this exception kind.
    pub const fn class_name(self) -> &'static str {
        match self {
            Self::Protocol => "HydraCacheProtocolException",
            Self::Authentication => "HydraCacheAuthenticationException",
            Self::Authorization => "HydraCacheAuthorizationException",
            Self::QuotaExceeded => "HydraCacheQuotaExceededException",
            Self::RateLimited => "HydraCacheRateLimitedException",
            Self::ResidencyDenied => "HydraCacheResidencyDeniedException",
            Self::PayloadTooLarge => "HydraCachePayloadTooLargeException",
            Self::Timeout => "HydraCacheTimeoutException",
            Self::Conflict => "HydraCacheConflictException",
            Self::BackendUnavailable => "HydraCacheBackendUnavailableException",
            Self::MalformedFrame => "HydraCacheMalformedFrameException",
        }
    }
}

/// Java-facing mapping for one stable protocol error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JavaExceptionMapping {
    /// Documented exception kind.
    pub kind: JavaExceptionKind,
    /// Java class name the SDK must expose.
    pub class_name: &'static str,
    /// Retryability copied from the protocol envelope.
    pub retryable: bool,
    /// Whether the Java exception must retain the request id.
    pub preserves_request_id: bool,
    /// Whether the Java exception must retain retry-after metadata when present.
    pub preserves_retry_after: bool,
}

/// Return the Java exception mapping for a stable protocol error.
pub fn java_exception_mapping(error: &ClientErrorEnvelope) -> JavaExceptionMapping {
    let kind = match error.code {
        ClientErrorCode::IncompatibleVersion => JavaExceptionKind::Protocol,
        ClientErrorCode::Unauthenticated => JavaExceptionKind::Authentication,
        ClientErrorCode::Unauthorized => JavaExceptionKind::Authorization,
        ClientErrorCode::TenantQuota => JavaExceptionKind::QuotaExceeded,
        ClientErrorCode::RateLimited => JavaExceptionKind::RateLimited,
        ClientErrorCode::ResidencyDenied => JavaExceptionKind::ResidencyDenied,
        ClientErrorCode::TooLarge => JavaExceptionKind::PayloadTooLarge,
        ClientErrorCode::DeadlineExceeded => JavaExceptionKind::Timeout,
        ClientErrorCode::Conflict => JavaExceptionKind::Conflict,
        ClientErrorCode::BackendUnavailable => JavaExceptionKind::BackendUnavailable,
        ClientErrorCode::MalformedFrame => JavaExceptionKind::MalformedFrame,
    };

    JavaExceptionMapping {
        kind,
        class_name: kind.class_name(),
        retryable: error.retryable,
        preserves_request_id: true,
        preserves_retry_after: error.retry_after_ms.is_some(),
    }
}

/// Java application topology mode for migration from Hazelcast.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaClientTopology {
    /// Application JVM connects as a client.
    Client,
    /// Application JVM tries to become a data-owning member.
    Member,
    /// Toolkit is disabled and does not create a client.
    None,
}

/// Public identity source for Java clients.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaClientIdentityMode {
    /// Bearer/API token supplied by the application.
    Token,
    /// Client certificate identity over mTLS.
    Mtls,
}

/// Retry/backoff defaults for Java clients.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JavaRetryBackoff {
    /// Maximum attempts including the original call.
    pub max_attempts: u8,
    /// Initial backoff in milliseconds.
    pub initial_backoff_ms: u64,
    /// Maximum backoff in milliseconds.
    pub max_backoff_ms: u64,
}

impl Default for JavaRetryBackoff {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_backoff_ms: 25,
            max_backoff_ms: 1_000,
        }
    }
}

/// Java client runtime settings shared by the Boot 2/3/4 starters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JavaClientRuntimeConfig {
    /// Client endpoints.
    pub endpoints: Vec<String>,
    /// Tenant id carried to W4 isolation.
    pub tenant: String,
    /// Caller-visible client name.
    pub client_name: String,
    /// Whether the Java client may use server-provided routing hints.
    pub smart_routing: bool,
    /// Identity source.
    pub identity: JavaClientIdentityMode,
    /// Application topology mode.
    pub topology: JavaClientTopology,
    /// Retry/backoff policy.
    pub retry: JavaRetryBackoff,
    /// Default request deadline in milliseconds.
    pub deadline_ms: u64,
    /// Whether customizers are allowed to mutate the transport settings.
    pub customizer_hooks_enabled: bool,
}

impl JavaClientRuntimeConfig {
    /// Build client-first defaults.
    pub fn client_first(
        endpoints: impl IntoIterator<Item = impl Into<String>>,
        tenant: impl Into<String>,
        client_name: impl Into<String>,
    ) -> Self {
        Self {
            endpoints: endpoints.into_iter().map(Into::into).collect(),
            tenant: tenant.into(),
            client_name: client_name.into(),
            smart_routing: true,
            identity: JavaClientIdentityMode::Token,
            topology: JavaClientTopology::Client,
            retry: JavaRetryBackoff::default(),
            deadline_ms: 5_000,
            customizer_hooks_enabled: true,
        }
    }

    /// Validate release-0.49 Java client defaults.
    pub fn validate(&self) -> Result<(), JavaMigrationContractError> {
        if self.endpoints.is_empty() {
            return Err(JavaMigrationContractError::InvalidField("endpoints"));
        }
        if self.tenant.trim().is_empty() {
            return Err(JavaMigrationContractError::InvalidField("tenant"));
        }
        if self.client_name.trim().is_empty() {
            return Err(JavaMigrationContractError::InvalidField("client_name"));
        }
        if self.deadline_ms == 0 {
            return Err(JavaMigrationContractError::InvalidField("deadline_ms"));
        }
        if self.retry.max_attempts == 0 {
            return Err(JavaMigrationContractError::InvalidField(
                "retry.max_attempts",
            ));
        }
        if self.topology == JavaClientTopology::Member {
            return Err(JavaMigrationContractError::UnsupportedClientTopology(
                "member",
            ));
        }
        Ok(())
    }
}

/// Spring Cache integration mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpringCacheMode {
    /// Lazy map-backed cache names for legacy `CacheUtil` style code.
    Native,
    /// Bind to JCache when the optional JCache provider is present.
    JCache,
    /// Do not create a Spring `CacheManager`.
    None,
}

impl SpringCacheMode {
    /// Return whether this mode must lazily resolve dynamic cache names.
    pub const fn lazy_dynamic_cache_names(self) -> bool {
        matches!(self, Self::Native)
    }

    /// Return whether this mode requires a JCache provider.
    pub const fn requires_jcache_provider(self) -> bool {
        matches!(self, Self::JCache)
    }
}

/// Java map facade operation exposed by the migration toolkit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaMapOperation {
    /// `HydraCacheMap.get`.
    Get,
    /// `HydraCacheMap.put`.
    Put,
    /// `HydraCacheMap.putIfAbsent`.
    PutIfAbsent,
    /// `HydraCacheMap.replace(key, oldValue, newValue)`.
    Replace,
    /// `HydraCacheMap.replace(key, newValue)`.
    ReplaceIfPresent,
    /// `HydraCacheMap.remove`.
    Remove,
    /// `HydraCacheMap.remove(key, value)`.
    RemoveIfValue,
    /// `HydraCacheMap.addEntryListener`.
    AddEntryListener,
    /// `HydraCacheMap.containsKey`.
    ContainsKey,
    /// `HydraCacheMap.getAll`.
    GetAll,
    /// `HydraCacheMap.putAll`.
    PutAll,
    /// Key invalidation.
    Invalidate,
    /// Namespace clear for the map.
    ClearNamespace,
    /// Region/namespace eviction.
    EvictRegion,
}

/// Protocol-level operation family for a Java map facade method.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaMapProtocolFamily {
    /// Maps to protocol-v1 get.
    Get,
    /// Maps to protocol-v1 put.
    Put,
    /// Maps to protocol-v1 conflict-aware conditional put-if-absent.
    ConditionalPutIfAbsent,
    /// Maps to protocol-v2 compare-and-set replace.
    ConditionalReplace {
        /// Which wire expectation shape the facade must use.
        expectation: JavaMapCasExpectation,
    },
    /// Maps to protocol-v2 conditional tombstone.
    ConditionalRemove,
    /// Maps to protocol subscription with an entry-event projection.
    SubscribeInvalidations {
        /// Whether the mapping requests IMap entry-event shaping.
        projection: JavaMapListenerProjection,
    },
    /// Maps to protocol-v1 invalidation.
    Invalidate,
    /// Maps to protocol-v1 batch get.
    BatchGet,
    /// Maps to protocol-v1 batch put.
    BatchPut,
    /// Maps to protocol-v1 namespace/region eviction.
    EvictRegion,
}

/// Java map CAS expectation shape.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaMapCasExpectation {
    /// The caller provides an exact old value.
    ExactValue,
    /// Any live value is acceptable, but absent/tombstoned keys mismatch.
    Present,
}

/// Java listener projection over HydraCache invalidation signals.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaMapListenerProjection {
    /// IMap entry-event shaped cache signal.
    EntryEvent,
}

impl JavaMapOperation {
    /// Return the protocol family that backs this facade operation.
    pub const fn protocol_family(self) -> JavaMapProtocolFamily {
        match self {
            Self::Get | Self::ContainsKey => JavaMapProtocolFamily::Get,
            Self::Put => JavaMapProtocolFamily::Put,
            Self::PutIfAbsent => JavaMapProtocolFamily::ConditionalPutIfAbsent,
            Self::Replace => JavaMapProtocolFamily::ConditionalReplace {
                expectation: JavaMapCasExpectation::ExactValue,
            },
            Self::ReplaceIfPresent => JavaMapProtocolFamily::ConditionalReplace {
                expectation: JavaMapCasExpectation::Present,
            },
            Self::RemoveIfValue => JavaMapProtocolFamily::ConditionalRemove,
            Self::AddEntryListener => JavaMapProtocolFamily::SubscribeInvalidations {
                projection: JavaMapListenerProjection::EntryEvent,
            },
            Self::Remove | Self::Invalidate => JavaMapProtocolFamily::Invalidate,
            Self::GetAll => JavaMapProtocolFamily::BatchGet,
            Self::PutAll => JavaMapProtocolFamily::BatchPut,
            Self::ClearNamespace | Self::EvictRegion => JavaMapProtocolFamily::EvictRegion,
        }
    }
}

/// Java lock facade operation exposed by the migration toolkit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaLockOperation {
    /// `Lock.lock`.
    Lock,
    /// Hazelcast CP `lockAndGetFence`.
    LockAndGetFence,
    /// Immediate `tryLock`.
    TryLock,
    /// Timed `tryLock`.
    TryLockTimed,
    /// Explicit `unlock`.
    Unlock,
    /// Read the current fencing token.
    GetFence,
    /// Read whether the lock is held.
    IsLocked,
    /// Check whether this session/endpoint owns the lock.
    IsLockedByCurrentThread,
    /// Privileged force unlock.
    ForceUnlock,
}

/// Protocol-level operation family for a Java lock facade method.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaLockProtocolFamily {
    /// Maps to protocol-v2 `TryLock` and may wait/retry client-side.
    TryLock {
        /// Whether the Java facade returns the fence directly.
        returns_fence: bool,
        /// Whether the facade models blocking wait semantics.
        blocking_wait: bool,
    },
    /// Maps to protocol-v2 `Unlock`.
    Unlock,
    /// Maps to protocol-v2 `GetLockOwnership`.
    GetLockOwnership,
    /// Maps to protocol-v2 privileged `ForceUnlock`.
    ForceUnlock {
        /// Force unlock always requires admin authorization and audit.
        privileged: bool,
    },
}

impl JavaLockOperation {
    /// Return the protocol family that backs this facade operation.
    pub const fn protocol_family(self) -> JavaLockProtocolFamily {
        match self {
            Self::Lock => JavaLockProtocolFamily::TryLock {
                returns_fence: false,
                blocking_wait: true,
            },
            Self::LockAndGetFence => JavaLockProtocolFamily::TryLock {
                returns_fence: true,
                blocking_wait: true,
            },
            Self::TryLock => JavaLockProtocolFamily::TryLock {
                returns_fence: true,
                blocking_wait: false,
            },
            Self::TryLockTimed => JavaLockProtocolFamily::TryLock {
                returns_fence: true,
                blocking_wait: true,
            },
            Self::Unlock => JavaLockProtocolFamily::Unlock,
            Self::GetFence | Self::IsLocked | Self::IsLockedByCurrentThread => {
                JavaLockProtocolFamily::GetLockOwnership
            }
            Self::ForceUnlock => JavaLockProtocolFamily::ForceUnlock { privileged: true },
        }
    }

    /// Return whether this operation requires admin authorization and audit.
    pub const fn is_privileged(self) -> bool {
        matches!(self, Self::ForceUnlock)
    }
}

/// Serializer registration kind accepted or rejected by the Java toolkit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JavaCodecKind {
    /// Explicit codec instance or generated schema.
    Explicit,
    /// Package-scanned `@HydraCacheCodec`.
    CodecAnnotation,
    /// Package-scanned `@HydraCacheSchema`.
    SchemaAnnotation,
    /// Legacy serializer bridge enabled for migration only.
    LegacySerializerBridge,
    /// Reflective fallback serializer.
    ReflectiveFallback,
    /// Java native serialization.
    JavaNativeSerialization,
}

impl JavaCodecKind {
    /// Stable config label.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Explicit => "explicit",
            Self::CodecAnnotation => "codec-annotation",
            Self::SchemaAnnotation => "schema-annotation",
            Self::LegacySerializerBridge => "legacy-serializer-bridge",
            Self::ReflectiveFallback => "reflective-fallback",
            Self::JavaNativeSerialization => "java-native-serialization",
        }
    }
}

/// Codec/schema descriptor registered by the Java migration toolkit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JavaCodecDescriptor {
    /// Stable codec id.
    pub codec_id: String,
    /// Fully qualified Java type name.
    pub java_type: String,
    /// Schema version for reviewable migrations.
    pub schema_version: u32,
    /// Registration kind.
    pub kind: JavaCodecKind,
}

impl JavaCodecDescriptor {
    /// Create a descriptor.
    pub fn new(
        codec_id: impl Into<String>,
        java_type: impl Into<String>,
        schema_version: u32,
        kind: JavaCodecKind,
    ) -> Result<Self, JavaMigrationContractError> {
        let descriptor = Self {
            codec_id: codec_id.into(),
            java_type: java_type.into(),
            schema_version,
            kind,
        };
        descriptor.validate()?;
        Ok(descriptor)
    }

    /// Create an explicit codec descriptor.
    pub fn explicit(
        codec_id: impl Into<String>,
        java_type: impl Into<String>,
        schema_version: u32,
    ) -> Result<Self, JavaMigrationContractError> {
        Self::new(codec_id, java_type, schema_version, JavaCodecKind::Explicit)
    }

    fn validate(&self) -> Result<(), JavaMigrationContractError> {
        if self.codec_id.trim().is_empty() {
            return Err(JavaMigrationContractError::InvalidField("codec_id"));
        }
        if self.java_type.trim().is_empty() {
            return Err(JavaMigrationContractError::InvalidField("java_type"));
        }
        if self.schema_version == 0 {
            return Err(JavaMigrationContractError::InvalidField("schema_version"));
        }
        Ok(())
    }
}

/// Safe codec registry contract for Java clients.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct JavaCodecRegistryContract {
    codecs: BTreeMap<String, JavaCodecDescriptor>,
    legacy_serializer_bridge_enabled: bool,
}

impl JavaCodecRegistryContract {
    /// Create a registry with safe defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Explicitly allow the migration-only legacy serializer bridge.
    pub fn with_legacy_serializer_bridge_enabled(mut self) -> Self {
        self.legacy_serializer_bridge_enabled = true;
        self
    }

    /// Register a descriptor, failing loud on ambiguous or unsafe serializers.
    pub fn register(
        &mut self,
        descriptor: JavaCodecDescriptor,
    ) -> Result<(), JavaMigrationContractError> {
        match descriptor.kind {
            JavaCodecKind::ReflectiveFallback | JavaCodecKind::JavaNativeSerialization => {
                return Err(JavaMigrationContractError::UnsupportedSerializer(
                    descriptor.kind.as_str(),
                ));
            }
            JavaCodecKind::LegacySerializerBridge if !self.legacy_serializer_bridge_enabled => {
                return Err(JavaMigrationContractError::LegacySerializerBridgeDisabled(
                    descriptor.codec_id,
                ));
            }
            _ => {}
        }

        if self.codecs.contains_key(&descriptor.codec_id) {
            return Err(JavaMigrationContractError::AmbiguousCodec {
                codec_id: descriptor.codec_id,
            });
        }

        self.codecs.insert(descriptor.codec_id.clone(), descriptor);
        Ok(())
    }

    /// Return a descriptor by codec id.
    pub fn get(&self, codec_id: &str) -> Option<&JavaCodecDescriptor> {
        self.codecs.get(codec_id)
    }

    /// Number of registered descriptors.
    pub fn len(&self) -> usize {
        self.codecs.len()
    }

    /// Return whether the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.codecs.is_empty()
    }
}

/// One unsupported Hazelcast API and its migration hint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnsupportedHazelcastApi {
    /// Hazelcast API surface.
    pub api: String,
    /// Human migration hint.
    pub migration_hint: String,
}

/// One supported Hazelcast API migration mapping and its caveat.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SupportedHazelcastApiMapping {
    /// Hazelcast API surface.
    pub api: String,
    /// Human migration hint.
    pub migration_hint: String,
}

/// Versioned manifest of supported mappings and unsupported Hazelcast APIs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnsupportedHazelcastApiManifest {
    /// Manifest version.
    pub version: u16,
    /// Unsupported APIs.
    pub entries: Vec<UnsupportedHazelcastApi>,
    /// Supported source-level migration mappings.
    pub supported_mappings: Vec<SupportedHazelcastApiMapping>,
}

impl UnsupportedHazelcastApiManifest {
    /// Parse a manifest and reject unknown future versions.
    pub fn parse(contents: &str) -> Result<Self, JavaMigrationContractError> {
        let mut version = None;
        let mut entries = Vec::new();
        let mut supported_mappings = Vec::new();

        for raw in contents.lines() {
            let line = raw.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Some(value) = line.strip_prefix("version=") {
                let parsed = value
                    .parse()
                    .map_err(|_| JavaMigrationContractError::InvalidManifest("version"))?;
                version = Some(parsed);
                continue;
            }

            let parts = line.split('|').map(str::trim).collect::<Vec<_>>();
            let (kind, api, migration_hint) = match parts.as_slice() {
                [api, migration_hint] => ("unsupported", *api, *migration_hint),
                [kind @ ("unsupported" | "supported"), api, migration_hint] => {
                    (*kind, *api, *migration_hint)
                }
                _ => {
                    return Err(JavaMigrationContractError::InvalidManifest("entry"));
                }
            };
            if api.is_empty() || migration_hint.is_empty() {
                return Err(JavaMigrationContractError::InvalidManifest("entry"));
            }
            match kind {
                "unsupported" => entries.push(UnsupportedHazelcastApi {
                    api: api.to_owned(),
                    migration_hint: migration_hint.to_owned(),
                }),
                "supported" => supported_mappings.push(SupportedHazelcastApiMapping {
                    api: api.to_owned(),
                    migration_hint: migration_hint.to_owned(),
                }),
                _ => return Err(JavaMigrationContractError::InvalidManifest("entry")),
            }
        }

        let version = version.ok_or(JavaMigrationContractError::InvalidManifest("version"))?;
        if version != JAVA_MIGRATION_CONTRACT_VERSION {
            return Err(JavaMigrationContractError::UnsupportedManifestVersion {
                actual: version,
                supported: JAVA_MIGRATION_CONTRACT_VERSION,
            });
        }
        if entries.is_empty() {
            return Err(JavaMigrationContractError::InvalidManifest("entries"));
        }

        Ok(Self {
            version,
            entries,
            supported_mappings,
        })
    }

    /// Parse the checked-in manifest.
    pub fn checked_in() -> Result<Self, JavaMigrationContractError> {
        Self::parse(UNSUPPORTED_HAZELCAST_APIS_MANIFEST)
    }

    /// Find a manifest entry by API name.
    pub fn find(&self, api: &str) -> Option<&UnsupportedHazelcastApi> {
        self.entries.iter().find(|entry| entry.api == api)
    }

    /// Find a supported mapping entry by API name.
    pub fn find_supported(&self, api: &str) -> Option<&SupportedHazelcastApiMapping> {
        self.supported_mappings
            .iter()
            .find(|entry| entry.api == api)
    }
}

/// Java migration contract errors.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum JavaMigrationContractError {
    /// A field is missing or invalid.
    #[error("invalid java migration contract field: {0}")]
    InvalidField(&'static str),
    /// Application JVM member mode is unsupported in release 0.49.
    #[error("unsupported java client topology for release 0.49: {0}")]
    UnsupportedClientTopology(&'static str),
    /// Codec id is ambiguous.
    #[error("ambiguous java codec id: {codec_id}")]
    AmbiguousCodec {
        /// Duplicate codec id.
        codec_id: String,
    },
    /// Serializer kind is unsupported.
    #[error("unsupported java serializer kind: {0}")]
    UnsupportedSerializer(&'static str),
    /// Legacy serializer bridge is disabled by default.
    #[error("legacy serializer bridge is disabled for codec id: {0}")]
    LegacySerializerBridgeDisabled(String),
    /// Manifest is malformed.
    #[error("invalid unsupported Hazelcast API manifest: {0}")]
    InvalidManifest(&'static str),
    /// Manifest version is newer than this library supports.
    #[error(
        "unsupported unsupported-Hazelcast-API manifest version {actual}; supported {supported}"
    )]
    UnsupportedManifestVersion {
        /// Actual manifest version.
        actual: u16,
        /// Supported manifest version.
        supported: u16,
    },
}