Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

3.8 KB
Raw Blame
//! The client vault datamodel registry.
//!
//! The data (table list, keys, scoping, bucket layout, format sentinels) is generated from the
//! TypeScript source of truth in core/models/src/vault/VaultTableRegistry.ts; see generated.rs.
//! This module owns the [`TableConfig`] type the generated data instantiates.

mod generated;

pub use generated::*;

/// The comparison key of an id (GUIDs compare case-insensitively).
pub fn id_key(id: &str) -> String {
    id.trim().to_ascii_lowercase()
}

/// Whether two ids name the same row.
pub fn ids_equal(a: &str, b: &str) -> bool {
    a.trim().eq_ignore_ascii_case(b.trim())
}

/// A system field and the metadata every client reads it with.
#[derive(Debug, Clone)]
pub struct SystemField {
    pub key: &'static str,
    pub field_type: &'static str,
    pub is_hidden: bool,
    pub is_multi_value: bool,
    pub enable_history: bool,
    pub default_display_order: i64,
}

/// The system field with this key (case-insensitive), or None for a key this build does not know.
pub fn system_field(key: &str) -> Option<&'static SystemField> {
    SYSTEM_FIELDS.iter().find(|field| field.key.eq_ignore_ascii_case(key))
}

/// A column whose bytes are extracted into a content-addressed blob.
#[derive(Debug, Clone)]
pub struct BlobColumn {
    pub table: &'static str,
    /// The column holding the bytes locally and the blob reference in a manifest.
    pub column: &'static str,
    /// The local column holding the blob's hash, so a row whose bytes are not loaded still knows its blob.
    pub hash_column: &'static str,
    /// The label reported to the server on upload.
    pub kind: &'static str,
}

/// A syncable table.
#[derive(Debug, Clone)]
pub struct TableConfig {
    pub name: &'static str,
    /// Merge match columns in the manifest shape; empty means rows match on their identity columns.
    pub canonical_key_columns: &'static [&'static str],
    /// The columns that name a row within its manifest, `["Id"]` unless overridden.
    pub primary_key_columns: &'static [&'static str],
    /// Whether rows are namespaced per manifest.
    pub manifest_scoped: bool,
    /// Whether rows are child rows of an Item row (re-stamped with it, cascaded on delete).
    pub item_child: bool,
}

impl TableConfig {
    pub const fn new(name: &'static str) -> Self {
        Self {
            name,
            canonical_key_columns: &[],
            primary_key_columns: &["Id"],
            manifest_scoped: false,
            item_child: false,
        }
    }

    pub const fn with_canonical_key(mut self, columns: &'static [&'static str]) -> Self {
        self.canonical_key_columns = columns;
        self
    }

    pub const fn with_primary_key(mut self, columns: &'static [&'static str]) -> Self {
        self.primary_key_columns = columns;
        self
    }

    pub const fn manifest_scoped(mut self) -> Self {
        self.manifest_scoped = true;
        self
    }

    pub const fn item_child(mut self) -> Self {
        self.item_child = true;
        self
    }

    /// The column of an item-child table that carries the owning item's id: `Id` for ItemStats, `ItemId` elsewhere.
    pub fn item_ref_column(&self) -> &'static str {
        if self.name == names::ITEM_STATS_TABLE { names::ID_COL } else { names::ITEM_ID_COL }
    }

    /// The columns that identify one row: the primary key columns, behind `ManifestId` for a manifest-scoped table.
    pub fn identity_columns(&self) -> Vec<&'static str> {
        if self.manifest_scoped {
            let mut columns = Vec::with_capacity(1 + self.primary_key_columns.len());
            columns.push(MANIFEST_ID_COL);
            columns.extend_from_slice(self.primary_key_columns);
            columns
        } else {
            self.primary_key_columns.to_vec()
        }
    }
}

#[cfg(test)]
mod tests;