sealed-surface 0.2.0

Non-extractable data surface: tables and timeseries as glyphs, never DOM text
Documentation
//! Redaction applied **before** paint. Unlicensed scalars are dropped.

use crate::model::{CellValue, ColumnId, Grid};

/// Product-agnostic entitlement bucket. Hosts map their plans onto this.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AccessTier {
    /// No sample rows; every data cell is redacted.
    Public,
    /// First two data rows remain; the rest are redacted.
    Trial,
    /// All rows in the supplied grid (caller already filtered columns).
    Subscription,
    /// Same as [`AccessTier::Subscription`] in v0; column carveouts later.
    Pro,
    /// Open lens.
    Enterprise,
    /// Admin / operator: no redaction.
    Open,
}

/// How redacted cells are painted (never with the original scalar).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RedactionStyle {
    /// Four middle-dot glyphs.
    Dots,
    /// The word `LOCK`.
    Lock,
}

impl RedactionStyle {
    pub(crate) fn glyph_str(self) -> &'static str {
        match self {
            Self::Dots => "....",
            Self::Lock => "LOCK",
        }
    }
}

/// Row/column policy applied to a [`Grid`] before raster.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct LicenseLens {
    /// Inclusive count of leading data rows that keep their values.
    /// `0` redacts every row. `usize::MAX` redacts none (column filter still applies).
    pub sample_clear_rows: usize,
    /// When `Some`, columns not listed become [`CellValue::Redacted`] in every row.
    pub allowed_columns: Option<Vec<ColumnId>>,
    /// Placeholder paint for redacted cells.
    pub style: RedactionStyle,
}

impl LicenseLens {
    /// Lens for a named tier.
    #[must_use]
    pub fn from_tier(tier: AccessTier) -> Self {
        let sample_clear_rows = match tier {
            AccessTier::Public => 0,
            AccessTier::Trial => 2,
            AccessTier::Subscription
            | AccessTier::Pro
            | AccessTier::Enterprise
            | AccessTier::Open => usize::MAX,
        };
        Self {
            sample_clear_rows,
            allowed_columns: None,
            style: RedactionStyle::Dots,
        }
    }

    /// No redaction.
    #[must_use]
    pub fn open() -> Self {
        Self::from_tier(AccessTier::Open)
    }

    /// Restrict to these columns (others become [`CellValue::Redacted`]).
    #[must_use]
    pub fn with_allowed_columns(mut self, ids: Vec<ColumnId>) -> Self {
        self.allowed_columns = Some(ids);
        self
    }

    /// Placeholder glyphs for redacted cells.
    #[must_use]
    pub fn with_style(mut self, style: RedactionStyle) -> Self {
        self.style = style;
        self
    }

    fn column_allowed(&self, id: ColumnId) -> bool {
        match &self.allowed_columns {
            None => true,
            Some(ids) => ids.contains(&id),
        }
    }

    fn cell_clear(&self, row_index: usize, col: ColumnId) -> bool {
        row_index < self.sample_clear_rows && self.column_allowed(col)
    }

    /// Return a grid whose unlicensed cells are [`CellValue::Redacted`].
    /// Original scalars for those cells are not cloned or retained.
    #[must_use]
    pub fn apply(&self, grid: &Grid) -> Grid {
        let ncols = grid.columns.len();
        let mut rows = Vec::with_capacity(grid.rows.len());
        for (ri, row) in grid.rows.iter().enumerate() {
            let mut cells = Vec::with_capacity(ncols);
            for ci in 0..ncols {
                let col_id = grid.columns[ci].id;
                if self.cell_clear(ri, col_id) {
                    if let Some(cell) = row.cells.get(ci) {
                        cells.push(cell.clone());
                    } else {
                        cells.push(CellValue::Redacted);
                    }
                } else {
                    cells.push(CellValue::Redacted);
                }
            }
            rows.push(crate::model::Row { id: row.id, cells });
        }
        Grid {
            columns: grid.columns.clone(),
            rows,
        }
    }

    /// Drop unlicensed **points** (index treated as a row). Original y is not kept.
    #[must_use]
    pub fn apply_series(&self, series: &[crate::chart::Series]) -> Vec<crate::chart::Series> {
        series
            .iter()
            .map(|s| crate::chart::Series {
                id: s.id,
                name: s.name.clone(),
                points: s
                    .points
                    .iter()
                    .enumerate()
                    .filter(|(i, _)| *i < self.sample_clear_rows)
                    .map(|(_, p)| *p)
                    .collect(),
            })
            .collect()
    }
}

impl Default for LicenseLens {
    fn default() -> Self {
        Self::from_tier(AccessTier::Public)
    }
}