toasty_core/schema/app/
index.rs

1use super::{FieldId, ModelId};
2use crate::schema::db::{IndexOp, IndexScope};
3
4#[derive(Debug, Clone)]
5pub struct Index {
6    /// Uniquely identifies the model index within the schema
7    pub id: IndexId,
8
9    /// Fields included in the index.
10    pub fields: Vec<IndexField>,
11
12    /// When `true`, indexed entries are unique
13    pub unique: bool,
14
15    /// When true, the index is the primary key
16    pub primary_key: bool,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub struct IndexId {
21    pub model: ModelId,
22    pub index: usize,
23}
24
25#[derive(Debug, Copy, Clone)]
26pub struct IndexField {
27    /// The field being indexed
28    pub field: FieldId,
29
30    /// The comparison operation used to index the field
31    pub op: IndexOp,
32
33    /// Scope of the index
34    pub scope: IndexScope,
35}
36
37impl Index {
38    pub fn partition_fields(&self) -> &[IndexField] {
39        let i = self.index_of_first_local_field();
40        &self.fields[0..i]
41    }
42
43    pub fn local_fields(&self) -> &[IndexField] {
44        let i = self.index_of_first_local_field();
45        &self.fields[i..]
46    }
47
48    fn index_of_first_local_field(&self) -> usize {
49        self.fields
50            .iter()
51            .position(|field| field.scope.is_local())
52            .unwrap_or(self.fields.len())
53    }
54}