toasty_core/schema/db/
schema.rs

1use super::{
2    Column, ColumnId, DiffContext, Index, IndexId, RenameHints, Table, TableId, TablesDiff,
3};
4
5#[derive(Debug, Default, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct Schema {
8    pub tables: Vec<Table>,
9}
10
11impl Schema {
12    pub fn column(&self, id: impl Into<ColumnId>) -> &Column {
13        let id = id.into();
14        self.table(id.table)
15            .columns
16            .get(id.index)
17            .expect("invalid column ID")
18    }
19
20    pub fn column_mut(&mut self, id: impl Into<ColumnId>) -> &mut Column {
21        let id = id.into();
22        self.table_mut(id.table)
23            .columns
24            .get_mut(id.index)
25            .expect("invalid column ID")
26    }
27
28    // NOTE: this is unlikely to confuse users given the context.
29    #[allow(clippy::should_implement_trait)]
30    pub fn index(&self, id: IndexId) -> &Index {
31        self.table(id.table)
32            .indices
33            .get(id.index)
34            .expect("invalid index ID")
35    }
36
37    // NOTE: this is unlikely to confuse users given the context.
38    #[allow(clippy::should_implement_trait)]
39    pub fn index_mut(&mut self, id: IndexId) -> &mut Index {
40        self.table_mut(id.table)
41            .indices
42            .get_mut(id.index)
43            .expect("invalid index ID")
44    }
45
46    pub fn table(&self, id: impl Into<TableId>) -> &Table {
47        self.tables.get(id.into().0).expect("invalid table ID")
48    }
49
50    pub fn table_mut(&mut self, id: impl Into<TableId>) -> &mut Table {
51        self.tables.get_mut(id.into().0).expect("invalid table ID")
52    }
53}
54
55pub struct SchemaDiff<'a> {
56    previous: &'a Schema,
57    next: &'a Schema,
58    tables: TablesDiff<'a>,
59}
60
61impl<'a> SchemaDiff<'a> {
62    pub fn from(from: &'a Schema, to: &'a Schema, rename_hints: &'a RenameHints) -> Self {
63        let cx = &DiffContext::new(from, to, rename_hints);
64        Self {
65            previous: from,
66            next: to,
67            tables: TablesDiff::from(cx, &from.tables, &to.tables),
68        }
69    }
70
71    pub fn tables(&self) -> &TablesDiff<'a> {
72        &self.tables
73    }
74
75    pub fn is_empty(&self) -> bool {
76        self.tables.is_empty()
77    }
78
79    pub fn previous(&self) -> &'a Schema {
80        self.previous
81    }
82
83    pub fn next(&self) -> &'a Schema {
84        self.next
85    }
86}