tabled/settings/location/
by_content.rs

1use crate::{
2    grid::config::Entity,
3    grid::{
4        config::Position,
5        records::{ExactRecords, PeekableRecords, Records},
6    },
7    settings::location::Location,
8    settings::object::Object,
9};
10
11/// The structure is an implementation of [`Location`] to search for cells with a given content.
12#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
13pub struct ByContent<S>(S);
14
15impl<S> ByContent<S> {
16    /// Constructs a new object of the structure.
17    pub fn new(text: S) -> Self
18    where
19        S: AsRef<str>,
20    {
21        Self(text)
22    }
23}
24
25impl<R, S> Location<R> for ByContent<S>
26where
27    S: AsRef<str>,
28    R: Records + ExactRecords + PeekableRecords,
29{
30    type Coordinate = Position;
31    type IntoIter = Vec<Position>;
32
33    fn locate(&mut self, records: &R) -> Self::IntoIter {
34        // todo: can be optimized by creating Iterator
35        let text = self.0.as_ref();
36
37        let mut out = vec![];
38        for row in 0..records.count_rows() {
39            for col in 0..records.count_columns() {
40                let cell = records.get_text((row, col).into());
41                if cell.eq(text) {
42                    out.push((row, col).into());
43                }
44            }
45        }
46
47        out
48    }
49}
50
51impl<S, R> Object<R> for ByContent<S>
52where
53    S: AsRef<str>,
54    R: Records + PeekableRecords + ExactRecords,
55{
56    type Iter = std::vec::IntoIter<Entity>;
57
58    fn cells(&self, records: &R) -> Self::Iter {
59        // todo: can be optimized by creating Iterator
60        let text = self.0.as_ref();
61
62        let mut out = vec![];
63        for row in 0..records.count_rows() {
64            for col in 0..records.count_columns() {
65                let cell = records.get_text((row, col).into());
66                if cell.eq(text) {
67                    out.push(Entity::Cell(row, col));
68                }
69            }
70        }
71
72        out.into_iter()
73    }
74}