some_random_api/structs/
canvas.rs

1use serde::Deserialize;
2use std::num::ParseIntError;
3
4#[derive(Debug, Deserialize)]
5pub struct Hex {
6    pub hex: String,
7}
8
9impl From<u32> for Hex {
10    fn from(value: u32) -> Self {
11        Self {
12            hex: format!("{:06x}", value.min(0xffffff)),
13        }
14    }
15}
16
17impl TryFrom<&str> for Hex {
18    type Error = ParseIntError;
19
20    fn try_from(value: &str) -> Result<Self, Self::Error> {
21        Ok(Self {
22            hex: format!(
23                "{:06x}",
24                u32::from_str_radix(value.strip_prefix("#").unwrap_or(&value), 16)?.min(0xffffff),
25            ),
26        })
27    }
28}
29
30impl TryFrom<String> for Hex {
31    type Error = ParseIntError;
32
33    fn try_from(value: String) -> Result<Self, Self::Error> {
34        Ok(Self {
35            hex: format!(
36                "{:06x}",
37                u32::from_str_radix(value.strip_prefix("#").unwrap_or(&value), 16)?.min(0xffffff),
38            ),
39        })
40    }
41}
42
43#[derive(Debug, Deserialize)]
44pub struct RGB {
45    pub r: u8,
46    pub g: u8,
47    pub b: u8,
48}