danube_client/
schema_types.rs

1/// Schema compatibility modes for schema evolution
2///
3/// Defines how strictly new schema versions must be compatible with existing versions.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum CompatibilityMode {
6    /// No compatibility checking - any schema change is allowed
7    None,
8    /// New schema can read data written with old schema (most common)
9    /// - Allows: adding optional fields, removing fields from reader
10    /// - Use case: Consumers upgrade before producers
11    Backward,
12    /// Old schema can read data written with new schema
13    /// - Allows: adding required fields, removing optional fields
14    /// - Use case: Producers upgrade before consumers
15    Forward,
16    /// Both backward and forward compatible (strictest)
17    /// - Use case: Critical schemas that need both directions
18    Full,
19}
20
21impl CompatibilityMode {
22    /// Convert to string representation for API calls
23    pub fn as_str(&self) -> &'static str {
24        match self {
25            CompatibilityMode::None => "none",
26            CompatibilityMode::Backward => "backward",
27            CompatibilityMode::Forward => "forward",
28            CompatibilityMode::Full => "full",
29        }
30    }
31}
32
33impl Default for CompatibilityMode {
34    fn default() -> Self {
35        CompatibilityMode::Backward
36    }
37}
38
39/// Schema types supported by the registry
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum SchemaType {
42    /// Raw bytes - no schema validation
43    /// Use for binary data or custom serialization
44    Bytes,
45
46    /// UTF-8 string - validates string encoding
47    /// Use for plain text messages
48    String,
49
50    /// Numeric types (int, long, float, double)
51    /// Validates numeric data
52    Number,
53
54    /// Apache Avro schema format
55    /// Structured binary format with schema evolution support
56    Avro,
57
58    /// JSON Schema format
59    /// JSON-based schema validation
60    JsonSchema,
61
62    /// Protocol Buffers schema format
63    /// Google's language-neutral serialization format
64    Protobuf,
65}
66
67impl SchemaType {
68    /// Convert to string representation for API calls
69    pub fn as_str(&self) -> &'static str {
70        match self {
71            SchemaType::Bytes => "bytes",
72            SchemaType::String => "string",
73            SchemaType::Number => "number",
74            SchemaType::Avro => "avro",
75            SchemaType::JsonSchema => "json_schema",
76            SchemaType::Protobuf => "protobuf",
77        }
78    }
79}
80
81use danube_core::proto::danube_schema::GetSchemaResponse as ProtoGetSchemaResponse;
82
83/// Information about a schema retrieved from the registry
84///
85/// This is a user-friendly wrapper around the proto GetSchemaResponse.
86/// Consumers can use this to fetch and validate schemas.
87#[derive(Debug, Clone)]
88pub struct SchemaInfo {
89    /// Schema ID (identifies the subject)
90    pub schema_id: u64,
91    /// Subject name
92    pub subject: String,
93    /// Schema version number
94    pub version: u32,
95    /// Schema type (avro, json, protobuf, etc.)
96    pub schema_type: String,
97    /// Schema definition as bytes (e.g., Avro schema JSON, Protobuf descriptor)
98    pub schema_definition: Vec<u8>,
99    /// Fingerprint for deduplication
100    pub fingerprint: String,
101}
102
103impl SchemaInfo {
104    /// Get schema definition as a UTF-8 string (for JSON-based schemas)
105    ///
106    /// Returns None if the schema definition is not valid UTF-8
107    pub fn schema_definition_as_string(&self) -> Option<String> {
108        String::from_utf8(self.schema_definition.clone()).ok()
109    }
110}
111
112impl From<ProtoGetSchemaResponse> for SchemaInfo {
113    fn from(proto: ProtoGetSchemaResponse) -> Self {
114        SchemaInfo {
115            schema_id: proto.schema_id,
116            subject: proto.subject,
117            version: proto.version,
118            schema_type: proto.schema_type,
119            schema_definition: proto.schema_definition,
120            fingerprint: proto.fingerprint,
121        }
122    }
123}