Skip to content

Commit

Permalink
Add convenience method to decode RPC requests from strings
Browse files Browse the repository at this point in the history
Signed-off-by: Thane Thomson <connect@thanethomson.com>
  • Loading branch information
thanethomson committed Jan 31, 2022
1 parent 39484dd commit e170520
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion rpc/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! JSON-RPC requests
use super::{Id, Method, Version};
use crate::prelude::*;
use crate::{prelude::*, Error};
use core::fmt::Debug;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

Expand All @@ -17,6 +17,12 @@ pub trait Request: Debug + DeserializeOwned + Serialize + Sized + Send {
fn into_json(self) -> String {
Wrapper::new(self).into_json()
}

/// Parse a JSON-RPC request from a JSON string.
fn from_string(s: impl AsRef<[u8]>) -> Result<Self, Error> {
let wrapper: Wrapper<Self> = serde_json::from_slice(s.as_ref()).map_err(Error::serde)?;
Ok(wrapper.params)
}
}

/// Simple JSON-RPC requests which correlate with a single response from the
Expand Down

0 comments on commit e170520

Please sign in to comment.