leetcode_tui_config/theme/
color.rs

1use std::str::FromStr;
2
3use anyhow::Result;
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Deserialize, Debug)]
7#[serde(try_from = "String")]
8pub struct Color(ratatui::style::Color);
9
10impl FromStr for Color {
11    type Err = anyhow::Error;
12
13    fn from_str(s: &str) -> Result<Self, Self::Err> {
14        ratatui::style::Color::from_str(s)
15            .map(Self)
16            .map_err(|_| anyhow::anyhow!("invalid color"))
17    }
18}
19
20impl TryFrom<String> for Color {
21    type Error = anyhow::Error;
22
23    fn try_from(s: String) -> Result<Self, Self::Error> {
24        Self::from_str(s.as_str())
25    }
26}
27
28impl From<Color> for ratatui::style::Color {
29    fn from(value: Color) -> Self {
30        value.0
31    }
32}
33
34impl Serialize for Color {
35    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
36    where
37        S: serde::Serializer,
38    {
39        self.0.to_string().serialize(serializer)
40    }
41}