use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompatibilityMode {
None,
Backward,
Forward,
Full,
}
impl CompatibilityMode {
pub fn as_str(&self) -> &'static str {
match self {
CompatibilityMode::None => "none",
CompatibilityMode::Backward => "backward",
CompatibilityMode::Forward => "forward",
CompatibilityMode::Full => "full",
}
}
}
impl Default for CompatibilityMode {
fn default() -> Self {
CompatibilityMode::Backward
}
}
impl fmt::Display for CompatibilityMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for CompatibilityMode {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"none" => Ok(CompatibilityMode::None),
"backward" => Ok(CompatibilityMode::Backward),
"forward" => Ok(CompatibilityMode::Forward),
"full" => Ok(CompatibilityMode::Full),
other => Err(format!("unknown compatibility mode: '{}'", other)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaType {
Bytes,
String,
Number,
Avro,
JsonSchema,
Protobuf,
}
impl SchemaType {
pub fn as_str(&self) -> &'static str {
match self {
SchemaType::Bytes => "bytes",
SchemaType::String => "string",
SchemaType::Number => "number",
SchemaType::Avro => "avro",
SchemaType::JsonSchema => "json_schema",
SchemaType::Protobuf => "protobuf",
}
}
}
impl fmt::Display for SchemaType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for SchemaType {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"bytes" => Ok(SchemaType::Bytes),
"string" => Ok(SchemaType::String),
"number" => Ok(SchemaType::Number),
"avro" => Ok(SchemaType::Avro),
"json_schema" | "jsonschema" => Ok(SchemaType::JsonSchema),
"protobuf" | "proto" => Ok(SchemaType::Protobuf),
other => Err(format!("unknown schema type: '{}'", other)),
}
}
}
use danube_core::proto::danube_schema::GetSchemaResponse as ProtoGetSchemaResponse;
#[derive(Debug, Clone)]
pub struct SchemaInfo {
pub schema_id: u64,
pub subject: String,
pub version: u32,
pub schema_type: String,
pub schema_definition: Vec<u8>,
pub fingerprint: String,
}
impl SchemaInfo {
pub fn schema_definition_as_string(&self) -> Option<String> {
std::str::from_utf8(&self.schema_definition)
.ok()
.map(|s| s.to_string())
}
}
impl From<ProtoGetSchemaResponse> for SchemaInfo {
fn from(proto: ProtoGetSchemaResponse) -> Self {
SchemaInfo {
schema_id: proto.schema_id,
subject: proto.subject,
version: proto.version,
schema_type: proto.schema_type,
schema_definition: proto.schema_definition,
fingerprint: proto.fingerprint,
}
}
}