1use std::pin::Pin;
2
3use futures::Sink;
4
5use crate::{Command, DeError};
6use futures::SinkExt;
7
8use super::{AsyncWriteConnection, MaybeSend};
9
10pub struct SinkCommandWrapper<'a, S>
11where
12 S: Sink<Command> + Unpin,
13 S::Error: Into<crate::DeError>,
14{
15 inner: &'a mut S,
16}
17
18impl<'a, S> From<&'a mut S> for SinkCommandWrapper<'a, S>
19where
20 S: Sink<Command, Error = DeError> + Unpin,
21{
22 fn from(value: &'a mut S) -> Self {
23 SinkCommandWrapper { inner: value }
24 }
25}
26
27impl<T> AsyncWriteConnection for T
28where
29 T: Sink<Command, Error = DeError> + Unpin + MaybeSend,
30 for<'a> &'a mut T: Into<SinkCommandWrapper<'a, T>>,
31{
32 fn shutdown(
33 &mut self,
34 ) -> impl std::future::Future<Output = Result<(), crate::DeError>> + MaybeSend {
35 async move { SinkCommandWrapper::from(self).inner.close().await }
36 }
37
38 fn write(
39 &mut self,
40 cmd: Command,
41 ) -> impl std::future::Future<Output = Result<(), crate::DeError>> + MaybeSend {
42 async move {
43 SinkCommandWrapper::from(self).inner.send(cmd).await?;
44 Ok(())
45 }
46 }
47}
48
49pub struct SinkStringWrapper<S>
50where
51 S: Sink<String> + Unpin,
52 S::Error: Into<crate::DeError>,
53{
54 inner: S,
55}
56
57impl<S> From<S> for SinkStringWrapper<S>
58where
59 S: Sink<String, Error = DeError> + Unpin,
60{
61 fn from(value: S) -> Self {
62 SinkStringWrapper { inner: value }
63 }
64}
65
66impl<S> Sink<Command> for SinkStringWrapper<S>
67where
68 S: Sink<String, Error = DeError> + Unpin,
69{
70 type Error = crate::DeError;
71
72 fn poll_ready(
73 mut self: std::pin::Pin<&mut Self>,
74 cx: &mut std::task::Context<'_>,
75 ) -> std::task::Poll<Result<(), Self::Error>> {
76 Pin::new(&mut self.inner).poll_ready(cx)
77 }
78
79 fn start_send(mut self: std::pin::Pin<&mut Self>, item: Command) -> Result<(), Self::Error> {
80 Pin::new(&mut self.inner).start_send(quick_xml::se::to_string(&item)?)
81 }
82
83 fn poll_flush(
84 mut self: std::pin::Pin<&mut Self>,
85 cx: &mut std::task::Context<'_>,
86 ) -> std::task::Poll<Result<(), Self::Error>> {
87 Pin::new(&mut self.inner).poll_flush(cx)
88 }
89
90 fn poll_close(
91 mut self: std::pin::Pin<&mut Self>,
92 cx: &mut std::task::Context<'_>,
93 ) -> std::task::Poll<Result<(), Self::Error>> {
94 Pin::new(&mut self.inner).poll_close(cx)
95 }
96}