tabled/settings/location/
locator.rs

1use super::{by_value::ByValue, ByColumnName, ByCondition, ByContent};
2
3/// An abstract factory for locations, to be used to find different things on the table.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
5pub struct Locator;
6
7impl Locator {
8    /// Constructs a new location searcher for a cells with a given content.
9    ///
10    /// # Example
11    ///
12    /// ```
13    /// use tabled::{
14    ///     settings::location::Locator,
15    ///     assert::assert_table,
16    ///     Table, Tabled,
17    /// };
18    ///
19    /// #[derive(Tabled)]
20    /// struct Reading {
21    ///     link: &'static str,
22    ///     comment: &'static str,
23    /// }
24    ///
25    /// let data = [
26    ///     Reading { link: "https://www.gnu.org/software/grub/manual/multiboot/multiboot.html", comment: "todo" },
27    ///     Reading { link: "https://wiki.debian.org/initramfs", comment: "todo" },
28    ///     Reading { link: "http://jdebp.uk/FGA/efi-boot-process.html", comment: "..." },
29    ///     Reading { link: "https://wiki.debian.org/UEFI", comment: "" },
30    /// ];
31    ///
32    /// let mut table = Table::new(data);
33    /// table.modify(Locator::content("todo"), "todo-soon");
34    ///
35    /// assert_table!(
36    ///     table,
37    ///     "+-------------------------------------------------------------------+-----------+"
38    ///     "| link                                                              | comment   |"
39    ///     "+-------------------------------------------------------------------+-----------+"
40    ///     "| https://www.gnu.org/software/grub/manual/multiboot/multiboot.html | todo-soon |"
41    ///     "+-------------------------------------------------------------------+-----------+"
42    ///     "| https://wiki.debian.org/initramfs                                 | todo-soon |"
43    ///     "+-------------------------------------------------------------------+-----------+"
44    ///     "| http://jdebp.uk/FGA/efi-boot-process.html                         | ...       |"
45    ///     "+-------------------------------------------------------------------+-----------+"
46    ///     "| https://wiki.debian.org/UEFI                                      |           |"
47    ///     "+-------------------------------------------------------------------+-----------+"
48    /// );
49    /// ```
50    pub fn content<S>(text: S) -> ByContent<S>
51    where
52        S: AsRef<str>,
53    {
54        ByContent::new(text)
55    }
56
57    /// Constructs a new location searcher for a column by its header.
58    ///
59    /// # Example
60    ///
61    /// ```
62    /// use tabled::{
63    ///     settings::{location::Locator, Width},
64    ///     assert::assert_table,
65    ///     Table, Tabled,
66    /// };
67    ///
68    /// #[derive(Tabled)]
69    /// struct Reading {
70    ///     link: &'static str,
71    ///     comment: &'static str,
72    /// }
73    ///
74    /// let data = [
75    ///     Reading { link: "https://www.gnu.org/software/grub/manual/multiboot/multiboot.html", comment: "todo" },
76    ///     Reading { link: "https://wiki.debian.org/initramfs", comment: "todo" },
77    ///     Reading { link: "http://jdebp.uk/FGA/efi-boot-process.html", comment: "..." },
78    ///     Reading { link: "https://wiki.debian.org/UEFI", comment: "" },
79    /// ];
80    ///
81    /// let mut table = Table::new(data);
82    /// table.modify(Locator::column("link"), Width::truncate(10));
83    ///
84    /// assert_table!(
85    ///     table,
86    ///     "+------------+---------+"
87    ///     "| link       | comment |"
88    ///     "+------------+---------+"
89    ///     "| https://ww | todo    |"
90    ///     "+------------+---------+"
91    ///     "| https://wi | todo    |"
92    ///     "+------------+---------+"
93    ///     "| http://jde | ...     |"
94    ///     "+------------+---------+"
95    ///     "| https://wi |         |"
96    ///     "+------------+---------+"
97    /// );
98    /// ```
99    pub fn column<S>(text: S) -> ByColumnName<S>
100    where
101        S: AsRef<str>,
102    {
103        ByColumnName::new(text)
104    }
105
106    /// Constructs a new location searcher with a specified condition closure.
107    ///
108    /// Return `true` if it shall be included in output.
109    /// Otherwise return `false`.
110    ///  
111    /// # Example
112    ///
113    /// ```
114    /// use tabled::{
115    ///     settings::{location::Locator, Width},
116    ///     assert::assert_table,
117    ///     Table, Tabled,
118    /// };
119    ///
120    /// #[derive(Tabled)]
121    /// struct Reading {
122    ///     link: &'static str,
123    ///     comment: &'static str,
124    /// }
125    ///
126    /// let data = [
127    ///     Reading { link: "https://www.gnu.org/software/grub/manual/multiboot/multiboot.html", comment: "todo" },
128    ///     Reading { link: "https://wiki.debian.org/initramfs", comment: "todo" },
129    ///     Reading { link: "http://jdebp.uk/FGA/efi-boot-process.html", comment: "..." },
130    ///     Reading { link: "https://wiki.debian.org/UEFI", comment: "" },
131    /// ];
132    ///
133    /// let mut table = Table::new(data);
134    /// table.modify(Locator::by(|text| text.len() > 33), Width::truncate(33));
135    ///
136    /// assert_table!(
137    ///     table,
138    ///     "+-----------------------------------+---------+"
139    ///     "| link                              | comment |"
140    ///     "+-----------------------------------+---------+"
141    ///     "| https://www.gnu.org/software/grub | todo    |"
142    ///     "+-----------------------------------+---------+"
143    ///     "| https://wiki.debian.org/initramfs | todo    |"
144    ///     "+-----------------------------------+---------+"
145    ///     "| http://jdebp.uk/FGA/efi-boot-proc | ...     |"
146    ///     "+-----------------------------------+---------+"
147    ///     "| https://wiki.debian.org/UEFI      |         |"
148    ///     "+-----------------------------------+---------+"
149    /// );
150    /// ```
151    pub fn by<F>(condition: F) -> ByCondition<F>
152    where
153        F: Fn(&str) -> bool,
154    {
155        ByCondition::new(condition)
156    }
157
158    /// Constructs a new location searcher which finds only all values equal to the top.
159    ///
160    /// Return `true` if it shall be counted.
161    /// Otherwise return `false`.
162    ///
163    /// # Example
164    ///
165    /// ```
166    /// use tabled::{
167    ///     settings::{location::Locator, Width, Format},
168    ///     assert::assert_table,
169    ///     Table, Tabled,
170    /// };
171    ///
172    /// #[derive(Tabled)]
173    /// struct Reading {
174    ///     link: &'static str,
175    ///     comment: &'static str,
176    /// }
177    ///
178    /// let data = [
179    ///     Reading { link: "https://www.gnu.org/software/grub/manual/multiboot/multiboot.html", comment: "todo" },
180    ///     Reading { link: "https://wiki.debian.org/initramfs", comment: "todo" },
181    ///     Reading { link: "http://jdebp.uk/FGA/efi-boot-process.html", comment: "..." },
182    ///     Reading { link: "https://wiki.debian.org/UEFI", comment: "" },
183    /// ];
184    ///
185    /// let mut table = Table::new(data);
186    /// table.modify(
187    ///     Locator::value(Locator::column("link"), |link1, link2| link1.len() > link2.len()),
188    ///     Format::content(|s| format!("[ {s} ]")),
189    /// );
190    ///
191    /// assert_table!(
192    ///     table,
193    ///     "+-----------------------------------------------------------------------+---------+"
194    ///     "| link                                                                  | comment |"
195    ///     "+-----------------------------------------------------------------------+---------+"
196    ///     "| [ https://www.gnu.org/software/grub/manual/multiboot/multiboot.html ] | todo    |"
197    ///     "+-----------------------------------------------------------------------+---------+"
198    ///     "| https://wiki.debian.org/initramfs                                     | todo    |"
199    ///     "+-----------------------------------------------------------------------+---------+"
200    ///     "| http://jdebp.uk/FGA/efi-boot-process.html                             | ...     |"
201    ///     "+-----------------------------------------------------------------------+---------+"
202    ///     "| https://wiki.debian.org/UEFI                                          |         |"
203    ///     "+-----------------------------------------------------------------------+---------+"
204    /// );
205    /// ```
206    pub fn value<O, F>(search: O, condition: F) -> ByValue<O, F>
207    where
208        F: Fn(&str, &str) -> bool,
209    {
210        ByValue::new(search, condition)
211    }
212}