Skip to main content

sim_kernel/catalog/
schema.rs

1use crate::Symbol;
2
3/// Write policy governing how rows of a catalog table may change.
4///
5/// See the README section "Contract: registry catalog substrate" for how the
6/// registry's tables map onto these policies.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum CatalogWritePolicy {
9    /// Insert, replace, and delete are all permitted.
10    Mutable,
11    /// A key may be inserted once and then never changed or deleted.
12    Sealed,
13    /// Keys may be inserted but never changed or deleted.
14    AppendOnly,
15    /// Direct writes fail; rows are derived from other tables.
16    Derived,
17}
18
19/// Specification of one catalog table: its name, write policy, owner, and field
20/// constraints.
21///
22/// # Examples
23///
24/// ```
25/// # use sim_kernel::catalog::{CatalogTableSpec, CatalogWritePolicy};
26/// # use sim_kernel::Symbol;
27/// let spec = CatalogTableSpec::new(Symbol::new("registry/libs"), CatalogWritePolicy::Mutable)
28///     .with_required_fields(vec![Symbol::new("manifest")]);
29/// assert_eq!(spec.policy, CatalogWritePolicy::Mutable);
30/// assert_eq!(spec.required_fields, vec![Symbol::new("manifest")]);
31/// ```
32#[derive(Clone, Debug, PartialEq, Eq)]
33pub struct CatalogTableSpec {
34    /// Open symbol naming the table.
35    pub name: Symbol,
36    /// Write policy enforced on the table's rows.
37    pub policy: CatalogWritePolicy,
38    /// Optional owning library symbol.
39    pub owner: Option<Symbol>,
40    /// Fields every row of the table must carry.
41    pub required_fields: Vec<Symbol>,
42    /// Field tuples that must be unique across the table's rows.
43    pub unique_fields: Vec<Vec<Symbol>>,
44}
45
46impl CatalogTableSpec {
47    /// Creates a spec with the given name and policy and no further constraints.
48    pub fn new(name: Symbol, policy: CatalogWritePolicy) -> Self {
49        Self {
50            name,
51            policy,
52            owner: None,
53            required_fields: Vec::new(),
54            unique_fields: Vec::new(),
55        }
56    }
57
58    /// Returns the spec with its required fields replaced.
59    pub fn with_required_fields(mut self, fields: Vec<Symbol>) -> Self {
60        self.required_fields = fields;
61        self
62    }
63}