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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! BED record color.

use std::{error, fmt, num, str::FromStr};

const DELIMITER: char = ',';

/// A BED record color.
///
/// A color is represented as an RGB triplet, where each component ranges from 0 to 255, inclusive.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
}

impl Color {
    /// White `(255, 255, 255)`.
    pub const WHITE: Self = Self::new(0xff, 0xff, 0xff);

    /// Black `(0, 0, 0)`.
    pub const BLACK: Self = Self::new(0x00, 0x00, 0x00);

    /// Red `(255, 0, 0)`.
    pub const RED: Self = Self::new(0xff, 0x00, 0x00);

    /// Lime `(0, 255, 0)`.
    pub const LIME: Self = Self::new(0x00, 0xff, 0x00);

    /// Green `(0, 128, 0)`.
    pub const GREEN: Self = Self::new(0x00, 0x80, 0x00);

    /// Blue `(0, 0, 255)`.
    pub const BLUE: Self = Self::new(0x00, 0x00, 0xff);

    /// Creates a BED record color.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bed::record::Color;
    /// let color = Color::new(250, 128, 114);
    /// ```
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    /// Returns the value of the red component.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bed::record::Color;
    /// let color = Color::new(250, 128, 114);
    /// assert_eq!(color.red(), 250);
    /// ```
    pub const fn red(&self) -> u8 {
        self.r
    }

    /// Returns the value of the red component.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bed::record::Color;
    /// let color = Color::new(250, 128, 114);
    /// assert_eq!(color.green(), 128);
    /// ```
    pub const fn green(&self) -> u8 {
        self.g
    }

    /// Returns the value of the red component.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bed::record::Color;
    /// let color = Color::new(250, 128, 114);
    /// assert_eq!(color.blue(), 114);
    /// ```
    pub const fn blue(&self) -> u8 {
        self.b
    }
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}{}{}{}",
            self.r, DELIMITER, self.g, DELIMITER, self.b
        )
    }
}

/// An error returned when a raw BED record color fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The input is invalid.
    Invalid,
    /// A color component is invalid.
    InvalidComponent(num::ParseIntError),
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::Invalid => None,
            Self::InvalidComponent(e) => Some(e),
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid => f.write_str("invalid input"),
            Self::InvalidComponent(e) => write!(f, "invalid component: {e}"),
        }
    }
}

impl FromStr for Color {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (a, b, c) = split_twice(s, DELIMITER).ok_or(ParseError::Invalid)?;

        let r = a.parse().map_err(ParseError::InvalidComponent)?;
        let g = b.parse().map_err(ParseError::InvalidComponent)?;
        let b = c.parse().map_err(ParseError::InvalidComponent)?;

        Ok(Color::new(r, g, b))
    }
}

fn split_twice(s: &str, delimiter: char) -> Option<(&str, &str, &str)> {
    let (a, s) = s.split_once(delimiter)?;
    let (b, c) = s.split_once(delimiter)?;
    Some((a, b, c))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fmt() {
        assert_eq!(Color::new(0, 0, 0).to_string(), "0,0,0");
        assert_eq!(Color::new(8, 16, 128).to_string(), "8,16,128");
        assert_eq!(Color::new(255, 255, 255).to_string(), "255,255,255");
    }

    #[test]
    fn test_from_str() {
        assert_eq!("0,0,0".parse(), Ok(Color::new(0, 0, 0)));
        assert_eq!("8,16,128".parse(), Ok(Color::new(8, 16, 128)));
        assert_eq!("255,255,255".parse(), Ok(Color::new(255, 255, 255)));

        assert_eq!("0".parse::<Color>(), Err(ParseError::Invalid));
        assert_eq!("0,0".parse::<Color>(), Err(ParseError::Invalid));

        assert!(matches!(
            "0,0,0,0".parse::<Color>(),
            Err(ParseError::InvalidComponent(_))
        ));
        assert!(matches!(
            "red,0,0".parse::<Color>(),
            Err(ParseError::InvalidComponent(_))
        ));
        assert!(matches!(
            "0,green,0".parse::<Color>(),
            Err(ParseError::InvalidComponent(_))
        ));
        assert!(matches!(
            "0,0,blue".parse::<Color>(),
            Err(ParseError::InvalidComponent(_))
        ));
    }
}