meteroid-rs 0.31.4

Official Rust SDK for Meteroid, the open-source billing platform: subscriptions, invoicing, usage-based billing and webhooks.
Documentation
// this file is @generated by Perseid; edit the shared runtime template.
//! Incremental SSE decoding. Connections are never automatically reconnected.
use crate::{
    error::Error,
    request::{decode_error, transport_error},
};
use bytes::{Buf, Bytes};
use hyper::body::{Body as _, Incoming};
use serde::de::DeserializeOwned;
use std::{
    fmt, io,
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};

/// One SSE event. Data lines are joined with a newline; metadata is not JSON-decoded.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SseEvent {
    /// The event type, `message` unless the server named one.
    pub event: String,
    /// The data lines, joined with a newline.
    pub data: String,
    /// Last event ID, including updates from events with no data.
    pub id: Option<String>,
    /// Reconnection delay sent with this event. The SDK does not reconnect automatically.
    pub retry: Option<Duration>,
}

impl SseEvent {
    /// An event of type `message` carrying `data`.
    #[must_use]
    pub fn new(data: impl Into<String>) -> Self {
        Self {
            event: "message".into(),
            data: data.into(),
            id: None,
            retry: None,
        }
    }

    /// The event with its type set.
    #[must_use]
    pub fn with_event(mut self, event: impl Into<String>) -> Self {
        self.event = event.into();
        self
    }

    /// The event with its id set.
    #[must_use]
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// The event with its reconnection delay set.
    #[must_use]
    pub fn with_retry(mut self, retry: Duration) -> Self {
        self.retry = Some(retry);
        self
    }
}

/// What one raw event decodes to.
enum Step<T> {
    Item(T),
    Done,
}

type Decode<T> = fn(&SseEvent) -> Result<Step<T>, Error>;

#[allow(clippy::unnecessary_wraps)]
fn raw(event: &SseEvent) -> Result<Step<SseEvent>, Error> {
    Ok(Step::Item(event.clone()))
}

fn json<T: DeserializeOwned>(event: &SseEvent) -> Result<Step<T>, Error> {
    match event.data.as_str() {
        "[DONE]" => Ok(Step::Done),
        data => serde_json::from_str(data)
            .map(Step::Item)
            .map_err(decode_error),
    }
}

/// Live response stream, of [`SseEvent`]s or of the JSON model each event carries. It is a
/// `futures_core::Stream`. Dropping it closes the response; cancelling `next()` is safe.
///
/// A typed stream ends at a `[DONE]` event, and [`last_event`](Self::last_event) gives the raw
/// event of the last item, with its type and id.
///
/// The client timeout covers opening this stream. Use `tokio::time::timeout`
/// around `next()` when an idle timeout is desired. On a body or parsing error,
/// one error is returned and the stream terminates. EOF discards incomplete events.
pub struct EventStream<T = SseEvent> {
    body: Option<Incoming>,
    pending: Bytes,
    parser: Parser,
    last: Option<SseEvent>,
    decode: Decode<T>,
}

impl EventStream<SseEvent> {
    /// The stream of `pending` bytes, then of `body` when the server left it open.
    pub(crate) fn new(body: Option<Incoming>, pending: Bytes) -> Self {
        Self {
            body,
            pending,
            parser: Parser::default(),
            last: None,
            decode: raw,
        }
    }

    pub(crate) fn typed<T: DeserializeOwned>(self) -> EventStream<T> {
        self.decoding(json::<T>)
    }
}

impl<T> EventStream<T> {
    fn decoding<U>(self, decode: Decode<U>) -> EventStream<U> {
        EventStream {
            body: self.body,
            pending: self.pending,
            parser: self.parser,
            last: self.last,
            decode,
        }
    }

    /// Maximum buffered event/line size in bytes (default 1 MiB).
    #[must_use]
    pub fn with_max_event_bytes(mut self, limit: usize) -> Self {
        self.parser.limit = limit;
        self
    }

    /// The id of the last event, the `Last-Event-ID` to resume from.
    #[must_use]
    pub fn last_event_id(&self) -> Option<&str> {
        self.parser.id.as_deref()
    }

    /// The reconnection delay the server last asked for.
    #[must_use]
    pub fn retry_delay(&self) -> Option<Duration> {
        self.parser.retry
    }

    /// The raw event the last item was read from.
    #[must_use]
    pub fn last_event(&self) -> Option<&SseEvent> {
        self.last.as_ref()
    }

    /// The rest of the stream as raw events, without decoding their data.
    #[must_use]
    pub fn into_raw(self) -> EventStream<SseEvent> {
        self.decoding(raw)
    }

    /// The next item, `None` once the stream ended.
    pub async fn next(&mut self) -> Option<Result<T, Error>> {
        std::future::poll_fn(|cx| self.poll_item(cx)).await
    }

    fn poll_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<T, Error>>> {
        let event = match std::task::ready!(self.poll_event(cx)) {
            Some(Ok(event)) => event,
            Some(Err(error)) => return Poll::Ready(Some(Err(error))),
            None => return Poll::Ready(None),
        };
        let step = (self.decode)(&event);
        self.last = Some(event);
        Poll::Ready(match step {
            Ok(Step::Item(item)) => Some(Ok(item)),
            Ok(Step::Done) => {
                self.close();
                None
            }
            Err(error) => {
                self.close();
                Some(Err(error))
            }
        })
    }

    fn close(&mut self) {
        self.body = None;
        self.pending = Bytes::new();
    }

    fn poll_event(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<SseEvent, Error>>> {
        loop {
            while !self.pending.is_empty() {
                let byte = self.pending[0];
                self.pending.advance(1);
                match self.parser.push(byte) {
                    Ok(Some(event)) => return Poll::Ready(Some(Ok(event))),
                    Ok(None) => {}
                    Err(error) => {
                        self.close();
                        return Poll::Ready(Some(Err(decode_error(error))));
                    }
                }
            }
            let Some(body) = self.body.as_mut() else {
                return Poll::Ready(None);
            };
            match std::task::ready!(Pin::new(body).poll_frame(cx)) {
                Some(Ok(frame)) => {
                    if let Ok(bytes) = frame.into_data() {
                        self.pending = bytes;
                    }
                }
                Some(Err(error)) => {
                    self.close();
                    return Poll::Ready(Some(Err(transport_error(error))));
                }
                None => {
                    self.close();
                    return Poll::Ready(None);
                }
            }
        }
    }
}

impl<T> futures_core::Stream for EventStream<T> {
    type Item = Result<T, Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.get_mut().poll_item(cx)
    }
}

impl<T> fmt::Debug for EventStream<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EventStream")
            .field("last_event_id", &self.last_event_id())
            .finish_non_exhaustive()
    }
}

struct Parser {
    line: Vec<u8>,
    data: String,
    event: String,
    id: Option<String>,
    retry: Option<Duration>,
    event_retry: Option<Duration>,
    after_cr: bool,
    first_line: bool,
    limit: usize,
}

impl Default for Parser {
    fn default() -> Self {
        Self {
            line: Vec::new(),
            data: String::new(),
            event: String::new(),
            id: None,
            retry: None,
            event_retry: None,
            after_cr: false,
            first_line: true,
            limit: 1024 * 1024,
        }
    }
}

impl Parser {
    fn push(&mut self, byte: u8) -> io::Result<Option<SseEvent>> {
        if self.after_cr {
            self.after_cr = false;
            if byte == b'\n' {
                return Ok(None);
            }
        }
        if byte != b'\n' && byte != b'\r' {
            if self.line.len()
                + self.data.len()
                + self.event.len()
                + self.id.as_ref().map_or(0, String::len)
                >= self.limit
            {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "SSE event exceeds the configured size limit",
                ));
            }
            self.line.push(byte);
            return Ok(None);
        }
        self.after_cr = byte == b'\r';
        let line = String::from_utf8_lossy(&self.line);
        let line = if self.first_line {
            line.strip_prefix('\u{feff}').unwrap_or(&line)
        } else {
            &line
        };
        self.first_line = false;
        let result = if line.is_empty() {
            let event = std::mem::take(&mut self.event);
            if self.data.is_empty() {
                None
            } else {
                let retry = self.event_retry.take();
                self.data.pop();
                Some(SseEvent {
                    event: if event.is_empty() {
                        "message".into()
                    } else {
                        event
                    },
                    data: std::mem::take(&mut self.data),
                    id: self.id.clone(),
                    retry,
                })
            }
        } else if line.starts_with(':') {
            None
        } else {
            let (name, value) = line.split_once(':').unwrap_or((line, ""));
            let value = value.strip_prefix(' ').unwrap_or(value);
            match name {
                "data" => {
                    self.data.push_str(value);
                    self.data.push('\n');
                }
                "event" => self.event = value.into(),
                "id" if !value.contains('\0') => self.id = Some(value.into()),
                "retry" if !value.is_empty() && value.bytes().all(|b| b.is_ascii_digit()) => {
                    if let Ok(ms) = value.parse::<u64>() {
                        self.retry = Some(Duration::from_millis(ms));
                        self.event_retry = self.retry;
                    }
                }
                _ => {}
            }
            None
        };
        self.line.clear();
        Ok(result)
    }
}