danube_client/
schema_types.rs1use std::fmt;
2use std::str::FromStr;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum CompatibilityMode {
9 None,
11 Backward,
15 Forward,
19 Full,
22}
23
24impl CompatibilityMode {
25 pub fn as_str(&self) -> &'static str {
27 match self {
28 CompatibilityMode::None => "none",
29 CompatibilityMode::Backward => "backward",
30 CompatibilityMode::Forward => "forward",
31 CompatibilityMode::Full => "full",
32 }
33 }
34}
35
36impl Default for CompatibilityMode {
37 fn default() -> Self {
38 CompatibilityMode::Backward
39 }
40}
41
42impl fmt::Display for CompatibilityMode {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.write_str(self.as_str())
45 }
46}
47
48impl FromStr for CompatibilityMode {
49 type Err = String;
50
51 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
52 match s.to_lowercase().as_str() {
53 "none" => Ok(CompatibilityMode::None),
54 "backward" => Ok(CompatibilityMode::Backward),
55 "forward" => Ok(CompatibilityMode::Forward),
56 "full" => Ok(CompatibilityMode::Full),
57 other => Err(format!("unknown compatibility mode: '{}'", other)),
58 }
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum SchemaType {
65 Bytes,
68
69 String,
72
73 Number,
76
77 Avro,
80
81 JsonSchema,
84
85 Protobuf,
88}
89
90impl SchemaType {
91 pub fn as_str(&self) -> &'static str {
93 match self {
94 SchemaType::Bytes => "bytes",
95 SchemaType::String => "string",
96 SchemaType::Number => "number",
97 SchemaType::Avro => "avro",
98 SchemaType::JsonSchema => "json_schema",
99 SchemaType::Protobuf => "protobuf",
100 }
101 }
102}
103
104impl fmt::Display for SchemaType {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 f.write_str(self.as_str())
107 }
108}
109
110impl FromStr for SchemaType {
111 type Err = String;
112
113 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
114 match s.to_lowercase().as_str() {
115 "bytes" => Ok(SchemaType::Bytes),
116 "string" => Ok(SchemaType::String),
117 "number" => Ok(SchemaType::Number),
118 "avro" => Ok(SchemaType::Avro),
119 "json_schema" | "jsonschema" => Ok(SchemaType::JsonSchema),
120 "protobuf" | "proto" => Ok(SchemaType::Protobuf),
121 other => Err(format!("unknown schema type: '{}'", other)),
122 }
123 }
124}
125
126use danube_core::proto::danube_schema::GetSchemaResponse as ProtoGetSchemaResponse;
127
128#[derive(Debug, Clone)]
133pub struct SchemaInfo {
134 pub schema_id: u64,
136 pub subject: String,
138 pub version: u32,
140 pub schema_type: String,
142 pub schema_definition: Vec<u8>,
144 pub fingerprint: String,
146}
147
148impl SchemaInfo {
149 pub fn schema_definition_as_string(&self) -> Option<String> {
153 std::str::from_utf8(&self.schema_definition)
154 .ok()
155 .map(|s| s.to_string())
156 }
157}
158
159impl From<ProtoGetSchemaResponse> for SchemaInfo {
160 fn from(proto: ProtoGetSchemaResponse) -> Self {
161 SchemaInfo {
162 schema_id: proto.schema_id,
163 subject: proto.subject,
164 version: proto.version,
165 schema_type: proto.schema_type,
166 schema_definition: proto.schema_definition,
167 fingerprint: proto.fingerprint,
168 }
169 }
170}