1use std::collections::HashMap;
7
8use spacedb_consistency::Tier;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum CrdtType {
13 Register,
15 Counter,
17 Text,
19 Set,
21}
22
23impl CrdtType {
24 pub fn name(&self) -> &'static str {
25 match self {
26 CrdtType::Register => "register",
27 CrdtType::Counter => "counter",
28 CrdtType::Text => "text",
29 CrdtType::Set => "set",
30 }
31 }
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub struct FieldSpec {
37 pub crdt: CrdtType,
38 pub tier: Tier,
39}
40
41#[derive(Clone, Debug)]
43pub struct Schema {
44 collection: String,
45 fields: HashMap<String, FieldSpec>,
46}
47
48impl Schema {
49 pub fn new(collection: impl Into<String>) -> Self {
50 Self {
51 collection: collection.into(),
52 fields: HashMap::new(),
53 }
54 }
55
56 pub fn field(mut self, name: impl Into<String>, crdt: CrdtType, tier: Tier) -> Self {
59 self.fields.insert(name.into(), FieldSpec { crdt, tier });
60 self
61 }
62
63 pub fn collection(&self) -> &str {
64 &self.collection
65 }
66
67 pub fn spec(&self, field: &str) -> Option<FieldSpec> {
68 self.fields.get(field).copied()
69 }
70}