schemreg 0.1.0

Async Confluent + AWS Glue schema registry client — wire format, traits, caching, HTTP
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
//! Async trait interfaces for schema registry backends, caches, and codecs.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use bytes::Bytes;

use crate::error::Result;
use crate::types::{Schema, SchemaId, SchemaReference, SchemaType, SchemaVersion};

/// Async client interface for a schema registry.
///
/// Implement this trait to integrate with any schema registry backend.
/// When the `confluent` feature is enabled, [`ConfluentSchemaRegistry`](crate::confluent::ConfluentSchemaRegistry)
/// provides a ready-made HTTP implementation for the Confluent Schema
/// Registry (and compatible registries such as Karapace and Apicurio).
///
/// All methods use `async fn` (RPITIT), allowing zero-cost monomorphization at
/// generic call sites. Object-safe erased wrappers are used internally where
/// dynamic dispatch is needed (e.g. [`WireFormatDecoder`](crate::WireFormatDecoder)).
pub trait SchemaRegistryClient: Send + Sync {
    /// Retrieve a schema by its globally unique ID.
    ///
    /// Schema IDs are immutable — a given ID always maps to the same schema.
    /// The returned `Arc<Schema>` allows callers to hold a zero-copy reference
    /// into the cache without cloning the schema bytes.
    fn get_schema_by_id(
        &self,
        id: SchemaId,
    ) -> impl Future<Output = Result<Arc<Schema>>> + Send + '_;

    /// Retrieve the latest schema registered under the given subject.
    fn get_latest_schema<'a>(
        &'a self,
        subject: &'a str,
    ) -> impl Future<Output = Result<Schema>> + Send + 'a;

    /// Retrieve a specific version of a schema under a subject.
    fn get_schema_by_version<'a>(
        &'a self,
        subject: &'a str,
        version: SchemaVersion,
    ) -> impl Future<Output = Result<Schema>> + Send + 'a;

    /// Register a schema under the given subject.
    ///
    /// If the same schema is already registered, the existing ID is returned
    /// (the operation is idempotent). Pass `&[]` for `references` when the
    /// schema has no dependencies.
    fn register_schema<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: SchemaType,
        references: &'a [SchemaReference],
    ) -> impl Future<Output = Result<SchemaId>> + Send + 'a;

    /// Check whether a schema is compatible with the latest version registered
    /// under `subject`.
    ///
    /// Returns `true` when the schema is compatible according to the subject's
    /// configured compatibility level, `false` otherwise.
    ///
    /// Implementations that do not support this operation return
    /// `Err(SchemaRegError::registry("check_compatibility: not implemented"))`.
    fn check_compatibility<'a>(
        &'a self,
        _subject: &'a str,
        _schema: &'a str,
        _schema_type: SchemaType,
        _references: &'a [SchemaReference],
    ) -> impl Future<Output = Result<bool>> + Send + 'a {
        std::future::ready(Err(crate::error::SchemaRegError::not_supported(
            "check_compatibility is not supported by this registry",
        )))
    }

    /// Delete a subject and all its registered versions.
    ///
    /// Returns the list of deleted version numbers. Set `permanent` to `true`
    /// to perform a hard delete (bypasses the soft-delete stage).
    ///
    /// Implementations that do not support this operation return
    /// `Err(SchemaRegError::registry("delete_subject: not implemented"))`.
    fn delete_subject<'a>(
        &'a self,
        _subject: &'a str,
        _permanent: bool,
    ) -> impl Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a {
        std::future::ready(Err(crate::error::SchemaRegError::not_supported(
            "delete_subject is not supported by this registry",
        )))
    }

    /// List all subjects currently registered in the registry.
    ///
    /// Implementations that do not support this operation return
    /// `Err(SchemaRegError::registry("get_subjects: not implemented"))`.
    fn get_subjects(&self) -> impl Future<Output = Result<Vec<String>>> + Send + '_ {
        std::future::ready(Err(crate::error::SchemaRegError::not_supported(
            "get_subjects is not supported by this registry",
        )))
    }

    /// List all version numbers registered under `subject`.
    ///
    /// Implementations that do not support this operation return
    /// `Err(SchemaRegError::registry("get_versions: not implemented"))`.
    fn get_versions<'a>(
        &'a self,
        _subject: &'a str,
    ) -> impl Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a {
        std::future::ready(Err(crate::error::SchemaRegError::not_supported(
            "get_versions is not supported by this registry",
        )))
    }
}

// Blanket forward implementation so that `&T` and `Arc<T>` can be used
// wherever a `SchemaRegistryClient` is expected.
impl<T: SchemaRegistryClient + ?Sized> SchemaRegistryClient for &T {
    fn get_schema_by_id(
        &self,
        id: crate::types::SchemaId,
    ) -> impl Future<Output = crate::error::Result<Arc<crate::types::Schema>>> + Send + '_ {
        T::get_schema_by_id(self, id)
    }
    fn get_latest_schema<'a>(
        &'a self,
        subject: &'a str,
    ) -> impl Future<Output = crate::error::Result<crate::types::Schema>> + Send + 'a {
        T::get_latest_schema(self, subject)
    }
    fn get_schema_by_version<'a>(
        &'a self,
        subject: &'a str,
        version: crate::types::SchemaVersion,
    ) -> impl Future<Output = crate::error::Result<crate::types::Schema>> + Send + 'a {
        T::get_schema_by_version(self, subject, version)
    }
    fn register_schema<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: crate::types::SchemaType,
        references: &'a [crate::types::SchemaReference],
    ) -> impl Future<Output = crate::error::Result<crate::types::SchemaId>> + Send + 'a {
        T::register_schema(self, subject, schema, schema_type, references)
    }
    fn check_compatibility<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: crate::types::SchemaType,
        references: &'a [crate::types::SchemaReference],
    ) -> impl Future<Output = crate::error::Result<bool>> + Send + 'a {
        T::check_compatibility(self, subject, schema, schema_type, references)
    }
    fn delete_subject<'a>(
        &'a self,
        subject: &'a str,
        permanent: bool,
    ) -> impl Future<Output = crate::error::Result<Vec<crate::types::SchemaVersion>>> + Send + 'a
    {
        T::delete_subject(self, subject, permanent)
    }
    fn get_subjects(&self) -> impl Future<Output = crate::error::Result<Vec<String>>> + Send + '_ {
        T::get_subjects(self)
    }
    fn get_versions<'a>(
        &'a self,
        subject: &'a str,
    ) -> impl Future<Output = crate::error::Result<Vec<crate::types::SchemaVersion>>> + Send + 'a
    {
        T::get_versions(self, subject)
    }
}

impl<T: SchemaRegistryClient + ?Sized> SchemaRegistryClient for std::sync::Arc<T> {
    fn get_schema_by_id(
        &self,
        id: crate::types::SchemaId,
    ) -> impl Future<Output = crate::error::Result<Arc<crate::types::Schema>>> + Send + '_ {
        T::get_schema_by_id(self, id)
    }
    fn get_latest_schema<'a>(
        &'a self,
        subject: &'a str,
    ) -> impl Future<Output = crate::error::Result<crate::types::Schema>> + Send + 'a {
        T::get_latest_schema(self, subject)
    }
    fn get_schema_by_version<'a>(
        &'a self,
        subject: &'a str,
        version: crate::types::SchemaVersion,
    ) -> impl Future<Output = crate::error::Result<crate::types::Schema>> + Send + 'a {
        T::get_schema_by_version(self, subject, version)
    }
    fn register_schema<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: crate::types::SchemaType,
        references: &'a [crate::types::SchemaReference],
    ) -> impl Future<Output = crate::error::Result<crate::types::SchemaId>> + Send + 'a {
        T::register_schema(self, subject, schema, schema_type, references)
    }
    fn check_compatibility<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: crate::types::SchemaType,
        references: &'a [crate::types::SchemaReference],
    ) -> impl Future<Output = crate::error::Result<bool>> + Send + 'a {
        T::check_compatibility(self, subject, schema, schema_type, references)
    }
    fn delete_subject<'a>(
        &'a self,
        subject: &'a str,
        permanent: bool,
    ) -> impl Future<Output = crate::error::Result<Vec<crate::types::SchemaVersion>>> + Send + 'a
    {
        T::delete_subject(self, subject, permanent)
    }
    fn get_subjects(&self) -> impl Future<Output = crate::error::Result<Vec<String>>> + Send + '_ {
        T::get_subjects(self)
    }
    fn get_versions<'a>(
        &'a self,
        subject: &'a str,
    ) -> impl Future<Output = crate::error::Result<Vec<crate::types::SchemaVersion>>> + Send + 'a
    {
        T::get_versions(self, subject)
    }
}

/// Shared cache-management interface implemented by schema cache wrappers.
///
/// This trait allows generic orchestration over both
/// [`CachedSchemaRegistry`](crate::CachedSchemaRegistry) and
/// [`glue::CachedGlueSchemaRegistry`](crate::glue::CachedGlueSchemaRegistry)
/// for cache lifecycle operations (invalidate, clear, prewarm), without
/// coupling to a specific registry provider.
pub trait AnySchemaCache: Send + Sync {
    /// Identifier type used by this cache (schema ID or schema version ID).
    type Id: Copy + Send + Sync;

    /// Number of entries currently held in the cache.
    fn cache_len(&self) -> usize;

    /// Returns `true` when the cache contains no entries.
    fn cache_is_empty(&self) -> bool;

    /// Clear all cached entries and cancel in-flight cache repopulation.
    fn clear_cache(&self);

    /// Invalidate a specific cache entry.
    fn invalidate(&self, id: Self::Id);

    /// Invalidate all cache entries.
    fn invalidate_all(&self);

    /// Pre-warm the cache for a set of immutable IDs.
    fn warm_cache<'a>(
        &'a self,
        ids: &'a [Self::Id],
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
}

/// Pluggable schema encoder for producer payloads.
///
/// Implement this trait to encode raw bytes into wire-framed bytes before
/// sending. The default implementation ([`ConfluentSchemaEncoder`](crate::confluent::ConfluentSchemaEncoder))
/// registers schemas with a Confluent-compatible registry and applies the
/// 5-byte Confluent wire format header.
///
/// # Object Safety
///
/// The trait is object-safe. Use `Arc<dyn SchemaEncoder>` to share an encoder
/// across tasks or store it in a struct field.
///
/// # Example
///
/// ```rust,ignore
/// use std::pin::Pin;
/// use std::future::Future;
/// use bytes::Bytes;
/// use schemreg::SchemaEncoder;
/// use schemreg::error::Result;
///
/// struct NoopEncoder;
///
/// impl SchemaEncoder for NoopEncoder {
///     fn encode(
///         &self,
///         payload: Bytes,
///         _topic: &str,
///         _record_name: Option<&str>,
///         _is_key: bool,
///     ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + '_>> {
///         Box::pin(async move { Ok(payload) })
///     }
/// }
/// ```
pub trait SchemaEncoder: Send + Sync {
    /// Encode raw bytes, returning wire-framed bytes.
    ///
    /// `payload` contains the raw (pre-serialized) bytes to frame.
    /// `topic` is the target topic name. `record_name` is the schema record
    /// name (used by [`SubjectNameStrategy::RecordName`](crate::SubjectNameStrategy::RecordName)
    /// and [`SubjectNameStrategy::TopicRecordName`](crate::SubjectNameStrategy::TopicRecordName);
    /// pass `None` for the `TopicName` strategy). `is_key` distinguishes
    /// key vs value subjects.
    fn encode(
        &self,
        payload: Bytes,
        topic: &str,
        record_name: Option<&str>,
        is_key: bool,
    ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + '_>>;
}

/// Object-safe async trait for consumer-side schema decoding.
///
/// A [`SchemaDecoder`] receives a raw (possibly wire-framed) [`Bytes`] payload,
/// strips any framing, and returns the decoded payload.
///
/// # Example — custom decoder
///
/// ```rust,ignore
/// use std::pin::Pin;
/// use std::future::Future;
/// use bytes::Bytes;
/// use schemreg::SchemaDecoder;
/// use schemreg::error::Result;
///
/// struct StripPrefixDecoder;
///
/// impl SchemaDecoder for StripPrefixDecoder {
///     fn decode(
///         &self,
///         payload: Bytes,
///         _topic: &str,
///         _is_key: bool,
///     ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + '_>> {
///         Box::pin(async move {
///             // Strip a 4-byte proprietary header.
///             Ok(payload.slice(4..))
///         })
///     }
/// }
/// ```
pub trait SchemaDecoder: Send + Sync {
    /// Decode a wire-framed payload, returning the raw inner bytes.
    ///
    /// `payload` is the raw bytes (key or value).
    /// `topic` is the source topic name. `is_key` is `true` for key
    /// payloads and `false` for value payloads.
    fn decode(
        &self,
        payload: Bytes,
        topic: &str,
        is_key: bool,
    ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + '_>>;
}

// ── DynSchemaRegistryClient ───────────────────────────────────────────────

/// Object-safe variant of [`SchemaRegistryClient`].
///
/// Because [`SchemaRegistryClient`] uses `impl Future` return types (RPITIT)
/// it cannot be used as `dyn SchemaRegistryClient`. This trait provides the
/// same interface with [`Pin<Box<dyn Future>>`] return types, enabling you to
/// hold and pass schema registry clients as trait objects:
///
/// ```rust,ignore
/// use std::sync::Arc;
/// use schemreg::DynSchemaRegistryClient;
///
/// fn use_registry(client: Arc<dyn DynSchemaRegistryClient>) {
///     // store in structs, pass across async boundaries, etc.
/// }
/// ```
///
/// A blanket implementation is provided for every type that implements
/// [`SchemaRegistryClient`], so no extra `impl` is needed.
pub trait DynSchemaRegistryClient: Send + Sync {
    /// Retrieve a schema by its globally unique ID.
    fn get_schema_by_id<'a>(
        &'a self,
        id: SchemaId,
    ) -> Pin<Box<dyn Future<Output = Result<Arc<Schema>>> + Send + 'a>>;

    /// Retrieve the latest schema registered under the given subject.
    fn get_latest_schema<'a>(
        &'a self,
        subject: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Schema>> + Send + 'a>>;

    /// Retrieve a specific version of a schema under a subject.
    fn get_schema_by_version<'a>(
        &'a self,
        subject: &'a str,
        version: SchemaVersion,
    ) -> Pin<Box<dyn Future<Output = Result<Schema>> + Send + 'a>>;

    /// Register a schema under the given subject.
    fn register_schema<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: SchemaType,
        references: &'a [SchemaReference],
    ) -> Pin<Box<dyn Future<Output = Result<SchemaId>> + Send + 'a>>;

    /// Check whether a schema is compatible with the latest registered version.
    fn check_compatibility<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: SchemaType,
        references: &'a [SchemaReference],
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'a>>;

    /// Delete a subject and all its registered versions.
    fn delete_subject<'a>(
        &'a self,
        subject: &'a str,
        permanent: bool,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a>>;

    /// List all subjects currently registered.
    fn get_subjects<'a>(&'a self)
    -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'a>>;

    /// List all version numbers registered under `subject`.
    fn get_versions<'a>(
        &'a self,
        subject: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a>>;
}

/// Blanket implementation: any [`SchemaRegistryClient`] is automatically a
/// [`DynSchemaRegistryClient`].
impl<T: SchemaRegistryClient> DynSchemaRegistryClient for T {
    fn get_schema_by_id<'a>(
        &'a self,
        id: SchemaId,
    ) -> Pin<Box<dyn Future<Output = Result<Arc<Schema>>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::get_schema_by_id(self, id))
    }
    fn get_latest_schema<'a>(
        &'a self,
        subject: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Schema>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::get_latest_schema(self, subject))
    }
    fn get_schema_by_version<'a>(
        &'a self,
        subject: &'a str,
        version: SchemaVersion,
    ) -> Pin<Box<dyn Future<Output = Result<Schema>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::get_schema_by_version(
            self, subject, version,
        ))
    }
    fn register_schema<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: SchemaType,
        references: &'a [SchemaReference],
    ) -> Pin<Box<dyn Future<Output = Result<SchemaId>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::register_schema(
            self,
            subject,
            schema,
            schema_type,
            references,
        ))
    }
    fn check_compatibility<'a>(
        &'a self,
        subject: &'a str,
        schema: &'a str,
        schema_type: SchemaType,
        references: &'a [SchemaReference],
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::check_compatibility(
            self,
            subject,
            schema,
            schema_type,
            references,
        ))
    }
    fn delete_subject<'a>(
        &'a self,
        subject: &'a str,
        permanent: bool,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::delete_subject(
            self, subject, permanent,
        ))
    }
    fn get_subjects<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::get_subjects(self))
    }
    fn get_versions<'a>(
        &'a self,
        subject: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<SchemaVersion>>> + Send + 'a>> {
        Box::pin(SchemaRegistryClient::get_versions(self, subject))
    }
}