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
use std::sync::Arc;
use async_trait::async_trait;
use crate::api::Api;
use crate::cursor::Cursor;

pub type StreamDataResult<S> = Vec<Arc<<S as Stream>::Data>>;
pub type StreamResult<S> = Result<StreamDataResult<S>, <S as Stream>::Error>;

#[async_trait]
pub trait Stream where Self: Sized + Send + Sync {
    type Data: Send + Sync;
    type Error: Send + Sync;

    async fn progress(&mut self) -> StreamResult<Self>;
    fn can_progress(&self) -> bool;

    async fn next(&mut self) -> Option<StreamResult<Self>> {
        if self.can_progress() {
            Some(self.progress().await)
        } else {
            None
        }
    }

    async fn collect(&mut self) -> StreamResult<Self> {
        let mut result = Vec::new();
        while let Some(data) = self.next().await {
            result.append(&mut data?);
        }
        Ok(result)
    }

    async fn for_each<Cb: FnMut(StreamDataResult<Self>) + Send + Sync>(&mut self, mut callback: Cb) -> Result<(), Self::Error> {
        while let Some(data) = self.next().await {
            callback(data?);
        }
        Ok(())
    }
}

pub type GeneralStreamResult<S> = Result<Vec<Arc<<S as GeneralStreamGen>::Data>>, <S as GeneralStreamGen>::Error>;

// region: GeneralStream
#[async_trait]
pub trait GeneralStreamGen {
    type Data: Send + Sync;
    type Error: Send + Sync;
    type This: Send + Sync;
    async fn gen(&self, cursor: Cursor, this: &Arc<Self::This>, api: &Arc<Api>) -> GeneralStreamResult<Self>;
}

pub struct GeneralStream<G: GeneralStreamGen + Clone + Send + Sync> {
    api: Arc<Api>,
    this: Arc<G::This>,
    cursor: Cursor,
    gen: G,
}

impl<G: GeneralStreamGen + Clone + Send + Sync> GeneralStream<G> {
    pub fn with_this(gen: G, cursor: Cursor, this: Arc<G::This>, api: Arc<Api>) -> Self {
        Self {
            api,
            cursor,
            gen,
            this
        }
    }
}

#[async_trait]
impl<G: GeneralStreamGen + Clone + Send + Sync> Stream for GeneralStream<G> {
    type Data = G::Data;
    type Error = G::Error;
    async fn progress(&mut self) -> StreamResult<Self> {
        let result = self.gen.gen(self.cursor.clone(), &self.this, &self.api).await?;
        self.cursor.progress(result.len());
        if result.is_empty() {
            self.cursor.kill();
        }
        Ok(result)
    }
    fn can_progress(&self) -> bool {
        self.cursor.can_progress()
    }
}
// endregion: GeneralStream