1 | //! A module which contains [Colors] trait and its blanket implementations. |
2 | |
3 | use crate::{color::Color, config::Position}; |
4 | |
5 | /// A trait which represents map of colors. |
6 | pub trait Colors { |
7 | /// Color implementation. |
8 | type Color: Color; |
9 | |
10 | /// Returns a color for a given position. |
11 | fn get_color(&self, pos: (usize, usize)) -> Option<&Self::Color>; |
12 | } |
13 | |
14 | impl<C> Colors for &'_ C |
15 | where |
16 | C: Colors, |
17 | { |
18 | type Color = C::Color; |
19 | |
20 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
21 | C::get_color(self, pos) |
22 | } |
23 | } |
24 | |
25 | #[cfg (feature = "std" )] |
26 | impl<C> Colors for std::collections::HashMap<Position, C> |
27 | where |
28 | C: Color, |
29 | { |
30 | type Color = C; |
31 | |
32 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
33 | self.get(&pos) |
34 | } |
35 | } |
36 | |
37 | #[cfg (feature = "std" )] |
38 | impl<C> Colors for std::collections::BTreeMap<Position, C> |
39 | where |
40 | C: Color, |
41 | { |
42 | type Color = C; |
43 | |
44 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
45 | self.get(&pos) |
46 | } |
47 | } |
48 | |
49 | #[cfg (feature = "std" )] |
50 | impl<C> Colors for crate::config::spanned::EntityMap<Option<C>> |
51 | where |
52 | C: Color, |
53 | { |
54 | type Color = C; |
55 | |
56 | fn get_color(&self, pos: Position) -> Option<&Self::Color> { |
57 | self.get(entity:pos.into()).as_ref() |
58 | } |
59 | } |
60 | |
61 | /// The structure represents empty [`Colors`] map. |
62 | #[derive (Debug, Default, Clone)] |
63 | pub struct NoColors; |
64 | |
65 | impl Colors for NoColors { |
66 | type Color = EmptyColor; |
67 | |
68 | fn get_color(&self, _: Position) -> Option<&Self::Color> { |
69 | None |
70 | } |
71 | } |
72 | |
73 | /// A color which is actually has not value. |
74 | #[derive (Debug)] |
75 | pub struct EmptyColor; |
76 | |
77 | impl Color for EmptyColor { |
78 | fn fmt_prefix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result { |
79 | Ok(()) |
80 | } |
81 | |
82 | fn colorize<W: core::fmt::Write>(&self, _: &mut W, _: &str) -> core::fmt::Result { |
83 | Ok(()) |
84 | } |
85 | |
86 | fn fmt_suffix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result { |
87 | Ok(()) |
88 | } |
89 | } |
90 | |