Skip to content

Commit

Permalink
BUG FIX : Deserializing Structs containing flattened RawValues always…
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsukiTak committed Mar 6, 2020
1 parent 43cda1c commit 2437786
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ use std::sync::Arc;
* Request
* =======
*/
/// Deserializing structs containing flattened RawValue always failes.
/// https://github.com/serde-rs/json/issues/599
///
/// So currently we wraps `method` and `params` by `Arc` separately.
#[derive(Debug, Clone, Deserialize)]
pub struct Request {
#[serde(flatten)]
inner: Arc<Inner>,
}

#[derive(Debug, Deserialize)]
struct Inner {
jsonrpc: Version,
id: Option<u64>,
method: String,
params: Option<Box<RawValue>>,
method: Arc<String>,
params: Arc<Option<Box<RawValue>>>,
}

#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)]
Expand All @@ -29,18 +27,18 @@ pub enum Version {

impl Request {
pub fn id(&self) -> Option<u64> {
self.inner.id
self.id
}

pub fn method(&self) -> &str {
self.inner.method.as_str()
self.method.as_str()
}

pub fn deserialize_param<'de, T>(&'de self) -> Result<T, anyhow::Error>
where
T: Deserialize<'de>,
{
match &self.inner.params {
match self.params.as_ref() {
Some(params) => Ok(serde_json::from_str(params.get())?),
None => Err(anyhow::anyhow!("No parameter is presented")),
}
Expand All @@ -60,8 +58,8 @@ mod test {
"id": 42
}"#;
let req = serde_json::from_str::<Request>(req_str).unwrap();
assert_eq!(req.id, Some(42));
assert_eq!(req.method, "op".to_string());
assert_eq!(req.id(), Some(42));
assert_eq!(req.method(), "op");

#[derive(PartialEq, Eq, Debug, Deserialize)]
struct Param {
Expand Down Expand Up @@ -90,8 +88,8 @@ mod test {
"id": 42
}"#;
let req = serde_json::from_str::<Request>(req_str).unwrap();
assert_eq!(req.id, Some(42));
assert_eq!(req.method, "op".to_string());
assert_eq!(req.id(), Some(42));
assert_eq!(req.method(), "op");

let (lhs, rhs, op) = req.deserialize_param::<(i32, i32, String)>().unwrap();
assert_eq!(lhs, 24);
Expand Down

0 comments on commit 2437786

Please sign in to comment.