ewilken / hap-rs

Rust implementation of the Apple HomeKit Accessory Protocol (HAP)
Apache License 2.0
196 stars 33 forks source link

Value Type check for Characteristics #74

Open soundprojects opened 2 years ago

soundprojects commented 2 years ago

I think it would be good to have a value type check for characteristics in some way

Now a characteristic value only has to be of a Json Value type but that does allow the user to set a bool where the specification excepts a number, resulting in a panic

ewilken commented 2 years ago

Good point!

soundprojects commented 2 years ago

I was looking through your handle bar templating code and I am wondering if it would help to make the method:

async fn set_value(&mut self, value: serde_json::Value) -> Result<()> { HapCharacteristic::set_value(&mut self.0, value).await }

in codegen/src/main.rs

Would it be an idea to change the argument type to the unit type that the characteristic expects, and then have your template generate the Value structure inside of the set_value function?

That way you can still have all the generics, but the end user will know exactly what value is expected here and code analyzer will notify them of this as well?

It could also make your template have the unit shown in the documentation. This would make each accessory file have different arguments for this function but it would surely make things more clearer?

Something like:

///This function accepts a bool async fn set_value(&mut self, value: bool) -> Result<()> { HapCharacteristic::set_value(&mut self.0, json!(value)).await }