hydrate_schema/schema/
enum.rs

1use crate::SchemaFingerprint;
2use std::ops::Deref;
3use std::sync::Arc;
4use uuid::Uuid;
5
6#[derive(Debug)]
7pub struct SchemaEnumSymbol {
8    name: String,
9    symbol_uuid: Uuid,
10    aliases: Box<[String]>,
11}
12
13impl SchemaEnumSymbol {
14    pub fn new(
15        name: String,
16        symbol_uuid: Uuid,
17        aliases: Box<[String]>,
18    ) -> Self {
19        SchemaEnumSymbol {
20            name,
21            symbol_uuid,
22            aliases,
23        }
24    }
25
26    pub fn name(&self) -> &str {
27        &self.name
28    }
29
30    pub fn symbol_uuid(&self) -> Uuid {
31        self.symbol_uuid
32    }
33
34    pub fn aliases(&self) -> &[String] {
35        &self.aliases
36    }
37}
38
39#[derive(Debug)]
40pub struct SchemaEnumInner {
41    name: String,
42    type_uuid: Uuid,
43    fingerprint: SchemaFingerprint,
44    aliases: Box<[String]>,
45    symbols: Box<[SchemaEnumSymbol]>,
46}
47
48#[derive(Clone, Debug)]
49pub struct SchemaEnum {
50    inner: Arc<SchemaEnumInner>,
51}
52
53impl Deref for SchemaEnum {
54    type Target = SchemaEnumInner;
55
56    fn deref(&self) -> &Self::Target {
57        &*self.inner
58    }
59}
60
61impl SchemaEnum {
62    pub fn new(
63        name: String,
64        type_uuid: Uuid,
65        fingerprint: SchemaFingerprint,
66        aliases: Box<[String]>,
67        symbols: Box<[SchemaEnumSymbol]>,
68    ) -> Self {
69        assert!(!symbols.is_empty());
70
71        // Check names are unique
72        for i in 0..symbols.len() {
73            for j in 0..i {
74                assert_ne!(symbols[i].name, symbols[j].name);
75            }
76        }
77
78        let inner = SchemaEnumInner {
79            name,
80            type_uuid,
81            fingerprint,
82            aliases,
83            symbols,
84        };
85
86        SchemaEnum {
87            inner: Arc::new(inner),
88        }
89    }
90
91    pub fn name(&self) -> &str {
92        &self.name
93    }
94
95    pub fn type_uuid(&self) -> Uuid {
96        self.type_uuid
97    }
98
99    pub fn aliases(&self) -> &[String] {
100        &self.aliases
101    }
102
103    pub fn symbols(&self) -> &[SchemaEnumSymbol] {
104        &*self.symbols
105    }
106
107    pub fn default_value(&self) -> &SchemaEnumSymbol {
108        &self.symbols[0]
109    }
110
111    pub fn fingerprint(&self) -> SchemaFingerprint {
112        self.fingerprint
113    }
114
115    pub fn find_symbol_from_name(
116        &self,
117        name: &str,
118    ) -> Option<&SchemaEnumSymbol> {
119        for symbol in &*self.symbols {
120            if symbol.name() == name {
121                return Some(symbol);
122            }
123
124            for alias in symbol.aliases() {
125                if alias == name {
126                    return Some(symbol);
127                }
128            }
129        }
130
131        None
132    }
133
134    pub fn find_symbol_from_uuid(
135        &self,
136        uuid: Uuid,
137    ) -> Option<&SchemaEnumSymbol> {
138        self.symbols.iter().find(|x| x.symbol_uuid == uuid)
139    }
140}