summaryrefslogtreecommitdiffstats
path: root/vendor/papergrid/src/colors.rs
blob: f671ec005099cb6d42caa6c3e5c5edb1b9cc950a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! A module which contains [Colors] trait and its blanket implementations.

use crate::{color::Color, config::Position};

/// A trait which represents map of colors.
pub trait Colors {
    /// Color implementation.
    type Color: Color;

    /// Returns a color for a given position.
    fn get_color(&self, pos: (usize, usize)) -> Option<&Self::Color>;
}

impl<C> Colors for &'_ C
where
    C: Colors,
{
    type Color = C::Color;

    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
        C::get_color(self, pos)
    }
}

#[cfg(feature = "std")]
impl<C> Colors for std::collections::HashMap<Position, C>
where
    C: Color,
{
    type Color = C;

    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
        self.get(&pos)
    }
}

#[cfg(feature = "std")]
impl<C> Colors for std::collections::BTreeMap<Position, C>
where
    C: Color,
{
    type Color = C;

    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
        self.get(&pos)
    }
}

#[cfg(feature = "std")]
impl<C> Colors for crate::config::spanned::EntityMap<Option<C>>
where
    C: Color,
{
    type Color = C;

    fn get_color(&self, pos: Position) -> Option<&Self::Color> {
        self.get(pos.into()).as_ref()
    }
}

/// The structure represents empty [`Colors`] map.
#[derive(Debug, Default, Clone)]
pub struct NoColors;

impl Colors for NoColors {
    type Color = EmptyColor;

    fn get_color(&self, _: Position) -> Option<&Self::Color> {
        None
    }
}

/// A color which is actually has not value.
#[derive(Debug)]
pub struct EmptyColor;

impl Color for EmptyColor {
    fn fmt_prefix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result {
        Ok(())
    }

    fn colorize<W: core::fmt::Write>(&self, _: &mut W, _: &str) -> core::fmt::Result {
        Ok(())
    }

    fn fmt_suffix<W: core::fmt::Write>(&self, _: &mut W) -> core::fmt::Result {
        Ok(())
    }
}