Skip to main content

plexus_client/
frame.rs

1//! What a client stream yields once its caller opts into progress.
2
3use crate::Progress;
4
5/// One frame of a call whose caller asked to see progress: an item, or a
6/// progress report.
7///
8/// `I` is the method's item (a plain `T`, or a `Result<T, E>`); `P` is its
9/// progress type ([`Note`](crate::Note) unless the method declares its own).
10/// A caller that does not opt in never sees a `Frame` — its stream yields
11/// `I` directly and progress is dropped.
12///
13/// Client-side only; never serialized.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum Frame<I, P> {
16    /// One of the method's items (a data frame).
17    Item(I),
18    /// A progress report (a progress frame).
19    Progress(P),
20}
21
22impl<I, P> Frame<I, P> {
23    /// `true` for [`Frame::Item`].
24    pub fn is_item(&self) -> bool {
25        matches!(self, Frame::Item(_))
26    }
27
28    /// `true` for [`Frame::Progress`].
29    pub fn is_progress(&self) -> bool {
30        matches!(self, Frame::Progress(_))
31    }
32
33    /// Borrow the item, if this is one.
34    pub fn item(&self) -> Option<&I> {
35        match self {
36            Frame::Item(i) => Some(i),
37            Frame::Progress(_) => None,
38        }
39    }
40
41    /// The item, if this is one; a progress report is dropped.
42    pub fn into_item(self) -> Option<I> {
43        match self {
44            Frame::Item(i) => Some(i),
45            Frame::Progress(_) => None,
46        }
47    }
48
49    /// The progress report, if this is one; an item is dropped.
50    pub fn into_progress(self) -> Option<P> {
51        match self {
52            Frame::Progress(p) => Some(p),
53            Frame::Item(_) => None,
54        }
55    }
56}
57
58/// What the server sends as `Progress<T, P>` arrives as `Frame<T, P>`:
59/// `Update` ⇄ `Progress`, `Item` ⇄ `Item`.
60impl<T, P> From<Progress<T, P>> for Frame<T, P> {
61    fn from(p: Progress<T, P>) -> Self {
62        match p {
63            Progress::Update(p) => Frame::Progress(p),
64            Progress::Item(t) => Frame::Item(t),
65        }
66    }
67}