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
use super::{Connection, ConnectionError, Request, Subscribe};
use crate::rpc::eth_unsubscribe;
use crate::types::U128;
use log::{error, info, trace};
use serde::de::DeserializeOwned;
use std::fmt::Debug;
use std::marker::PhantomData;
use thiserror::Error;
pub struct Subscription<T: Subscribe + Request, U: DeserializeOwned + Debug> {
pub id: U128,
pub(crate) connection: Connection<T>,
pub(crate) result_type: PhantomData<U>,
}
impl<T: Subscribe + Request, U: DeserializeOwned + Debug> Subscription<T, U> {
pub fn next_item(&mut self) -> Result<U, SubscriptionError> {
trace!("Fetching next item from subscription");
let response = self.connection.transport.read_next()?;
deserialize_from_sub(&response)
}
pub fn close(self) {
info!("Closing subscription with id {}", self.id);
}
}
impl<T: Subscribe + Request, U: DeserializeOwned + Debug> Drop for Subscription<T, U> {
fn drop(&mut self) {
match self.connection.call(eth_unsubscribe(self.id)) {
Ok(true) => (),
Ok(_) => error!("Unable to cancel subscription"),
Err(err) => error!("{}", err),
}
}
}
fn deserialize_from_sub<U: DeserializeOwned + Debug>(
response: &str,
) -> Result<U, SubscriptionError> {
trace!("Deserializing response {}", response);
let value: serde_json::Value = serde_json::from_str(response)?;
serde_json::from_value::<U>(value["params"]["result"].clone()).map_err(SubscriptionError::from)
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Error)]
pub enum SubscriptionError {
#[error("Subscription Transport Error {0}")]
Read(#[from] ConnectionError),
#[error("Subscription Error during canceling subscription")]
Cancel,
#[error("Subscription De-/Serialization Error: {0}")]
Serde(#[from] serde_json::Error),
}