Skip to main content

sealed_surface/
lens.rs

1//! Redaction applied **before** paint. Unlicensed scalars are dropped.
2
3use crate::model::{CellValue, ColumnId, Grid};
4
5/// Product-agnostic entitlement bucket. Hosts map their plans onto this.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum AccessTier {
9    /// No sample rows; every data cell is redacted.
10    Public,
11    /// First two data rows remain; the rest are redacted.
12    Trial,
13    /// All rows in the supplied grid (caller already filtered columns).
14    Subscription,
15    /// Same as [`AccessTier::Subscription`] in v0; column carveouts later.
16    Pro,
17    /// Open lens.
18    Enterprise,
19    /// Admin / operator: no redaction.
20    Open,
21}
22
23/// How redacted cells are painted (never with the original scalar).
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum RedactionStyle {
26    /// Four middle-dot glyphs.
27    Dots,
28    /// The word `LOCK`.
29    Lock,
30}
31
32impl RedactionStyle {
33    pub(crate) fn glyph_str(self) -> &'static str {
34        match self {
35            Self::Dots => "....",
36            Self::Lock => "LOCK",
37        }
38    }
39}
40
41/// Row/column policy applied to a [`Grid`] before raster.
42#[derive(Debug, Clone, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct LicenseLens {
45    /// Inclusive count of leading data rows that keep their values.
46    /// `0` redacts every row. `usize::MAX` redacts none (column filter still applies).
47    pub sample_clear_rows: usize,
48    /// When `Some`, columns not listed become [`CellValue::Redacted`] in every row.
49    pub allowed_columns: Option<Vec<ColumnId>>,
50    /// Placeholder paint for redacted cells.
51    pub style: RedactionStyle,
52}
53
54impl LicenseLens {
55    /// Lens for a named tier.
56    #[must_use]
57    pub fn from_tier(tier: AccessTier) -> Self {
58        let sample_clear_rows = match tier {
59            AccessTier::Public => 0,
60            AccessTier::Trial => 2,
61            AccessTier::Subscription
62            | AccessTier::Pro
63            | AccessTier::Enterprise
64            | AccessTier::Open => usize::MAX,
65        };
66        Self {
67            sample_clear_rows,
68            allowed_columns: None,
69            style: RedactionStyle::Dots,
70        }
71    }
72
73    /// No redaction.
74    #[must_use]
75    pub fn open() -> Self {
76        Self::from_tier(AccessTier::Open)
77    }
78
79    /// Restrict to these columns (others become [`CellValue::Redacted`]).
80    #[must_use]
81    pub fn with_allowed_columns(mut self, ids: Vec<ColumnId>) -> Self {
82        self.allowed_columns = Some(ids);
83        self
84    }
85
86    /// Placeholder glyphs for redacted cells.
87    #[must_use]
88    pub fn with_style(mut self, style: RedactionStyle) -> Self {
89        self.style = style;
90        self
91    }
92
93    fn column_allowed(&self, id: ColumnId) -> bool {
94        match &self.allowed_columns {
95            None => true,
96            Some(ids) => ids.contains(&id),
97        }
98    }
99
100    fn cell_clear(&self, row_index: usize, col: ColumnId) -> bool {
101        row_index < self.sample_clear_rows && self.column_allowed(col)
102    }
103
104    /// Return a grid whose unlicensed cells are [`CellValue::Redacted`].
105    /// Original scalars for those cells are not cloned or retained.
106    #[must_use]
107    pub fn apply(&self, grid: &Grid) -> Grid {
108        let ncols = grid.columns.len();
109        let mut rows = Vec::with_capacity(grid.rows.len());
110        for (ri, row) in grid.rows.iter().enumerate() {
111            let mut cells = Vec::with_capacity(ncols);
112            for ci in 0..ncols {
113                let col_id = grid.columns[ci].id;
114                if self.cell_clear(ri, col_id) {
115                    if let Some(cell) = row.cells.get(ci) {
116                        cells.push(cell.clone());
117                    } else {
118                        cells.push(CellValue::Redacted);
119                    }
120                } else {
121                    cells.push(CellValue::Redacted);
122                }
123            }
124            rows.push(crate::model::Row { id: row.id, cells });
125        }
126        Grid {
127            columns: grid.columns.clone(),
128            rows,
129        }
130    }
131
132    /// Drop unlicensed **points** (index treated as a row). Original y is not kept.
133    #[must_use]
134    pub fn apply_series(&self, series: &[crate::chart::Series]) -> Vec<crate::chart::Series> {
135        series
136            .iter()
137            .map(|s| crate::chart::Series {
138                id: s.id,
139                name: s.name.clone(),
140                points: s
141                    .points
142                    .iter()
143                    .enumerate()
144                    .filter(|(i, _)| *i < self.sample_clear_rows)
145                    .map(|(_, p)| *p)
146                    .collect(),
147            })
148            .collect()
149    }
150}
151
152impl Default for LicenseLens {
153    fn default() -> Self {
154        Self::from_tier(AccessTier::Public)
155    }
156}