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
use futures::Stream;
use pin_project::pin_project;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use thiserror::Error;

mod cache;
mod tree;

pub use cache::{Cache, CacheBuilder};

pub type Result<T> = std::result::Result<T, Error>;
pub type SharedChildData = Arc<ChildData>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("zk error: {0}")]
    ZK(#[from] zookeeper_client::Error),
}

//todo remove stat
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ChildData {
    pub path: String,
    pub data: Vec<u8>,
    pub stat: zookeeper_client::Stat,
}

#[derive(Debug, Clone)]
pub enum Event {
    Add(SharedChildData),
    Delete(SharedChildData),
    Update {
        old: SharedChildData,
        new: SharedChildData,
    },
}

#[pin_project]
pub(crate) struct EventStream<T> {
    #[pin]
    pub(crate) watcher: tokio::sync::mpsc::UnboundedReceiver<T>,
}

impl<T> Stream for EventStream<T> {
    type Item = T;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        this.watcher.as_mut().poll_recv(cx)
    }
}