| 1 | use crate::{ |
| 2 | grid::config::Entity, |
| 3 | grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut}, |
| 4 | settings::{CellOption, TableOption}, |
| 5 | }; |
| 6 | |
| 7 | /// [`FormatContentPositioned`] is like a [`FormatContent`] an abstraction over a function you can use against a cell. |
| 8 | /// |
| 9 | /// It different from [`FormatContent`] that it provides a row and column index. |
| 10 | /// |
| 11 | /// [`FormatContent`]: crate::settings::format::FormatContent |
| 12 | #[derive (Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 13 | pub struct FormatContentPositioned<F>(F); |
| 14 | |
| 15 | impl<F> FormatContentPositioned<F> { |
| 16 | pub(crate) fn new(f: F) -> Self { |
| 17 | Self(f) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<F, R, D, C> TableOption<R, C, D> for FormatContentPositioned<F> |
| 22 | where |
| 23 | F: FnMut(&str, (usize, usize)) -> String, |
| 24 | R: Records + ExactRecords + PeekableRecords + RecordsMut<String>, |
| 25 | { |
| 26 | fn change(self, records: &mut R, cfg: &mut C, _: &mut D) { |
| 27 | CellOption::change(self, records, cfg, Entity::Global); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<F, R, C> CellOption<R, C> for FormatContentPositioned<F> |
| 32 | where |
| 33 | F: FnMut(&str, (usize, usize)) -> String, |
| 34 | R: Records + ExactRecords + PeekableRecords + RecordsMut<String>, |
| 35 | { |
| 36 | fn change(mut self, records: &mut R, _: &mut C, entity: Entity) { |
| 37 | let count_rows: usize = records.count_rows(); |
| 38 | let count_cols: usize = records.count_columns(); |
| 39 | |
| 40 | for pos: (usize, usize) in entity.iter(count_rows, count_cols) { |
| 41 | let is_valid_pos: bool = pos.0 < count_rows && pos.1 < count_cols; |
| 42 | if !is_valid_pos { |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | let content: &str = records.get_text(pos); |
| 47 | let content: String = (self.0)(content, pos); |
| 48 | records.set(pos, text:content); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |