Skip to content

Commit 2dea25e

Browse files
committed
Added custom serialization and deserialization for HomieColorValue into and from its string representation
1 parent c1810d8 commit 2dea25e

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ ctrlc = "3.1"
3030
log = "0.4"
3131
env_logger = "0.11"
3232
serde_yaml = "0.9"
33-

src/device_description/property_format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl FromStr for BooleanFormat {
100100
}
101101
}
102102
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, PartialOrd)]
103+
#[serde(rename_all = "lowercase")]
103104
pub enum ColorFormat {
104105
Rgb,
105106
Hsv,

src/value.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
str::FromStr,
77
};
88

9-
use serde::de;
9+
use serde::{de, Serialize, Serializer};
1010
use serde::{Deserialize, Deserializer};
1111

1212
use crate::{
@@ -112,7 +112,7 @@ impl std::error::Error for Homie5ValueConversionError {
112112
/// attribute of the property, and the value must conform to that format.
113113
///
114114
/// For more details on the color formats and their constraints, refer to the Homie specification.
115-
#[derive(Debug, Clone, Copy, Deserialize)]
115+
#[derive(Debug, Clone, Copy)]
116116
pub enum HomieColorValue {
117117
/// Represents a color in the RGB format, using three integers for red, green, and blue channels.
118118
/// Each value must be an integer between 0 and 255.
@@ -138,6 +138,38 @@ impl HomieColorValue {
138138
}
139139
}
140140

141+
/// Serialize the ColorValue to its string representation
142+
///
143+
/// - Example: `"rgb,255,0,0"` for red.
144+
/// - Example: `"hsv,120,100,100"` for bright green.
145+
/// - Example: `"xyz,0.25,0.34"`.
146+
impl Serialize for HomieColorValue {
147+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
148+
where
149+
S: Serializer,
150+
{
151+
// Serialize the enum as its string representation.
152+
serializer.serialize_str(&self.to_string())
153+
}
154+
}
155+
156+
/// Deserialize the ColorValue from its string representation
157+
///
158+
/// - Example: `"rgb,255,0,0"` for red.
159+
/// - Example: `"hsv,120,100,100"` for bright green.
160+
/// - Example: `"xyz,0.25,0.34"`.
161+
impl<'de> Deserialize<'de> for HomieColorValue {
162+
fn deserialize<D>(deserializer: D) -> Result<HomieColorValue, D::Error>
163+
where
164+
D: Deserializer<'de>,
165+
{
166+
// Deserialize into a string first.
167+
let s = String::deserialize(deserializer)?;
168+
// Use the FromStr implementation to parse the string.
169+
HomieColorValue::from_str(&s).map_err(serde::de::Error::custom)
170+
}
171+
}
172+
141173
impl PartialEq for HomieColorValue {
142174
fn eq(&self, other: &Self) -> bool {
143175
const EPSILON: f64 = 1e-6; // this is already way to precise for color values

0 commit comments

Comments
 (0)