nardol/message/content_type.rs
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
use std::net::TcpStream;
use crate::error::Error;
use crate::message::{ContextType, MetaDataType, OutputType};
use crate::packet::Packet;
type ReceiveOutputOption<'a, M, C> = Option<<C as ContentType<'a, M, C>>::ReceiveOutput>;
// Create some Connection trait to use instead of TcpStream and UdpSocket?
/// Trait that needs to be implemented for type that will be used as `content` inside a `message`.
pub trait ContentType<'a, M, C>
where
Self: Sized,
M: MetaDataType<'a, M, C>,
C: ContentType<'a, M, C>,
{
/// Context provided to [ContentType::send] inside
/// [TcpMessage::send](crate::message::TcpMessage::send), created in [MetaDataType::send].
type SendContext: ContextType;
/// Context provided to [ContentType::receive] inside
/// [TcpMessage::receive](crate::message::TcpMessage::receive),
/// created in [MetaDataType::send].
type ReceiveContext: ContextType;
/// Output of [ContentType::send] that is used as an `output` of
/// [TcpMessage::send](crate::message::TcpMessage::send).
type SendOutput: OutputType;
/// Output of [ContentType::receive] that is used as an `output` of
/// [TcpMessage::receive](crate::message::TcpMessage::receive).
type ReceiveOutput: OutputType;
/// Send method used to send `content` inside
/// [TcpMessage::send](crate::message::TcpMessage::send).
///
/// Defines how `content` is send.
///
/// Returns an [optional](Option) [ContentType::SendOutput] if successful or an [Error] if not.
///
/// # Arguments
///
/// * `stream` -- [TcpStream] on which are data being sent.
/// * `context` -- [Optional](Option) context provided
/// [TcpMessage::send](crate::message::TcpMessage::send) which is [ContentType::SendContext]
/// and created inside [MetaDataType::send].
fn send(
self,
stream: &mut TcpStream,
context: Option<Self::SendContext>,
) -> Result<Option<Self::SendOutput>, Error>;
/// Receive method used to receive `content` inside
/// [TcpMessage::receive](crate::message::TcpMessage::receive).
///
/// Defines how `content` are received.
///
/// Returns a [tuple] if successful or an [Error] if not.
///
/// * `Self` -- Received `content` of `message`.
/// * [`Packet`] -- Received `end data` of a `message` as this `packet` marks end
/// of a `message` it need to be returned from this method. But depending on
/// implementation, this `packet` does not need to be valid.
/// * [Option] of [ContentType::ReceiveOutput] on [C](ContentType) [ContentType::ReceiveOutput]
/// to provide an `output` from both [this](ContentType::receive) method and
/// [TcpMessage::receive](crate::message::TcpMessage::receive).
///
/// # Arguments
///
/// * `stream` -- [TcpStream] on which are data being received.
/// * `metadata` -- a reference for already received `metadata`.
/// * `context` -- [Optional](Option) context provided inside
/// [TcpMessage::receive](crate::message::TcpMessage::receive) which is
/// [ContentType::ReceiveContext], created inside [MetaDataType::receive].
fn receive(
stream: &mut TcpStream,
metadata: &M,
context: Option<Self::ReceiveContext>,
) -> Result<(Self, Packet, ReceiveOutputOption<'a, M, C>), Error>;
}