tabled/settings/location/
by_column_name.rs1use crate::{
2 grid::config::Entity,
3 grid::records::{ExactRecords, PeekableRecords, Records},
4 settings::location::Location,
5 settings::object::Object,
6};
7
8#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
13pub struct ByColumnName<S>(S);
14
15impl<S> ByColumnName<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 ByColumnName<S>
26where
27 S: AsRef<str>,
28 R: Records + ExactRecords + PeekableRecords,
29{
30 type Coordinate = usize;
31 type IntoIter = Vec<usize>;
32
33 fn locate(&mut self, records: &R) -> Self::IntoIter {
34 (0..records.count_columns())
36 .filter(|col| records.get_text((0, *col).into()) == self.0.as_ref())
37 .collect::<Vec<_>>()
38 }
39}
40
41impl<S, R> Object<R> for ByColumnName<S>
42where
43 S: AsRef<str>,
44 R: Records + PeekableRecords + ExactRecords,
45{
46 type Iter = std::vec::IntoIter<Entity>;
47
48 fn cells(&self, records: &R) -> Self::Iter {
49 (0..records.count_columns())
51 .filter(|col| records.get_text((0, *col).into()) == self.0.as_ref())
52 .map(Entity::Column)
53 .collect::<Vec<_>>()
54 .into_iter()
55 }
56}