internet 0.0.3

Network library for rust
Documentation
use crate::Codec;
use crate::Cursor;
use crate::tls::ProtocolVersion;
use crate::{Buf, BufMut, BufResult};

use crate::tls::TlsVec;

/// A ContentType.
///
/// Defined in [IETF RFC 9846, B.1].
///
/// [IETF RFC 9846]: https://datatracker.ietf.org/doc/html/rfc9846#appendix-B.1
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContentType(pub u8);

impl ContentType {
    ///
    pub const INVALID: ContentType = ContentType(0);
    ///
    pub const CHANGE_CIPHER_SPEC: ContentType = ContentType(20);
    ///
    pub const ALERT: ContentType = ContentType(21);
    ///
    pub const HANDSHAKE: ContentType = ContentType(22);
    ///
    pub const APPLICATION_DATA: ContentType = ContentType(23);
}

impl Codec for ContentType {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        writer.write_u8(self.0)
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        Ok(ContentType(reader.read_u8()?))
    }
}

/// A TLSPlaintext.
///
/// Defined in [IETF RFC 9846, B.1].
///
/// [IETF RFC 9846]: https://datatracker.ietf.org/doc/html/rfc9846#appendix-B.1
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TlsPlaintext {
    ///
    pub type_: ContentType,
    ///
    pub legacy_record_version: ProtocolVersion,
    ///
    pub fragment: TlsVec<u8, u16>,
}

impl Codec for TlsPlaintext {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        self.type_.encode(writer, ())?;
        self.legacy_record_version.encode(writer, ())?;
        self.fragment.encode(writer, ())?;
        Ok(())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        Ok(TlsPlaintext {
            type_: ContentType::decode(reader, ())?,
            legacy_record_version: ProtocolVersion::decode(reader, ())?,
            fragment: TlsVec::<u8, u16>::decode(reader, ())?,
        })
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {}
}