Skip to content

Commit

Permalink
feat: add docstrings to util method
Browse files Browse the repository at this point in the history
  • Loading branch information
mgierada committed Aug 17, 2023
1 parent 1df9f99 commit 4370111
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "govee-api"
authors = ["Maciej Gierada"]
version = "1.0.1"
version = "1.0.2"
edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["govee", "api", "wrapper", "client", "sdk"]
Expand Down
46 changes: 46 additions & 0 deletions src/utils/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ use serde::de;
// ------------------------
//

/// 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>,
Expand Down

0 comments on commit 4370111

Please sign in to comment.