papergrid/colors/
mod.rs

1//! A module which contains [Colors] trait and its blanket implementations.
2
3mod nocolors;
4
5use crate::{ansi::ANSIFmt, config::Position};
6
7pub use nocolors::*;
8
9/// A trait which represents map of colors.
10pub trait Colors {
11    /// Color implementation.
12    type Color: ANSIFmt;
13
14    /// Returns a color for a given position.
15    fn get_color(&self, pos: Position) -> Option<&Self::Color>;
16
17    /// Verifies whether a map is empty or not.
18    fn is_empty(&self) -> bool;
19}
20
21impl<C> Colors for &'_ C
22where
23    C: Colors,
24{
25    type Color = C::Color;
26
27    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
28        C::get_color(self, pos)
29    }
30
31    fn is_empty(&self) -> bool {
32        C::is_empty(self)
33    }
34}
35
36#[cfg(feature = "std")]
37impl<C> Colors for std::collections::HashMap<Position, C>
38where
39    C: ANSIFmt,
40{
41    type Color = C;
42
43    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
44        self.get(&pos)
45    }
46
47    fn is_empty(&self) -> bool {
48        std::collections::HashMap::is_empty(self)
49    }
50}
51
52#[cfg(feature = "std")]
53impl<C> Colors for std::collections::BTreeMap<Position, C>
54where
55    C: ANSIFmt,
56{
57    type Color = C;
58
59    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
60        self.get(&pos)
61    }
62
63    fn is_empty(&self) -> bool {
64        std::collections::BTreeMap::is_empty(self)
65    }
66}
67
68#[cfg(feature = "std")]
69impl<C> Colors for crate::config::spanned::EntityMap<Option<C>>
70where
71    C: ANSIFmt + PartialEq,
72{
73    type Color = C;
74
75    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
76        self.get(pos).as_ref()
77    }
78
79    fn is_empty(&self) -> bool {
80        self.is_empty() && self.as_ref().is_none()
81    }
82}