use futures_core::Stream; use serde::{Deserialize, Serialize}; use crate::{ codec::MessageCodec, metadata::Metadata, status::{Code, Status}, }; pub trait Message { fn codec() -> MessageCodec; } pub trait MessageStream: Stream> {} pub enum MessageStreamItem { Message(MessageStreamMessage), EndStream(EndStream), } impl MessageStreamItem { pub fn message(message: T, encoded: bool) -> Self { Self::Message(MessageStreamMessage { encoded, message }) } pub fn complete(metadata: Metadata) -> Self { Self::EndStream(EndStream { error: None, metadata, }) } pub fn error(code: Code, message: impl Into, metadata: Metadata) -> Self { Self::EndStream(EndStream { error: Some(Status { code, message: message.into(), details: Default::default(), }), metadata, }) } } pub struct MessageStreamMessage { encoded: bool, message: T, } #[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct EndStream { /// If set, the stream has ended with the given error [`Status`]`. #[serde(default, skip_serializing_if = "Option::is_none")] pub error: Option, #[serde(default, skip_serializing_if = "Metadata::is_empty")] pub metadata: Metadata, }