#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
Key,
Value,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaKind {
Avro,
Protobuf,
Json,
}
impl SchemaKind {
#[must_use]
pub fn wire_name(self) -> Option<&'static str> {
match self {
Self::Avro => None,
Self::Protobuf => Some("PROTOBUF"),
Self::Json => Some("JSON"),
}
}
}
pub trait SubjectStrategy: Send + Sync + 'static {
fn subject(&self, topic: &str, role: Role) -> String;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct TopicNameStrategy;
impl SubjectStrategy for TopicNameStrategy {
fn subject(&self, topic: &str, role: Role) -> String {
match role {
Role::Key => format!("{topic}-key"),
Role::Value => format!("{topic}-value"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert2::check;
#[test]
fn topic_name_strategy() {
let s = TopicNameStrategy;
check!(s.subject("orders", Role::Value) == "orders-value");
check!(s.subject("orders", Role::Key) == "orders-key");
}
#[test]
fn schema_kind_wire_names() {
check!(SchemaKind::Avro.wire_name().is_none());
check!(SchemaKind::Protobuf.wire_name() == Some("PROTOBUF"));
check!(SchemaKind::Json.wire_name() == Some("JSON"));
}
}