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
use crate::error::Error;
use crate::model::Page;
use crate::request::impls::Paginated;
use crate::request::Endpoint;
use crate::request::RequestWithState;
use crate::result::Result;
use futures::stream::Stream;
use serde::de::DeserializeOwned;
use std::future::Future;
use std::ops::Range;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use std::vec::IntoIter;
#[cfg(target_arch = "wasm32")]
type FutureType<T> = dyn Future<Output = Result<Page<T>>>;
#[cfg(not(target_arch = "wasm32"))]
type FutureType<T> = dyn Future<Output = Result<Page<T>>> + Send;
pub struct PageStream<T, S>
where
RequestWithState<T, S>: Paginated + Clone + Endpoint,
T: DeserializeOwned + Clone,
S: Sync + Send + Clone + 'static,
{
request: RequestWithState<T, S>,
future: Option<Pin<Box<FutureType<T>>>>,
pages: Option<Range<usize>>,
iter: Option<IntoIter<T>>,
}
impl<T, S> From<RequestWithState<T, S>> for PageStream<T, S>
where
RequestWithState<T, S>: Paginated + Clone + Endpoint,
T: DeserializeOwned + Sync + Send + Clone + 'static,
S: Sync + Send + Clone + 'static,
{
fn from(request: RequestWithState<T, S>) -> Self {
let request_clone = request.clone();
Self {
request,
future: Some(Box::pin(request_clone.page(0))),
pages: None,
iter: None,
}
}
}
impl<T, S> PageStream<T, S>
where
RequestWithState<T, S>: Endpoint + Paginated + Unpin + Sync + Send + Clone,
T: DeserializeOwned + Unpin + Sync + Send + Clone + 'static,
S: Sync + Send + Clone + 'static,
{
fn poll_item(&mut self) -> Option<T> {
self.iter.as_mut().and_then(|x| x.next())
}
fn poll_future(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<T>>> {
if let Some(future) = self.future.as_mut() {
match Pin::new(future).poll(cx) {
Poll::Ready(Ok(page)) => self.on_new_page(page),
Poll::Ready(Err(err)) => self.on_error(err),
Poll::Pending => Poll::Pending,
}
} else {
Poll::Ready(None)
}
}
fn on_new_page(
mut self: Pin<&mut Self>,
page: Page<T>,
) -> Poll<Option<Result<T>>> {
let request = self.request.clone();
let pages = self.pages.get_or_insert(1..page.total_pages);
self.future = match pages.next() {
Some(x) => Some(Box::pin(request.page(x))),
None => None,
};
let mut iter = page.content.into_iter();
let first_item = iter.next().map(Ok);
self.iter = Some(iter);
Poll::Ready(first_item)
}
fn on_error(
mut self: Pin<&mut Self>,
err: Error,
) -> Poll<Option<Result<T>>> {
self.future = None;
self.pages = None;
Poll::Ready(Some(Err(err)))
}
}
impl<T, S> Stream for PageStream<T, S>
where
RequestWithState<T, S>: Endpoint + Paginated + Unpin + Sync + Send + Clone,
T: DeserializeOwned + Unpin + Send + Sync + Clone + 'static,
S: Sync + Send + Clone + 'static,
{
type Item = Result<T>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
if let Some(item) = self.poll_item() {
Poll::Ready(Some(Ok(item)))
} else {
self.poll_future(cx)
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
unimplemented!();
}
}