From ff9c410c44f3edfe69b69eafd13444e96091da66 Mon Sep 17 00:00:00 2001 From: aviac Date: Mon, 25 Sep 2023 08:56:23 +0200 Subject: [PATCH] feat: implement Display for Color --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/types/misc/color.rs | 18 +++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83a8b92..2d8bb2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "forgejo-api-types" -version = "0.1.5" +version = "0.1.6" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 16092cf..10093e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "forgejo-api-types" -version = "0.1.5" +version = "0.1.6" edition = "2021" license = "AGPL-3.0-or-later" keywords = ["forgejo", "types", "codeberg", "api"] diff --git a/src/types/misc/color.rs b/src/types/misc/color.rs index fbc2c4c..3139bb2 100644 --- a/src/types/misc/color.rs +++ b/src/types/misc/color.rs @@ -1,3 +1,4 @@ +use std::fmt::Display; use std::ops::{Deref, DerefMut}; use palette::rgb::Rgb; @@ -16,6 +17,12 @@ impl Deref for Color { } } +impl Display for Color { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "#{:02X}{:02X}{:02X}", self.red, self.green, self.blue) + } +} + impl DerefMut for Color { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 @@ -27,10 +34,7 @@ impl Serialize for Color { where S: Serializer, { - // Convert the RGB color to a six-digit hexadecimal string - let hex_string = - format!("#{:02X}{:02X}{:02X}", self.red, self.green, self.blue).to_lowercase(); - serializer.serialize_str(&hex_string) + serializer.serialize_str(self.to_string().as_str()) } } @@ -79,7 +83,7 @@ mod color_tests { fn color_serialize() { // Example of serializing and deserializing let rgb_color = Color(Rgb::new(0x64, 0x32, 0xc8)); - let rgb_color_str = "\"#6432c8\""; + let rgb_color_str = "\"#6432C8\""; let serialized = serde_json::to_string(&rgb_color).unwrap(); assert_eq!(serialized, rgb_color_str); @@ -89,7 +93,7 @@ mod color_tests { fn color_deserialize() { // Example of serializing and deserializing let rgb_color = Color(Rgb::new(0x64, 0x32, 0xc8)); - let rgb_color_str = "\"#6432c8\""; + let rgb_color_str = "\"#6432C8\""; let deserialized: Color = serde_json::from_str(rgb_color_str).unwrap(); assert_eq!(deserialized, rgb_color); @@ -98,7 +102,7 @@ mod color_tests { #[test] fn multiple_pound_symbols_not_allowed() { // Example of serializing and deserializing - let rgb_color_str = "\"##6432c8\""; + let rgb_color_str = "\"##6432C8\""; let deserialized: Result = serde_json::from_str(rgb_color_str); assert!(deserialized.is_err());