Skip to content

Commit 01e0171

Browse files
authored
Merge pull request #107 from chrivers/chrivers/z2m-crate-refactor
Refactor code to move all z2m-related code entirely into the z2m crate (`crates/z2m/`) This improves rebuild times, and makes it easier to reuse parts of the Bifrost code in other projects.
2 parents 37d65fc + fa15037 commit 01e0171

17 files changed

+146
-74
lines changed

Cargo.lock

+33-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ keywords = [
3737
members = [
3838
"crates/hue",
3939
"crates/svc",
40+
"crates/z2m",
4041
"crates/zcl",
4142
]
4243

@@ -139,6 +140,7 @@ tokio-openssl = "0.6.5"
139140
udp-stream = "0.0.12"
140141
maplit = "1.0.2"
141142
svc = { version = "0.1.0", path = "crates/svc" }
143+
z2m = { version = "0.1.0", path = "crates/z2m" }
142144

143145
[dev-dependencies]
144146
clap-stdin = "0.6.0"

crates/hue/src/api/light.rs

+27
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,39 @@ pub struct LightEffects {
485485
pub effect_values: Vec<LightEffect>,
486486
}
487487

488+
impl LightEffects {
489+
#[must_use]
490+
pub fn all() -> Self {
491+
Self {
492+
status_values: Vec::from(LightEffect::ALL),
493+
status: LightEffect::NoEffect,
494+
effect_values: Vec::from(LightEffect::ALL),
495+
}
496+
}
497+
}
498+
488499
#[derive(Debug, Serialize, Deserialize, Clone)]
489500
pub struct LightEffectsV2 {
490501
pub action: LightEffectValues,
491502
pub status: LightEffectStatus,
492503
}
493504

505+
impl LightEffectsV2 {
506+
#[must_use]
507+
pub fn all() -> Self {
508+
Self {
509+
action: LightEffectValues {
510+
effect_values: Vec::from(LightEffect::ALL),
511+
},
512+
status: LightEffectStatus {
513+
effect: LightEffect::NoEffect,
514+
effect_values: Vec::from(LightEffect::ALL),
515+
parameters: None,
516+
},
517+
}
518+
}
519+
}
520+
494521
#[derive(Debug, Serialize, Deserialize, Clone)]
495522
pub struct LightEffectsV2Update {
496523
#[serde(default)]

crates/z2m/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "z2m"
3+
version = "0.1.0"
4+
5+
edition.workspace = true
6+
authors.workspace = true
7+
rust-version.workspace = true
8+
description.workspace = true
9+
readme.workspace = true
10+
repository.workspace = true
11+
license.workspace = true
12+
categories.workspace = true
13+
keywords.workspace = true
14+
15+
[lints]
16+
workspace = true
17+
18+
[dependencies]
19+
hue = { version = "0.1.0", path = "../hue" }
20+
serde = "1.0.219"
21+
serde_json = "1.0.140"
22+
thiserror = "2.0.12"

src/z2m/api.rs crates/z2m/src/api.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub struct Config {
178178
pub external_converters: Vec<Option<Value>>,
179179
pub frontend: Value,
180180
pub groups: HashMap<String, GroupValue>,
181-
#[serde(with = "crate::z2m::serde_util::struct_or_false")]
181+
#[serde(with = "crate::serde_util::struct_or_false")]
182182
pub homeassistant: Option<ConfigHomeassistant>,
183183
pub map_options: Value,
184184
pub mqtt: Value,

src/z2m/convert.rs crates/z2m/src/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hue::api::{
77
use hue::devicedb::{hardware_platform_type, product_archetype};
88
use hue::xy::XY;
99

10-
use crate::z2m::api::{Device, Expose, ExposeList, ExposeNumeric};
10+
use crate::api::{Device, Expose, ExposeList, ExposeNumeric};
1111

1212
pub trait ExtractExposeNumeric {
1313
fn extract_mirek_schema(&self) -> Option<MirekSchema>;

crates/z2m/src/error.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use thiserror::Error;
2+
3+
#[derive(Error, Debug)]
4+
pub enum Z2mError {
5+
/* mapped errors */
6+
#[error(transparent)]
7+
FromUtf8Error(#[from] std::string::FromUtf8Error),
8+
9+
#[error(transparent)]
10+
ParseIntError(#[from] std::num::ParseIntError),
11+
12+
#[error(transparent)]
13+
IOError(#[from] std::io::Error),
14+
15+
#[error(transparent)]
16+
SerdeJson(#[from] serde_json::Error),
17+
18+
#[error(transparent)]
19+
HueError(#[from] hue::error::HueError),
20+
21+
#[error("Invalid hex color")]
22+
InvalidHexColor,
23+
}
24+
25+
pub type Z2mResult<T> = Result<T, Z2mError>;

src/z2m/hexcolor.rs crates/z2m/src/hexcolor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
use hue::xy::XY;
66

7-
use crate::error::ApiError;
7+
use crate::error::Z2mError;
88

99
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
1010
#[serde(into = "String", try_from = "&str")]
@@ -45,11 +45,11 @@ impl Display for HexColor {
4545
}
4646

4747
impl TryFrom<&str> for HexColor {
48-
type Error = ApiError;
48+
type Error = Z2mError;
4949

5050
fn try_from(value: &str) -> Result<Self, Self::Error> {
5151
if value.len() != 7 || !value.starts_with('#') {
52-
return Err(ApiError::InvalidHexColor);
52+
return Err(Z2mError::InvalidHexColor);
5353
}
5454
let r = u8::from_str_radix(&value[1..3], 16)?;
5555
let g = u8::from_str_radix(&value[3..5], 16)?;
@@ -60,7 +60,7 @@ impl TryFrom<&str> for HexColor {
6060

6161
#[cfg(test)]
6262
mod tests {
63-
use crate::z2m::hexcolor::HexColor;
63+
use crate::hexcolor::HexColor;
6464

6565
#[test]
6666
fn make_hexcolor() {

src/z2m/mod.rs crates/z2m/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod api;
22
pub mod convert;
3+
pub mod error;
34
pub mod hexcolor;
45
pub mod request;
56
pub mod serde_util;

src/z2m/request.rs crates/z2m/src/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::Serialize;
22
use serde_json::Value;
33

4-
use crate::z2m::update::DeviceUpdate;
4+
use crate::update::DeviceUpdate;
55

66
#[derive(Clone, Debug, Serialize)]
77
#[serde(rename_all = "snake_case")]

src/z2m/serde_util.rs crates/z2m/src/serde_util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mod tests {
6868
use serde::{Deserialize, Serialize};
6969
use serde_json::{from_str, to_string};
7070

71-
use crate::error::ApiResult;
71+
use crate::error::Z2mResult;
7272

7373
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
7474
struct Foo {
@@ -92,26 +92,26 @@ mod tests {
9292
const FOO_LIST: &str = r#"{"foo":[42]}"#;
9393

9494
#[test]
95-
pub fn serialize_none() -> ApiResult<()> {
95+
pub fn serialize_none() -> Z2mResult<()> {
9696
assert_eq!(to_string(&FOO_NONE)?, FOO_NONE_STR);
9797
Ok(())
9898
}
9999

100100
#[test]
101-
pub fn serialize_some() -> ApiResult<()> {
101+
pub fn serialize_some() -> Z2mResult<()> {
102102
assert_eq!(to_string(&FOO_SOME)?, FOO_SOME_STR);
103103

104104
Ok(())
105105
}
106106

107107
#[test]
108-
pub fn deserialize_false() -> ApiResult<()> {
108+
pub fn deserialize_false() -> Z2mResult<()> {
109109
assert_eq!(from_str::<Foo>(FOO_NONE_STR)?, FOO_NONE);
110110
Ok(())
111111
}
112112

113113
#[test]
114-
pub fn deserialize_struct() -> ApiResult<()> {
114+
pub fn deserialize_struct() -> Z2mResult<()> {
115115
assert_eq!(from_str::<Foo>(FOO_SOME_STR)?, FOO_SOME);
116116
Ok(())
117117
}

src/z2m/update.rs crates/z2m/src/update.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_json::Value;
66
use hue::api::{LightGradientUpdate, On};
77
use hue::xy::XY;
88

9-
use crate::z2m::hexcolor::HexColor;
9+
use crate::hexcolor::HexColor;
1010

1111
#[allow(clippy::pub_underscore_fields)]
1212
#[derive(Debug, Serialize, Deserialize, Clone, Default)]

examples/wsinput.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::select;
99
use tokio_tungstenite::{connect_async, tungstenite::Message};
1010

1111
use bifrost::error::ApiResult;
12-
use bifrost::z2m::api::RawMessage;
12+
use z2m::api::RawMessage;
1313

1414
#[macro_use]
1515
extern crate log;

0 commit comments

Comments
 (0)