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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::{error::SubscriptionClosedError, v2::params::SubscriptionId, Error};
use core::marker::PhantomData;
use futures_channel::{mpsc, oneshot};
use futures_util::{future::FutureExt, sink::SinkExt, stream::StreamExt};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value as JsonValue;
#[derive(Debug)]
#[non_exhaustive]
pub enum SubscriptionKind {
	
	Subscription(SubscriptionId),
	
	Method(String),
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum NotifResponse<Notif> {
	Ok(Notif),
	Err(SubscriptionClosedError),
}
#[derive(Debug)]
pub struct Subscription<Notif> {
	
	to_back: mpsc::Sender<FrontToBack>,
	
	notifs_rx: mpsc::Receiver<JsonValue>,
	
	kind: SubscriptionKind,
	
	marker: PhantomData<Notif>,
}
impl<Notif> Subscription<Notif> {
	
	pub fn new(
		to_back: mpsc::Sender<FrontToBack>,
		notifs_rx: mpsc::Receiver<JsonValue>,
		kind: SubscriptionKind,
	) -> Self {
		Self { to_back, notifs_rx, kind, marker: PhantomData }
	}
}
#[derive(Debug)]
pub struct BatchMessage {
	
	pub raw: String,
	
	pub ids: Vec<u64>,
	
	pub send_back: oneshot::Sender<Result<Vec<JsonValue>, Error>>,
}
#[derive(Debug)]
pub struct RequestMessage {
	
	pub raw: String,
	
	pub id: u64,
	
	pub send_back: Option<oneshot::Sender<Result<JsonValue, Error>>>,
}
#[derive(Debug)]
pub struct SubscriptionMessage {
	
	pub raw: String,
	
	pub subscribe_id: u64,
	
	pub unsubscribe_id: u64,
	
	pub unsubscribe_method: String,
	
	
	
	pub send_back: oneshot::Sender<Result<(mpsc::Receiver<JsonValue>, SubscriptionId), Error>>,
}
#[derive(Debug)]
pub struct RegisterNotificationMessage {
	
	pub method: String,
	
	
	
	pub send_back: oneshot::Sender<Result<(mpsc::Receiver<JsonValue>, String), Error>>,
}
#[derive(Debug)]
pub enum FrontToBack {
	
	Batch(BatchMessage),
	
	Notification(String),
	
	Request(RequestMessage),
	
	Subscribe(SubscriptionMessage),
	
	RegisterNotification(RegisterNotificationMessage),
	
	UnregisterNotification(String),
	
	
	
	
	
	SubscriptionClosed(SubscriptionId),
}
impl<Notif> Subscription<Notif>
where
	Notif: DeserializeOwned,
{
	
	
	
	pub async fn next(&mut self) -> Result<Option<Notif>, Error> {
		match self.notifs_rx.next().await {
			Some(n) => match serde_json::from_value::<NotifResponse<Notif>>(n) {
				Ok(NotifResponse::Ok(parsed)) => Ok(Some(parsed)),
				Ok(NotifResponse::Err(e)) => Err(Error::SubscriptionClosed(e)),
				Err(e) => Err(e.into()),
			},
			None => Ok(None),
		}
	}
}
impl<Notif> Drop for Subscription<Notif> {
	fn drop(&mut self) {
		
		
		
		
		let kind = std::mem::replace(&mut self.kind, SubscriptionKind::Subscription(SubscriptionId::Num(0)));
		let msg = match kind {
			SubscriptionKind::Method(notif) => FrontToBack::UnregisterNotification(notif),
			SubscriptionKind::Subscription(sub_id) => FrontToBack::SubscriptionClosed(sub_id),
		};
		let _ = self.to_back.send(msg).now_or_never();
	}
}