use serde::{Deserialize, Serialize};
use crate::{common::*, schema::NamedDataType};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct ExternalSchemaV2 {
pub(crate) named_data_types: Vec<NamedDataType>,
pub(crate) tables: Vec<Table>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub(crate) enum ExternalSchema {
V2(ExternalSchemaV2),
V1(Table),
}
impl ExternalSchema {
pub(crate) fn from_schema(schema: Schema) -> Self {
let v2 = ExternalSchemaV2 {
named_data_types: schema.named_data_types.into_values().collect(),
tables: vec![schema.table],
};
ExternalSchema::V2(v2)
}
pub(crate) fn into_schema(self) -> Result<Schema> {
match self {
ExternalSchema::V2(mut v2) => {
if v2.tables.len() != 1 {
return Err(format_err!(
"dbcrossbar-schema must contain only a single table for now"
));
}
let table = v2.tables.remove(0);
Schema::from_types_and_table(v2.named_data_types, table)
}
ExternalSchema::V1(table) => Schema::from_table(table),
}
}
}