icydb_core/model/
index.rs1use std::fmt::{self, Display};
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct IndexModel {
14 pub name: &'static str,
16 pub store: &'static str,
17 pub fields: &'static [&'static str],
18 pub 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]
38 pub fn is_prefix_of(&self, other: &Self) -> bool {
40 self.fields.len() < other.fields.len() && other.fields.starts_with(self.fields)
41 }
42}
43
44impl Display for IndexModel {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 let fields = self.fields.join(", ");
47
48 if self.unique {
49 write!(f, "{}: UNIQUE {}({})", self.name, self.store, fields)
50 } else {
51 write!(f, "{}: {}({})", self.name, self.store, fields)
52 }
53 }
54}