krab-api 0.2.3

Kubernetes-compatible API server for krabkube
Documentation
use bytes::Bytes;
use http_body::{Body as HttpBody, Frame};
use std::convert::Infallible;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc;

pub enum Body {
    Full(Option<Bytes>),
    Chan(mpsc::Receiver<Bytes>),
}

impl Body {
    pub fn full(b: impl Into<Bytes>) -> Self {
        Body::Full(Some(b.into()))
    }
}

impl HttpBody for Body {
    type Data = Bytes;
    type Error = Infallible;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
        match &mut *self {
            Body::Full(b) => Poll::Ready(
                b.take()
                    .filter(|b| !b.is_empty())
                    .map(|b| Ok(Frame::data(b))),
            ),
            Body::Chan(rx) => rx.poll_recv(cx).map(|o| o.map(|b| Ok(Frame::data(b)))),
        }
    }
}