Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/device_mini_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ fn make_device_description(
prop_light_state.prop_id().clone(),
PropertyDescriptionBuilder::new(HomieDataType::Boolean)
.name("Light state")
.format(HomiePropertyFormat::Boolean {
.format(HomiePropertyFormat::Boolean(BooleanFormat {
false_val: "off".to_string(),
true_val: "on".to_string(),
})
}))
.settable(true)
.build(),
)
Expand Down
8 changes: 4 additions & 4 deletions examples/devices/light_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use rumqttc::AsyncClient;

use homie5::{
device_description::{
DeviceDescriptionBuilder, HomieDeviceDescription, HomiePropertyFormat, IntegerRange, NodeDescriptionBuilder,
PropertyDescriptionBuilder,
BooleanFormat, DeviceDescriptionBuilder, HomieDeviceDescription, HomiePropertyFormat, IntegerRange,
NodeDescriptionBuilder, PropertyDescriptionBuilder,
},
Homie5DeviceProtocol, HomieDataType, HomieDeviceStatus, HomieDomain, HomieID, HomieValue, NodeRef, PropertyRef,
HOMIE_UNIT_PERCENT,
Expand Down Expand Up @@ -61,10 +61,10 @@ impl LightDevice {
prop_light_state.prop_id().clone(),
PropertyDescriptionBuilder::new(HomieDataType::Boolean)
.name("Light state")
.format(HomiePropertyFormat::Boolean {
.format(HomiePropertyFormat::Boolean(BooleanFormat {
false_val: "off".to_string(),
true_val: "on".to_string(),
})
}))
.retained(true)
.settable(true)
.build(),
Expand Down
58 changes: 39 additions & 19 deletions src/device_description/property_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum HomiePropertyFormat {
IntegerRange(IntegerRange),
Enum(Vec<String>),
Color(Vec<ColorFormat>),
Boolean { false_val: String, true_val: String },
Boolean(BooleanFormat),
Json(String), // Raw JSON schema as string
Custom(String),
Empty,
Expand All @@ -32,7 +32,7 @@ impl HomiePropertyFormat {
HomiePropertyFormat::IntegerRange(r) => r.is_empty(),
HomiePropertyFormat::Enum(values) => values.is_empty(),
HomiePropertyFormat::Color(formats) => formats.is_empty(),
HomiePropertyFormat::Boolean { false_val, true_val } => false_val.is_empty() || true_val.is_empty(),
HomiePropertyFormat::Boolean(bf) => bf.is_empty(),
HomiePropertyFormat::Json(data) => data.is_empty(),
HomiePropertyFormat::Custom(data) => data.is_empty(),
HomiePropertyFormat::Empty => true,
Expand All @@ -56,16 +56,49 @@ impl Display for HomiePropertyFormat {
formats.iter().map(|c| c.to_string()).collect::<Vec<String>>().join(",")
)
}
HomiePropertyFormat::Boolean { false_val, true_val } => {
write!(f, "{},{}", false_val, true_val)
}
HomiePropertyFormat::Boolean(bf) => bf.fmt(f),
HomiePropertyFormat::Json(data) => write!(f, "{}", data),
HomiePropertyFormat::Custom(data) => write!(f, "{}", data),
HomiePropertyFormat::Empty => write!(f, ""),
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, PartialOrd)]
pub struct BooleanFormat {
pub false_val: String,
pub true_val: String,
}

impl BooleanFormat {
pub fn is_empty(&self) -> bool {
self.false_val.is_empty() && self.true_val.is_empty()
}
}

impl Display for BooleanFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{},{}", self.false_val, self.true_val)
}
}

impl FromStr for BooleanFormat {
type Err = HomiePropertyFormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let tokens = s.split(',').collect::<Vec<&str>>();
if tokens.len() != 2 {
return Err(HomiePropertyFormatError::BooleanFormatError);
}
if tokens[0].is_empty() || tokens[1].is_empty() || tokens[0] == tokens[1] {
return Err(HomiePropertyFormatError::BooleanFormatError);
}

Ok(Self {
false_val: tokens[0].to_owned(),
true_val: tokens[1].to_owned(),
})
}
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, PartialOrd)]
pub enum ColorFormat {
Rgb,
Expand Down Expand Up @@ -141,20 +174,7 @@ impl HomiePropertyFormat {
}
Ok(Self::Color(formats))
}
HomieDataType::Boolean => {
let tokens = raw.split(',').collect::<Vec<&str>>();
if tokens.len() != 2 {
return Err(HomiePropertyFormatError::BooleanFormatError);
}
if tokens[0].is_empty() || tokens[1].is_empty() || tokens[0] == tokens[1] {
return Err(HomiePropertyFormatError::BooleanFormatError);
}

Ok(Self::Boolean {
false_val: tokens[0].to_owned(),
true_val: tokens[1].to_owned(),
})
}
HomieDataType::Boolean => Ok(Self::Boolean(BooleanFormat::from_str(raw)?)),
HomieDataType::JSON => Ok(Self::Json(raw.to_owned())), // todo: we need to check if
// this contains valid json
// string data
Expand Down
Loading