use std::collections::HashMap;
use spacedb_consistency::Tier;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CrdtType {
Register,
Counter,
Text,
Set,
}
impl CrdtType {
pub fn name(&self) -> &'static str {
match self {
CrdtType::Register => "register",
CrdtType::Counter => "counter",
CrdtType::Text => "text",
CrdtType::Set => "set",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FieldSpec {
pub crdt: CrdtType,
pub tier: Tier,
}
#[derive(Clone, Debug)]
pub struct Schema {
collection: String,
fields: HashMap<String, FieldSpec>,
}
impl Schema {
pub fn new(collection: impl Into<String>) -> Self {
Self {
collection: collection.into(),
fields: HashMap::new(),
}
}
pub fn field(mut self, name: impl Into<String>, crdt: CrdtType, tier: Tier) -> Self {
self.fields.insert(name.into(), FieldSpec { crdt, tier });
self
}
pub fn collection(&self) -> &str {
&self.collection
}
pub fn spec(&self, field: &str) -> Option<FieldSpec> {
self.fields.get(field).copied()
}
}