#[cfg(feature = "avro")]
pub mod avro;
#[cfg(feature = "json")]
pub mod json;
#[cfg(feature = "protobuf")]
pub mod protobuf;
use std::sync::Arc;
use bytes::Bytes;
use crate::cache::SchemaCache;
use crate::error::SchemaSerdeError;
use crate::subject::{Role, SchemaKind};
pub trait SchemaSerializer<T>: Send + Sync + 'static {
fn serialize(&self, topic: &str, value: &T) -> Result<Bytes, SchemaSerdeError>;
}
pub trait SchemaDeserializer<T>: Send + Sync + 'static {
fn deserialize(&self, topic: &str, bytes: &[u8]) -> Result<T, SchemaSerdeError>;
}
pub trait SchemaSubject: Send + Sync + 'static {
fn register_subject(&self, topic: &str);
}
#[derive(Clone)]
pub(crate) struct Binding {
pub cache: Arc<SchemaCache>,
pub role: Role,
pub kind: SchemaKind,
pub schema: String,
}
impl Binding {
pub(crate) fn subject(&self, topic: &str) -> String {
self.cache.subject(topic, self.role)
}
pub(crate) fn id(&self, topic: &str) -> Result<u32, SchemaSerdeError> {
let subject = self.subject(topic);
self.cache.id_for_subject(&subject).ok_or_else(|| {
SchemaSerdeError::Schema(format!("id for {subject} not resolved (run prewarm)"))
})
}
pub(crate) fn register(&self, topic: &str) {
let subject = self.subject(topic);
self.cache.intern(&subject, self.kind, &self.schema);
}
}