Skip to content

Commit bde8795

Browse files
committed
error in deserialize impl rpc is if not notification
1 parent 724c8eb commit bde8795

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/main.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Supported procedures:
3535
///
3636
/// Assumes that the request's parameters are always `String`s.
3737
#[derive(Debug)]
38-
#[cfg_attr(feature = "msgpack", derive(Deserialize))]
3938
pub struct Rpc {
4039
/// The type of msgpack request. Should always be notification.
4140
#[cfg(feature = "msgpack")]
@@ -49,6 +48,33 @@ pub struct Rpc {
4948
pub params: Vec<String>,
5049
}
5150

51+
#[cfg(feature = "msgpack")]
52+
impl<'de> Deserialize<'de> for Rpc {
53+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
54+
where
55+
D: serde::Deserializer<'de>,
56+
{
57+
use serde::de::{Error, Unexpected};
58+
59+
const NOTIFICATION_MESSAGE_TYPE: u64 = 2;
60+
61+
let (msg_type, method, params) = <(u64, String, Vec<String>)>::deserialize(deserializer)?;
62+
63+
if msg_type != NOTIFICATION_MESSAGE_TYPE {
64+
return Err(Error::invalid_value(
65+
Unexpected::Unsigned(msg_type),
66+
&format!("notification message type ({})", NOTIFICATION_MESSAGE_TYPE).as_str(),
67+
));
68+
}
69+
70+
Ok(Rpc {
71+
msg_type,
72+
method,
73+
params,
74+
})
75+
}
76+
}
77+
5278
#[cfg(feature = "json-rpc")]
5379
impl<'de> Deserialize<'de> for Rpc {
5480
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

0 commit comments

Comments
 (0)