zookeeper_cache_rust/
lib.rs

1use futures::Stream;
2use pin_project::pin_project;
3use std::pin::Pin;
4use std::sync::Arc;
5use std::task::{Context, Poll};
6use thiserror::Error;
7
8mod cache;
9mod tree;
10
11pub use cache::{Cache, CacheBuilder};
12
13pub type Result<T> = std::result::Result<T, Error>;
14pub type SharedChildData = Arc<ChildData>;
15
16#[derive(Error, Debug)]
17pub enum Error {
18    #[error("zk error: {0}")]
19    ZK(#[from] zookeeper_client::Error),
20}
21
22//todo remove stat
23#[derive(Debug, PartialEq, Eq, Clone)]
24pub struct ChildData {
25    pub path: String,
26    pub data: Vec<u8>,
27    pub stat: zookeeper_client::Stat,
28}
29
30#[derive(Debug, Clone)]
31pub enum Event {
32    Add(SharedChildData),
33    Delete(SharedChildData),
34    Update {
35        old: SharedChildData,
36        new: SharedChildData,
37    },
38}
39
40#[pin_project]
41pub(crate) struct EventStream<T> {
42    #[pin]
43    pub(crate) watcher: tokio::sync::mpsc::UnboundedReceiver<T>,
44}
45
46impl<T> Stream for EventStream<T> {
47    type Item = T;
48
49    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
50        let mut this = self.project();
51        this.watcher.as_mut().poll_recv(cx)
52    }
53}