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
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
use jsonrpc_core::types::request::{MethodCall, Notification};
use jsonrpc_core::types::response::Output;
use serde::{Deserialize, Serialize};
use serde_json::Error;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Incoming {
Request(MethodCall),
Notification(Notification),
Response(Output),
#[serde(skip)]
Invalid(String),
}
impl Display for Incoming {
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
match *self {
Incoming::Request(ref req) => fmt.write_str(&serde_json::to_string(req).unwrap()),
Incoming::Notification(ref n) => fmt.write_str(&serde_json::to_string(n).unwrap()),
Incoming::Response(ref res) => fmt.write_str(&serde_json::to_string(res).unwrap()),
Incoming::Invalid(ref s) => fmt.write_str(s),
}
}
}
impl From<String> for Incoming {
fn from(s: String) -> Self {
Incoming::from_str(&s).unwrap_or_else(|_| Incoming::Invalid(s))
}
}
impl FromStr for Incoming {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}