1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use serde::de;
// ------------------------
// Handling Govee Issues
// ------------------------
//

// Deserialize a boolean value from a given deserializer.
//
// This function deserializes a boolean value from the provided deserializer. It handles
// both boolean values and string representations of 'true' and 'false'. If the incoming
// value is a boolean, it is returned directly. If the incoming value is a string 'true',
// it returns true; if it's a string 'false', it returns false. For any other input, an
// error is returned.
//
// # Arguments
//
// * `deserializer` - The deserializer implementing the `de::Deserializer` trait.
//
// # Returns
//
// Returns a `Result` containing the deserialized boolean value if successful, or an
// error of type `D::Error` if deserialization fails or the input is not a valid boolean
// representation.
//
// # Examples
//
// ```rust
// use serde::de::Deserialize;
//
// let json = r#""true""#;
// let deserializer = serde_json::Deserializer::from_str(json);
// let result: Result<bool, _> = deserialize_bool(deserializer);
// assert_eq!(result, Ok(true));
// ```
//
// ```rust
// use serde::de::Deserialize;
//
// let json = r#"false"#;
// let deserializer = serde_json::Deserializer::from_str(json);
// let result: Result<bool, _> = deserialize_bool(deserializer);
// assert_eq!(result, Ok(false));
// ```
//
// ```rust
// use serde::de::Deserialize;
//
// let json = r#"42"#; // This input is not a valid boolean representation
// let deserializer = serde_json::Deserializer::from_str(json);
// let result: Result<bool, _> = deserialize_bool(deserializer);
// assert!(result.is_err());
// ```
pub fn deserialize_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: de::Deserializer<'de>,
{
    // If the incoming value is a string 'true' or 'false', return true or false
    // If the incoming value is a boolean, return the boolean
    match serde::Deserialize::deserialize(deserializer)? {
        serde_json::Value::Bool(b) => Ok(b),
        serde_json::Value::String(s) if s == "true" => Ok(true),
        serde_json::Value::String(s) if s == "false" => Ok(false),
        _ => Err(serde::de::Error::custom(
            "Expected a boolean or 'true'/'false' string",
        )),
    }
}