nardol/message/
metadata_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
use std::net::TcpStream;

use crate::error::Error;
use crate::message::{ContentType, ContextType};

type SendContextOption<'a, M, C> = Option<<C as ContentType<'a, M, C>>::SendContext>;
type ReceiveContextOption<'a, M, C> = Option<<C as ContentType<'a, M, C>>::ReceiveContext>;

/// Trait that needs to be implemented for type that will be used as `metadata` inside a `message`.
pub trait MetaDataType<'a, M, C>
where
    Self: Sized,
    M: MetaDataType<'a, M, C>,
    C: ContentType<'a, M, C>,
{
    /// Context provided to [MetaDataType::send]
    /// from [TcpMessage::send](crate::message::TcpMessage::send).
    type SendContext: ContextType;
    /// Context provided to [MetaDataType::receive].
    type ReceiveContext: ContextType;

    /// Send method used to send `metadata` inside
    /// [TcpMessage::send](crate::message::TcpMessage::send).
    ///
    /// Defines how are `metadata` send.  
    ///
    /// Returns an [Option] of [ContentType::SendContext] on associated [C](ContentType) that is
    /// passed into [ContentType::send] in [TcpMessage::send](crate::message::TcpMessage::send).
    /// or an [Error] if not.
    ///
    /// # Arguments
    /// * `stream` -- [TcpStream] on which are data being sent.
    /// * ` context` -- [Optional](Option) context provided from
    /// [TcpMessage::send](crate::message::TcpMessage::send)
    /// which is [MetaDataType::SendContext]
    fn send(
        self,
        stream: &mut TcpStream,
        context: Option<Self::SendContext>,
    ) -> Result<SendContextOption<'a, M, C>, Error>;

    /// Receive method used to receive `metadata` inside
    /// [TcpMessage::receive](crate::message::TcpMessage::receive).
    ///
    /// Defines how `metadata` are received.
    ///
    /// Returns a [tuple] of `Self` and an [Option] of [ContentType::ReceiveContext] on associated
    /// [C](ContentType) that is passed into [ContentType::receive] in
    /// [TcpMessage::send](crate::message::TcpMessage::send) if successful or an [Error] if not.
    ///
    /// # Arguments
    ///
    /// * `stream` -- [TcpStream] on which are data being received.
    /// * ` context` -- [Optional](Option) context provided from
    /// [TcpMessage::receive](crate::message::TcpMessage::receive) which is
    /// [MetaDataType::ReceiveContext]
    fn receive(
        stream: &mut TcpStream,
        context: &Option<Self::ReceiveContext>,
    ) -> Result<(Self, ReceiveContextOption<'a, M, C>), Error>;
}