Cotton code
60 lines · 2.3 KB · RustRaw
//! A table's `seal` block (sealing plan §2.2): which columns stay in the clear//! on the object, and which sealed columns make blind tokens.//!//! ```json//! "seal": { "clear": ["sent_at","thread_id"], "blind": ["body"], "blind_exact": ["external_id"],//!           "blind_bytes": { "body": 2048 } }//! ```//!//! Every column NOT in `clear` is sealed, so a column added later is sealed//! unless someone names it (fail closed). `blind` and `blind_exact` name sealed//! columns: a clear column needs no token. `blind_bytes` caps how much of a//! `blind` column is indexed, from its start (an email's body: its first 2 KB,//! sealing plan Q12); a column it does not name is indexed whole.use std::collections::BTreeMap;use serde::{Deserialize, Serialize};#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]#[serde(deny_unknown_fields)]pub struct SealSpec {    /// Columns kept in the clear in `rows.data`.    #[serde(default)]    pub clear: Vec<String>,    /// Sealed text columns indexed by word and word start.    #[serde(default, skip_serializing_if = "Vec::is_empty")]    pub blind: Vec<String>,    /// Sealed columns indexed by exact value (ids, handles, addresses).    #[serde(default, skip_serializing_if = "Vec::is_empty")]    pub blind_exact: Vec<String>,    /// The most bytes of a `blind` column indexed, from its start, by column.    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]    pub blind_bytes: BTreeMap<String, usize>,}impl SealSpec {    /// Is `column` sealed under this block?    pub fn seals(&self, column: &str) -> bool {        column != "id" && !self.clear.iter().any(|c| c == column)    }    /// `text` as `column` indexes it: whole, or its first `blind_bytes`    /// bytes cut back to a character boundary.    pub fn indexed<'a>(&self, column: &str, text: &'a str) -> &'a str {        match self.blind_bytes.get(column) {            Some(&cap) if text.len() > cap => {                let mut end = cap;                while !text.is_char_boundary(end) {                    end -= 1;                }                &text[..end]            }            _ => text,        }    }}/// Column keys a definition may not declare on a sealed table: the sealer/// writes them.pub const RESERVED_COLUMNS: [&str; 3] = ["sealed_enc", "dek_enc", "seal_v"];