sioc 0.4.0

Async Socket.IO client with type-safe event handling
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Typed Socket.IO acknowledgements (request/response over events).
//!
//! When one side emits an event and the other side calls the callback, the
//! response is called an **acknowledgement** ("ack").  On the wire, acks are
//! JSON arrays `[arg0, arg1, ...]`, like events, but without a leading name.
//!
//! The [`AckType`] trait (or `#[derive(AckType)]`) maps a Rust struct to this format.
//!
//! - [`Ack`]: inbound, decoded from a [`DynAck`] via [`TryFrom`].
//! - [`AckHandle`]: a future that resolves when the server's ack arrives.
//!
//! Outbound acks are sent directly via
//! [`SocketSender::acknowledge`](crate::client::SocketSender::acknowledge); blanket [`Acknowledge`] impls
//! handle serialization automatically for both plain acks and binary closures.

use crate::binary::AttachmentsBuilder;
use crate::client::Acknowledge;
use crate::error::{AckError, PayloadError};
use crate::marker::{BinaryMarker, HasBinary, NoBinary};
use crate::packet::Directive;
use crate::packet::DynAck;
use crate::payload::{DeserializePayload, SerializePayload, ack_from_json, ack_to_json};
use pin_project::pin_project;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::sync::oneshot;
use tokio::time::Instant;

/// Maps a Rust struct to the Socket.IO ack JSON-array encoding.
///
/// Prefer `#[derive(AckType)]` over a manual implementation.  The derive
/// generates the binary policy associated type.  Add `#[derive(SerializePayload)]`
/// to send acks and `#[derive(DeserializePayload)]` to receive them.
///
/// Unlike [`EventType`](crate::event::EventType), ack arrays have no leading name
/// element; the fields map directly to array positions.
///
/// # Example
///
/// ```rust
/// use sioc::prelude::*;
///
/// #[derive(AckType, SerializePayload)]
/// struct SaveAck { ok: bool, id: u64 }
///
/// fn main() {
///     assert_eq!(ack_to_json(&()).unwrap(), "[]");
///     assert_eq!(ack_to_json(&SaveAck { ok: true, id: 7 }).unwrap(), "[true,7]");
/// }
/// ```
pub trait AckType: Sized {
    /// Binary policy: [`NoBinary`] or [`HasBinary`].
    type Binary: BinaryMarker;
}

impl AckType for () {
    type Binary = NoBinary;
}

/// A typed inbound acknowledgement with a compile-time binary policy marker.
///
/// Construct via [`TryFrom<DynAck>`] after receiving a [`DynAck`]
/// from an ack oneshot channel. The binary policy is determined by `A::Binary`.
pub struct Ack<A>
where
    A: AckType,
{
    /// The deserialized ack payload.
    pub payload: A,
    /// Binary attachments (populated only when `A::Binary` = [`HasBinary`]).
    pub attachments: <A::Binary as BinaryMarker>::Attachments,
}

impl<A> std::fmt::Debug for Ack<A>
where
    A: std::fmt::Debug + AckType,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut map = f.debug_map();
        map.entry(&"payload", &self.payload);
        A::Binary::format(&self.attachments, &mut map);
        map.finish()
    }
}

impl<A> TryFrom<DynAck> for Ack<A>
where
    A: AckType + DeserializePayload,
{
    type Error = AckError;

    fn try_from(value: DynAck) -> Result<Self, AckError> {
        let payload = ack_from_json(&value.payload)?;
        let attachments = A::Binary::parse(value.attachments)?;
        Ok(Self {
            payload,
            attachments,
        })
    }
}

impl<A> Acknowledge<A, NoBinary> for A
where
    A: AckType<Binary = NoBinary> + SerializePayload,
{
    fn into_directive(self, id: u64) -> Result<Directive, PayloadError> {
        let payload = ack_to_json(&self)?.into();
        Ok(Directive::Ack {
            payload,
            id,
            attachments: None,
        })
    }
}

impl<F, A> Acknowledge<A, HasBinary> for F
where
    F: FnOnce(&mut AttachmentsBuilder) -> A,
    A: AckType<Binary = HasBinary> + SerializePayload,
{
    fn into_directive(self, id: u64) -> Result<Directive, PayloadError> {
        let mut builder = AttachmentsBuilder::new();
        let payload = ack_to_json(&self(&mut builder))?.into();
        Ok(Directive::Ack {
            payload,
            id,
            attachments: Some(builder.finish()),
        })
    }
}

/// Handle that resolves to [`Ack`] when the server's acknowledgement arrives.
///
/// Obtained from [`SocketSender::emit`](crate::client::SocketSender::emit).
/// Implements [`Future`]: `.await` it directly.
#[pin_project]
#[must_use = "AckHandle must be awaited to receive the ack"]
#[derive(Debug)]
pub struct AckHandle<A> {
    #[pin]
    rx: oneshot::Receiver<DynAck>,
    marker: PhantomData<A>,
}

impl<A: AckType> AckHandle<A> {
    /// Wraps a oneshot receiver into a typed ack handle.
    pub(crate) fn new(rx: oneshot::Receiver<DynAck>) -> Self {
        Self {
            rx,
            marker: PhantomData,
        }
    }
}
impl<A> AckHandle<A>
where
    A: AckType + DeserializePayload,
{
    /// Returns a future that resolves to [`AckError::Timeout`] if the server
    /// does not respond within `duration`.
    ///
    /// Mirrors [`tokio::time::timeout`].
    ///
    /// # Errors
    ///
    /// Returns [`AckError::Timeout`] if `duration` elapses, or any error from the inner [`AckHandle`].
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// // Requires a live Socket.IO connection; AckHandle is obtained from SocketSender::emit.
    /// use sioc::prelude::*;
    /// use std::time::Duration;
    ///
    /// async fn example(handle: AckHandle<()>) {
    ///     let result = handle.timeout(Duration::from_secs(5)).await;
    /// }
    /// ```
    pub async fn timeout(self, duration: Duration) -> Result<Ack<A>, AckError> {
        tokio::time::timeout(duration, self).await?
    }

    /// Returns a future that resolves to [`AckError::Timeout`] if the server
    /// does not respond by `deadline`.
    ///
    /// Mirrors [`tokio::time::timeout_at`].
    ///
    /// # Errors
    ///
    /// Returns [`AckError::Timeout`] if `deadline` passes, or any error from the inner [`AckHandle`].
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// // Requires a live Socket.IO connection; AckHandle is obtained from SocketSender::emit.
    /// use sioc::prelude::*;
    /// use tokio::time::{Instant, Duration};
    ///
    /// async fn example(handle: AckHandle<()>) {
    ///     let deadline = Instant::now() + Duration::from_secs(5);
    ///     let result = handle.timeout_at(deadline).await;
    /// }
    /// ```
    pub async fn timeout_at(self, deadline: Instant) -> Result<Ack<A>, AckError> {
        tokio::time::timeout_at(deadline, self).await?
    }
}

impl<A> Future for AckHandle<A>
where
    A: AckType + DeserializePayload,
{
    type Output = Result<Ack<A>, AckError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.project().rx.poll(cx)?.map(Ack::try_from)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::AckError;
    use crate::marker::{AckMarker, HasAck, HasBinary, NoBinary};
    use bytes::Bytes;
    use bytestring::ByteString;

    #[derive(Debug, PartialEq)]
    struct BinaryBoolAck(bool);

    impl AckType for BinaryBoolAck {
        type Binary = HasBinary;
    }

    impl SerializePayload for BinaryBoolAck {
        fn serialize_payload<S>(&self, seq: &mut S) -> std::result::Result<(), S::Error>
        where
            S: serde::ser::SerializeSeq,
        {
            seq.serialize_element(&self.0)?;
            Ok(())
        }
    }

    impl DeserializePayload for BinaryBoolAck {
        fn deserialize_payload<'de, S>(seq: &mut S) -> std::result::Result<Self, S::Error>
        where
            S: serde::de::SeqAccess<'de>,
        {
            let v = seq
                .next_element()?
                .ok_or_else(|| serde::de::Error::invalid_length(0, &"1 element"))?;
            Ok(Self(v))
        }
    }

    #[derive(Debug, PartialEq)]
    struct BinaryUnitAck;

    impl AckType for BinaryUnitAck {
        type Binary = HasBinary;
    }

    impl DeserializePayload for BinaryUnitAck {
        fn deserialize_payload<'de, S>(_seq: &mut S) -> std::result::Result<Self, S::Error>
        where
            S: serde::de::SeqAccess<'de>,
        {
            Ok(Self)
        }
    }

    #[test]
    fn deserialize_unit_ack() {
        assert_eq!(ack_from_json::<()>("[]").unwrap(), ());
    }

    #[test]
    fn from_ack_with_binary() {
        let attachment = Bytes::from_static(b"\xDE\xAD");
        let ack = DynAck {
            payload: ByteString::from_static("[true]"),
            attachments: Some(vec![attachment.clone()]),
        };
        let ack: Ack<BinaryBoolAck> = ack.try_into().unwrap();
        assert_eq!(ack.payload, BinaryBoolAck(true));
        assert_eq!(ack.attachments.len(), 1);
        assert_eq!(ack.attachments[0], attachment);
    }

    #[test]
    fn from_ack_missing_binary_fails() {
        let ack = DynAck {
            payload: ByteString::from_static("[]"),
            attachments: None,
        };
        let result: Result<Ack<BinaryUnitAck>, _> = ack.try_into();
        result.unwrap_err();
    }

    #[test]
    fn from_ack_unexpected_binary_fails() {
        let ack = DynAck {
            payload: ByteString::from_static("[]"),
            attachments: Some(vec![Bytes::from_static(b"x")]),
        };
        let result: Result<Ack<()>, _> = ack.try_into();
        result.unwrap_err();
    }
    #[test]
    fn send_ack_into_directive_binary() {
        let id = <HasAck<BinaryBoolAck>>::parse(Some(3)).unwrap();
        let directive = Acknowledge::<BinaryBoolAck, HasBinary>::into_directive(
            |builder: &mut AttachmentsBuilder| {
                let _p = builder.attach(Bytes::from_static(b"\xCA\xFE"));
                BinaryBoolAck(true)
            },
            id.get(),
        )
        .unwrap();
        let Directive::Ack {
            payload,
            id,
            attachments,
        } = directive
        else {
            panic!("expected Ack directive");
        };
        assert_eq!(&payload[..], "[true]");
        assert_eq!(id, 3);
        let att = attachments.expect("expected attachments");
        assert_eq!(att.len(), 1);
        assert_eq!(att[0], Bytes::from_static(b"\xCA\xFE"));
    }

    #[tokio::test]
    async fn ack_handle_closed_channel_errors() {
        let (tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        drop(tx);

        let result: Result<Ack<()>, _> = handle.await;
        result.unwrap_err();
    }

    #[test]
    fn send_ack_into_directive_no_binary() {
        let directive = Acknowledge::<(), NoBinary>::into_directive((), 7).unwrap();
        let Directive::Ack {
            payload,
            id,
            attachments,
        } = directive
        else {
            panic!("expected Ack directive");
        };
        assert_eq!(&payload[..], "[]");
        assert_eq!(id, 7);
        assert!(attachments.is_none());
    }

    #[tokio::test]
    async fn ack_handle_resolves_on_send() {
        let (tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        tx.send(DynAck {
            payload: ByteString::from_static("[]"),
            attachments: None,
        })
        .unwrap();
        let result: Result<Ack<()>, _> = handle.await;
        result.unwrap();
    }

    #[tokio::test]
    async fn ack_handle_timeout_expires() {
        use std::time::Duration;
        let (_tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        let result = handle.timeout(Duration::from_millis(1)).await;
        assert!(matches!(result, Err(AckError::Timeout(_))));
    }

    #[tokio::test]
    async fn ack_handle_timeout_resolves() {
        use std::time::Duration;
        let (tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        tx.send(DynAck {
            payload: ByteString::from_static("[]"),
            attachments: None,
        })
        .unwrap();
        let result = handle.timeout(Duration::from_secs(5)).await;
        result.unwrap();
    }

    #[tokio::test]
    async fn ack_handle_timeout_at_resolves() {
        use std::time::Duration;
        use tokio::time::Instant;
        let (tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        tx.send(DynAck {
            payload: ByteString::from_static("[]"),
            attachments: None,
        })
        .unwrap();
        let deadline = Instant::now() + Duration::from_secs(5);
        let result = handle.timeout_at(deadline).await;
        result.unwrap();
    }

    #[tokio::test]
    async fn ack_handle_timeout_at_past_deadline_times_out() {
        use std::time::Duration;
        use tokio::time::Instant;
        let (_tx, rx) = oneshot::channel::<DynAck>();
        let handle = AckHandle::<()>::new(rx);
        let deadline = Instant::now() - Duration::from_secs(1);
        let result = handle.timeout_at(deadline).await;
        assert!(matches!(result, Err(AckError::Timeout(_))));
    }

    #[test]
    fn ack_no_binary_debug() {
        let ack = Ack::<()> {
            payload: (),
            attachments: (),
        };
        let s = format!("{ack:?}");
        assert!(s.contains("payload"));
    }

    #[test]
    fn ack_has_binary_debug() {
        let ack = Ack::<BinaryUnitAck> {
            payload: BinaryUnitAck,
            attachments: vec![Bytes::from_static(b"x")],
        };
        let s = format!("{ack:?}");
        assert!(s.contains("count"));
    }
}