1use crate::model::{CellValue, ColumnId, Grid};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum AccessTier {
9 Public,
11 Trial,
13 Subscription,
15 Pro,
17 Enterprise,
19 Open,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum RedactionStyle {
26 Dots,
28 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#[derive(Debug, Clone, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct LicenseLens {
45 pub sample_clear_rows: usize,
48 pub allowed_columns: Option<Vec<ColumnId>>,
50 pub style: RedactionStyle,
52}
53
54impl LicenseLens {
55 #[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 #[must_use]
75 pub fn open() -> Self {
76 Self::from_tier(AccessTier::Open)
77 }
78
79 #[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 #[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 #[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 #[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}