tabled/settings/location/
by_content.rs1use 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#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
13pub struct ByContent<S>(S);
14
15impl<S> ByContent<S> {
16 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 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 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}