internet 0.0.4

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

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

impl AlertLevel {
    ///
    pub const WARNING: AlertLevel = AlertLevel(1);
    ///
    pub const FATAL: AlertLevel = AlertLevel(2);
}

impl Codec for AlertLevel {
    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(AlertLevel(reader.read_u8()?))
    }
}

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

impl AlertDescription {
    ///
    pub const CLOSE_NOTIFY: AlertDescription = AlertDescription(0);
    ///
    pub const UNEXPECTED_MESSAGE: AlertDescription = AlertDescription(10);
    ///
    pub const BAD_RECORD_MAC: AlertDescription = AlertDescription(20);
    ///
    pub const RECORD_OVERFLOW: AlertDescription = AlertDescription(22);
    ///
    pub const HANDSHAKE_FAILURE: AlertDescription = AlertDescription(40);
    ///
    pub const BAD_CERTIFICATE: AlertDescription = AlertDescription(42);
    ///
    pub const UNSUPPORTED_CERTIFICATE: AlertDescription = AlertDescription(43);
    ///
    pub const CERTIFICATE_REVOKED: AlertDescription = AlertDescription(44);
    ///
    pub const CERTIFICATE_EXPIRED: AlertDescription = AlertDescription(45);
    ///
    pub const CERTIFICATE_UNKNOWN: AlertDescription = AlertDescription(46);
    ///
    pub const ILLEGAL_PARAMETER: AlertDescription = AlertDescription(47);
    ///
    pub const UNKNOWN_CA: AlertDescription = AlertDescription(48);
    ///
    pub const ACCESS_DENIED: AlertDescription = AlertDescription(49);
    ///
    pub const DECODE_ERROR: AlertDescription = AlertDescription(50);
    ///
    pub const DECRYPT_ERROR: AlertDescription = AlertDescription(51);
    ///
    pub const PROTOCOL_VERSION: AlertDescription = AlertDescription(70);
    ///
    pub const INSUFFICIENT_SECURITY: AlertDescription = AlertDescription(71);
    ///
    pub const INTERNAL_ERROR: AlertDescription = AlertDescription(80);
    ///
    pub const INAPPROPRIATE_FALLBACK: AlertDescription = AlertDescription(86);
    ///
    pub const USER_CANCELED: AlertDescription = AlertDescription(90);
    ///
    pub const MISSING_EXTENSION: AlertDescription = AlertDescription(109);
    ///
    pub const UNSUPPORTED_EXTENSION: AlertDescription = AlertDescription(110);
    ///
    pub const UNRECOGNIZED_NAME: AlertDescription = AlertDescription(112);
    ///
    pub const BAD_CERTIFICATE_STATUS_RESPONSE: AlertDescription = AlertDescription(113);
    ///
    pub const UNKNOWN_PSK_IDENTITY: AlertDescription = AlertDescription(115);
    ///
    pub const CERTIFICATE_REQUIRED: AlertDescription = AlertDescription(116);
    ///
    pub const NO_APPLICATION_PROTOCOL: AlertDescription = AlertDescription(120);
}

impl Codec for AlertDescription {
    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(AlertDescription(reader.read_u8()?))
    }
}

/// An Alert.
///
/// Defined in [IETF RFC 9846, B.2].
///
/// [IETF RFC 9846, B.2]: https://datatracker.ietf.org/doc/html/rfc9846#appendix-B.2
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Alert {
    ///
    pub level: AlertLevel,
    ///
    pub description: AlertDescription,
}

impl Codec for Alert {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        self.level.encode(writer, ())?;
        self.description.encode(writer, ())?;
        Ok(())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        Ok(Alert {
            level: AlertLevel::decode(reader, ())?,
            description: AlertDescription::decode(reader, ())?,
        })
    }
}