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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::ErrorPayload;
use serde::{de::DeserializeOwned, Deserialize};
use serde_json::value::RawValue;
use std::borrow::Borrow;

/// A JSONRPC-2.0 response payload.
///
/// This enum covers both the success and error cases of a JSONRPC-2.0
/// response. It is used to represent the `result` and `error` fields of a
/// response object.
///
/// ### Note
///
/// This type does not implement `Serialize` or `Deserialize` directly. It is
/// deserialized as part of the [`Response`] type.
///
/// [`Response`]: crate::Response
#[derive(Clone, Debug)]
pub enum ResponsePayload<Payload = Box<RawValue>, ErrData = Box<RawValue>> {
    /// A successful response payload.
    Success(Payload),
    /// An error response payload.
    Failure(ErrorPayload<ErrData>),
}

/// A [`ResponsePayload`] that has been partially deserialized, borrowing its
/// contents from the deserializer. This is used primarily for intermediate
/// deserialization. Most users will not require it.
///
/// See the [top-level docs] for more info.
///
/// [top-level docs]: crate
pub type BorrowedResponsePayload<'a> = ResponsePayload<&'a RawValue, &'a RawValue>;

impl BorrowedResponsePayload<'_> {
    /// Convert this borrowed response payload into an owned payload by copying
    /// the data from the deserializer (if necessary).
    pub fn into_owned(self) -> ResponsePayload {
        match self {
            Self::Success(payload) => ResponsePayload::Success(payload.to_owned()),
            Self::Failure(error) => ResponsePayload::Failure(error.into_owned()),
        }
    }
}

impl<Payload, ErrData> ResponsePayload<Payload, ErrData> {
    /// Fallible conversion to the successful payload.
    pub const fn as_success(&self) -> Option<&Payload> {
        match self {
            Self::Success(payload) => Some(payload),
            _ => None,
        }
    }

    /// Fallible conversion to the error object.
    pub const fn as_error(&self) -> Option<&ErrorPayload<ErrData>> {
        match self {
            Self::Failure(payload) => Some(payload),
            _ => None,
        }
    }

    /// Returns `true` if the response payload is a success.
    pub const fn is_success(&self) -> bool {
        matches!(self, Self::Success(_))
    }

    /// Returns `true` if the response payload is an error.
    pub const fn is_error(&self) -> bool {
        matches!(self, Self::Failure(_))
    }
}

impl<'a, Payload, ErrData> ResponsePayload<Payload, ErrData>
where
    Payload: AsRef<RawValue> + 'a,
{
    /// Attempt to deserialize the success payload, borrowing from the payload
    /// if necessary.
    ///
    /// # Returns
    /// - `None` if the payload is an error
    /// - `Some(Ok(T))` if the payload is a success and can be deserialized
    /// - `Some(Err(serde_json::Error))` if the payload is a success and can't be deserialized as
    ///   `T`
    pub fn try_success_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
        self.as_success().map(|payload| serde_json::from_str(payload.as_ref().get()))
    }

    /// Deserialize a Success payload, if possible, transforming this type.
    ///
    /// # Returns
    ///
    /// - `Ok(ResponsePayload<T>)` if the payload is an error, or if the payload is a success and
    ///   can be deserialized as `T`
    /// - `Err(self)` if the payload is a success and can't be deserialized
    pub fn deserialize_success<T: DeserializeOwned>(
        self,
    ) -> Result<ResponsePayload<T, ErrData>, Self> {
        match self {
            Self::Success(ref payload) => serde_json::from_str(payload.as_ref().get())
                .map_or_else(|_| Err(self), |payload| Ok(ResponsePayload::Success(payload))),
            Self::Failure(e) => Ok(ResponsePayload::Failure(e)),
        }
    }
}

impl<'a, Payload, Data> ResponsePayload<Payload, Data>
where
    Data: Borrow<RawValue> + 'a,
{
    /// Attempt to deserialize the error payload, borrowing from the payload if
    /// necessary.
    ///
    /// # Returns
    /// - `None` if the payload is a success
    /// - `Some(Ok(T))` if the payload is an error and can be deserialized
    /// - `Some(Err(serde_json::Error))` if the payload is an error and can't be deserialized as `T`
    pub fn try_error_as<T: Deserialize<'a>>(&'a self) -> Option<serde_json::Result<T>> {
        self.as_error().and_then(|error| error.try_data_as::<T>())
    }

    /// Deserialize an Error payload, if possible, transforming this type.
    ///
    /// # Returns
    ///
    /// - `Ok(ResponsePayload<Payload, T>)` if the payload is an error, or if the payload is an
    ///   error and can be deserialized as `T`.
    /// - `Err(self)` if the payload is an error and can't be deserialized.
    pub fn deserialize_error<T: DeserializeOwned>(
        self,
    ) -> Result<ResponsePayload<Payload, T>, Self> {
        match self {
            Self::Failure(err) => match err.deser_data() {
                Ok(deser) => Ok(ResponsePayload::Failure(deser)),
                Err(err) => Err(Self::Failure(err)),
            },
            Self::Success(payload) => Ok(ResponsePayload::Success(payload)),
        }
    }
}