rust_anilist/models/
color.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR>
3
4//! This module contains the `Color` struct.
5
6use serde::{Deserialize, Serialize};
7
8/// Represents a color with various predefined options and a custom hex value.
9///
10/// The `Color` enum defines a list of supported colors, each with an
11/// associated variant. Additionally, it supports custom colors defined
12/// by a hex string.
13///
14/// # Variants
15///
16/// * `Blue` - The blue color.
17/// * `Purple` - The purple color. This is the default color.
18/// * `Pink` - The pink color.
19/// * `Orange` - The orange color.
20/// * `Red` - The red color.
21/// * `Green` - The green color.
22/// * `Gray` - The gray color.
23/// * `Hex(String)` - A custom color defined by a hex string.
24#[derive(Debug, Default, Clone, Eq, Hash, PartialEq, Deserialize, Serialize)]
25#[serde(rename_all(deserialize = "UPPERCASE"))]
26pub enum Color {
27    /// The blue color.
28    Blue,
29    /// The purple color.
30    #[default]
31    Purple,
32    /// The pink color.
33    Pink,
34    /// The orange color.
35    Orange,
36    /// The red color.
37    Red,
38    /// The green color.
39    Green,
40    /// The gray color.
41    Gray,
42    /// Others colors as hex.
43    #[serde(untagged)]
44    Hex(String),
45}
46
47impl Color {
48    /// Returns the hex value of the color.
49    pub fn hex(&self) -> Option<&str> {
50        match self {
51            Color::Hex(hex) => Some(hex),
52            _ => None,
53        }
54    }
55}
56
57impl From<&str> for Color {
58    fn from(value: &str) -> Self {
59        match value.trim().to_uppercase().as_str() {
60            "BLUE" => Color::Blue,
61            "PURPLE" => Color::Purple,
62            "PINK" => Color::Pink,
63            "ORANGE" => Color::Orange,
64            "RED" => Color::Red,
65            "GREEN" => Color::Green,
66            "GRAY" => Color::Gray,
67            _ => Color::Hex(value.to_string()),
68        }
69    }
70}
71
72impl From<String> for Color {
73    fn from(value: String) -> Self {
74        Color::from(value.as_str())
75    }
76}
77
78impl std::fmt::Display for Color {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            Color::Blue => write!(f, "Blue"),
82            Color::Purple => write!(f, "Purple"),
83            Color::Pink => write!(f, "Pink"),
84            Color::Orange => write!(f, "Orange"),
85            Color::Red => write!(f, "Red"),
86            Color::Green => write!(f, "Green"),
87            Color::Gray => write!(f, "Gray"),
88            Color::Hex(hex) => write!(f, "{}", hex),
89        }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_hex_with_hex_color() {
99        let color = Color::Hex("#FF5733".to_string());
100
101        assert_eq!(color.hex(), Some("#FF5733"));
102    }
103
104    #[test]
105    fn test_hex_with_predefined_color() {
106        let color = Color::Blue;
107
108        assert_eq!(color.hex(), None);
109    }
110
111    #[test]
112    fn test_from_str_predefined_colors() {
113        assert_eq!(Color::from("blue"), Color::Blue);
114        assert_eq!(Color::from("PURPLE"), Color::Purple);
115        assert_eq!(Color::from("Pink"), Color::Pink);
116        assert_eq!(Color::from("orange"), Color::Orange);
117        assert_eq!(Color::from("RED"), Color::Red);
118        assert_eq!(Color::from("green"), Color::Green);
119        assert_eq!(Color::from("gray"), Color::Gray);
120    }
121
122    #[test]
123    fn test_from_str_hex_color() {
124        assert_eq!(Color::from("#FF5733"), Color::Hex("#FF5733".to_string()));
125    }
126
127    #[test]
128    fn test_from_string_predefined_colors() {
129        assert_eq!(Color::from("blue".to_string()), Color::Blue);
130        assert_eq!(Color::from("PURPLE".to_string()), Color::Purple);
131        assert_eq!(Color::from("Pink".to_string()), Color::Pink);
132        assert_eq!(Color::from("orange".to_string()), Color::Orange);
133        assert_eq!(Color::from("RED".to_string()), Color::Red);
134        assert_eq!(Color::from("green".to_string()), Color::Green);
135        assert_eq!(Color::from("gray".to_string()), Color::Gray);
136    }
137
138    #[test]
139    fn test_from_string_hex_color() {
140        assert_eq!(
141            Color::from("#FF5733".to_string()),
142            Color::Hex("#FF5733".to_string())
143        );
144    }
145}