Struct Request

Source
pub struct Request<M, T> {
    pub jsonrpc: V2_0,
    pub method: M,
    pub params: Option<T>,
    pub id: IdReq,
}
Expand description

A rpc call is represented by sending a Request object to a Server.

See the parameters for details.

§Examples

extern crate serde_json;
use jrpc::{Id, Request, V2_0};

let value: Vec<u32> = vec![1, 2, 3];
let request = Request::with_params(
    Id::from(4),
    "CreateFoo".to_string(),
    Some(value.clone()),
);
let json = r#"
{
    "jsonrpc": "2.0",
    "method": "CreateFoo",
    "params": [1,2,3],
    "id": 4
}
"#;
let json = json.replace("\n", "").replace(" ", "");
let result = serde_json::to_string(&request).unwrap();
assert_eq!(json, result);

Fields§

§jsonrpc: V2_0

A String specifying the version of the JSON-RPC protocol. MUST be exactly “2.0”.

§method: M

A serializable method.

The spec states it must be a String containing the name of the method to be invoked. This library makes no guarantees about this. It is recomended to use a simple enum for your library’s method.

§Section 8: Extensions

Method names that begin with "rpc." are reserved for system extensions, and MUST NOT be used for anything else. Each system extension is defined in a related specification. All system extensions are OPTIONAL.

This library provides no way of checking for system extensions.

§params: Option<T>

A Structured value that holds the parameter values to be used during the invocation of the method.

§Spec Requiement

Note: the following spec is not upheld by this library.

If present, parameters for the rpc call MUST be provided as a Structured value. Either by-position through an Array or by-name through an Object.

  • by-position: params MUST be an Array, containing the values in the Server expected order.
  • by-name: params MUST be an Object, with member names that match the Server expected parameter names. The absence of expected names MAY result in an error being generated. The names MUST match exactly, including case, to the method’s expected parameters.
§id: IdReq

The id. See Id

Implementations§

Source§

impl<M: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned> Request<M, T>

Source

pub fn to_string(&self) -> String

Helper to serialize the Request as json.

Source

pub fn from_str(s: &str) -> Result<T>

Helper to deserialize the Request from json.

Source§

impl<M: Serialize + DeserializeOwned> Request<M, ()>

Source

pub fn new<I>(id: I, method: M) -> Self
where I: Into<IdReq>,

Create a new Request.

§Examples
§extern crate jrpc;
extern crate serde_json;
use jrpc::{Id, Request};

let value: Vec<u32> = vec![1, 2, 3];
let request = Request::new(
    Id::from(4),
    "CreateFoo".to_string(),
);
println!("{}", request.to_string());
Source§

impl<M: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned> Request<M, T>

Source

pub fn with_params<I>(id: I, method: M, params: T) -> Self
where I: Into<IdReq>,

Create a new Request with the specified params.

Trait Implementations§

Source§

impl<M: Debug, T: Debug> Debug for Request<M, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, M, T> Deserialize<'de> for Request<M, T>
where M: Deserialize<'de>, T: Deserialize<'de> + Default,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<M, T> Serialize for Request<M, T>
where M: Serialize, T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<M, T> Freeze for Request<M, T>
where M: Freeze, T: Freeze,

§

impl<M, T> RefUnwindSafe for Request<M, T>

§

impl<M, T> Send for Request<M, T>
where M: Send, T: Send,

§

impl<M, T> Sync for Request<M, T>
where M: Sync, T: Sync,

§

impl<M, T> Unpin for Request<M, T>
where M: Unpin, T: Unpin,

§

impl<M, T> UnwindSafe for Request<M, T>
where M: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,