icydb_core/model/
index.rs1use std::fmt::{self, Display};
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct IndexModel {
14 name: &'static str,
16 store: &'static str,
17 fields: &'static [&'static str],
18 unique: bool,
19}
20
21impl IndexModel {
22 #[must_use]
23 pub const fn new(
24 name: &'static str,
25 store: &'static str,
26 fields: &'static [&'static str],
27 unique: bool,
28 ) -> Self {
29 Self {
30 name,
31 store,
32 fields,
33 unique,
34 }
35 }
36
37 #[must_use]
39 pub const fn name(&self) -> &'static str {
40 self.name
41 }
42
43 #[must_use]
45 pub const fn store(&self) -> &'static str {
46 self.store
47 }
48
49 #[must_use]
51 pub const fn fields(&self) -> &'static [&'static str] {
52 self.fields
53 }
54
55 #[must_use]
57 pub const fn is_unique(&self) -> bool {
58 self.unique
59 }
60
61 #[must_use]
62 pub fn is_prefix_of(&self, other: &Self) -> bool {
64 self.fields().len() < other.fields().len() && other.fields().starts_with(self.fields())
65 }
66}
67
68impl Display for IndexModel {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 let fields = self.fields().join(", ");
71
72 if self.is_unique() {
73 write!(f, "{}: UNIQUE {}({})", self.name(), self.store(), fields)
74 } else {
75 write!(f, "{}: {}({})", self.name(), self.store(), fields)
76 }
77 }
78}