crabka_schema_serde/
subject.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Role {
7 Key,
8 Value,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum SchemaKind {
14 Avro,
15 Protobuf,
16 Json,
17}
18
19impl SchemaKind {
20 #[must_use]
22 pub fn wire_name(self) -> Option<&'static str> {
23 match self {
24 Self::Avro => None,
25 Self::Protobuf => Some("PROTOBUF"),
26 Self::Json => Some("JSON"),
27 }
28 }
29}
30
31pub trait SubjectStrategy: Send + Sync + 'static {
35 fn subject(&self, topic: &str, role: Role) -> String;
36}
37
38#[derive(Debug, Clone, Copy, Default)]
40pub struct TopicNameStrategy;
41
42impl SubjectStrategy for TopicNameStrategy {
43 fn subject(&self, topic: &str, role: Role) -> String {
44 match role {
45 Role::Key => format!("{topic}-key"),
46 Role::Value => format!("{topic}-value"),
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use assert2::check;
55
56 #[test]
57 fn topic_name_strategy() {
58 let s = TopicNameStrategy;
59 check!(s.subject("orders", Role::Value) == "orders-value");
60 check!(s.subject("orders", Role::Key) == "orders-key");
61 }
62
63 #[test]
64 fn schema_kind_wire_names() {
65 check!(SchemaKind::Avro.wire_name().is_none());
66 check!(SchemaKind::Protobuf.wire_name() == Some("PROTOBUF"));
67 check!(SchemaKind::Json.wire_name() == Some("JSON"));
68 }
69}