tabled/settings/format/
format_positioned.rs

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