Skip to main content

icydb_schema/node/
index.rs

1use crate::prelude::*;
2use std::{
3    fmt::{self, Display},
4    ops::Not,
5};
6
7///
8/// Index
9///
10
11#[derive(Clone, Debug, Serialize)]
12pub struct Index {
13    pub fields: &'static [&'static str],
14
15    #[serde(default, skip_serializing_if = "Not::not")]
16    pub unique: bool,
17}
18
19impl Index {
20    #[must_use]
21    pub fn is_prefix_of(&self, other: &Self) -> bool {
22        self.fields.len() < other.fields.len() && other.fields.starts_with(self.fields)
23    }
24}
25
26impl Display for Index {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        let fields = self.fields.join(", ");
29
30        if self.unique {
31            write!(f, "UNIQUE ({fields})")
32        } else {
33            write!(f, "({fields})")
34        }
35    }
36}
37
38impl MacroNode for Index {
39    fn as_any(&self) -> &dyn std::any::Any {
40        self
41    }
42}
43
44impl ValidateNode for Index {}
45
46impl VisitableNode for Index {
47    fn route_key(&self) -> String {
48        self.fields.join(", ")
49    }
50}