1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! 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)
}
}