Skip to content

Commit

Permalink
put serde_json::Error back into crate::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraef committed Jun 9, 2024
1 parent b6bce66 commit 1dd495e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The current changes need a *minor* version bump.
- add `json` feature (disabled by default).
- add `serde` and `serde_json` optional dependencies.
- add `Message::*_from_json` and `Method::json` methods.
- add `Error::Json(serde_json::Error)` variant.
- add `Error::Json(serde_json::Error)` variant. *breaking*
- this would normally break semver-compatibility, but it's behind a feature flag that wasn't available before.

- close: (#11)
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ pub enum Error {
#[cfg_attr(docsrs, doc(cfg(target_arch = "wasm32")))]
#[error("web_sys error")]
WebSys(#[from] wasm::WebSysError),

#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[error("json error")]
Json(#[from] serde_json::Error),
}

/// Opens a `WebSocket` connection at the specified `URL`.
Expand Down
13 changes: 6 additions & 7 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ use serde::{
Serialize,
};

#[cfg(feature = "json")]
use crate::Error;

/// A `WebSocket` message, which can be a text string or binary data.
#[derive(Clone, Debug)]
pub enum Message {
Text(String),
Binary(Vec<u8>),
}

#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub type JsonError = serde_json::Error;

#[cfg(feature = "json")]
impl Message {
/// Tries to serialize the JSON as a [`Message::Text`].
Expand All @@ -28,7 +27,7 @@ impl Message {
/// Serialization can fail if `T`'s implementation of `Serialize` decides to
/// fail, or if `T` contains a map with non-string keys.
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn text_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, JsonError> {
pub fn text_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error> {
serde_json::to_string(json)
.map(Message::Text)
.map_err(Into::into)
Expand All @@ -45,7 +44,7 @@ impl Message {
/// Serialization can fail if `T`'s implementation of `Serialize` decides to
/// fail, or if `T` contains a map with non-string keys.
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn binary_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, JsonError> {
pub fn binary_from_json<T: Serialize + ?Sized>(json: &T) -> Result<Self, Error> {
serde_json::to_vec(json)
.map(Message::Binary)
.map_err(Into::into)
Expand All @@ -65,7 +64,7 @@ impl Message {
/// For more details please see [`serde_json::from_str`] and
/// [`serde_json::from_slice`].
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub fn json<T: DeserializeOwned>(&self) -> Result<T, JsonError> {
pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error> {
match self {
Self::Text(x) => serde_json::from_str(x),
Self::Binary(x) => serde_json::from_slice(x),
Expand Down

0 comments on commit 1dd495e

Please sign in to comment.