hap-rs icon indicating copy to clipboard operation
hap-rs copied to clipboard

Value Type check for Characteristics

Open soundprojects opened this issue 3 years ago • 2 comments

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

soundprojects avatar Aug 11 '22 20:08 soundprojects

Good point!

ewilken avatar Aug 14 '22 15:08 ewilken

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 }

soundprojects avatar Aug 14 '22 17:08 soundprojects