1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2019 Boyu Yang
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use core::convert::{TryFrom, TryInto};

use jsonrpc_core::{
    id::Id,
    params::Params,
    request::{Call, MethodCall, Notification, Request},
    response::{Failure, Output, Response, Success},
    types::to_string,
    version::Version,
    Value,
};
use log::trace;

use crate::{Error, Result};

pub struct CommonPart {
    pub jsonrpc: Option<Version>,
    pub id_opt: Option<Id>,
}

impl Default for CommonPart {
    fn default() -> Self {
        Self::num(0)
    }
}

impl CommonPart {
    pub fn num(n: u64) -> Self {
        Self {
            jsonrpc: Some(Version::V2),
            id_opt: Some(Id::Num(n)),
        }
    }

    pub fn str(s: String) -> Self {
        Self {
            jsonrpc: Some(Version::V2),
            id_opt: Some(Id::Str(s)),
        }
    }
}

pub trait JsonRpcRequest
where
    Self: TryInto<Params> + Sized,
{
    type Output: TryFrom<Value>;

    fn method() -> &'static str;

    fn to_string(self, c: CommonPart) -> Result<String> {
        self.to_single_request(c).and_then(|sc| {
            to_string(&sc).map_err(|_| Error::serde("failed to convert a single request to string"))
        })
    }

    fn to_single_request(self, c: CommonPart) -> Result<Request> {
        self.to_call(c).map(Request::Single)
    }

    fn to_call(self, c: CommonPart) -> Result<Call> {
        let CommonPart { jsonrpc, id_opt } = c;
        let method = Self::method().to_owned();
        self.try_into()
            .map_err(|_| Error::serde("failed to parse a request core"))
            .map(|params: Params| {
                if let Some(id) = id_opt {
                    Call::MethodCall(MethodCall {
                        jsonrpc,
                        method,
                        params,
                        id,
                    })
                } else {
                    Call::Notification(Notification {
                        jsonrpc,
                        method,
                        params,
                    })
                }
            })
    }

    fn parse_single_response(response: Response) -> Result<Self::Output> {
        match response {
            Response::Single(output) => match output {
                Output::Success(success) => {
                    let Success {
                        jsonrpc,
                        result,
                        id,
                    } = success;
                    trace!("Success {{ jsonrpc: {:#?}, id: {:#?} }}", jsonrpc, id);
                    result
                        .try_into()
                        .map_err(|_| Error::custom("failed to parse the result"))
                }
                Output::Failure(failure) => {
                    let Failure { jsonrpc, error, id } = failure;
                    trace!("Failure {{ jsonrpc: {:#?}, id: {:#?} }}", jsonrpc, id);
                    Err(error.into())
                }
            },
            Response::Batch(_) => Err(Error::custom("could not be a batch response")),
        }
    }
}