icydb_core/
index.rs

1use std::fmt::{self, Display};
2
3///
4/// IndexSpec
5/// Runtime-only descriptor for an index used by the executor and stores.
6/// Keeps core decoupled from the schema `Index` shape.
7///
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub struct IndexSpec {
11    pub store: &'static str,
12    pub fields: &'static [&'static str],
13    pub unique: bool,
14}
15
16impl IndexSpec {
17    #[must_use]
18    pub const fn new(store: &'static str, fields: &'static [&'static str], unique: bool) -> Self {
19        Self {
20            store,
21            fields,
22            unique,
23        }
24    }
25
26    #[must_use]
27    /// Whether this index's field prefix matches the start of another index.
28    pub fn is_prefix_of(&self, other: &Self) -> bool {
29        self.fields.len() < other.fields.len() && other.fields.starts_with(self.fields)
30    }
31}
32
33impl Display for IndexSpec {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        let fields = self.fields.join(", ");
36
37        if self.unique {
38            write!(f, "UNIQUE {}({})", self.store, fields)
39        } else {
40            write!(f, "{}({})", self.store, fields)
41        }
42    }
43}