turn-types 0.7.1

TURN parsing and writing
Documentation
// Copyright (C) 2025 Matthew Waters <matthew@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use stun_types::{attribute::*, message::StunParseError};

/// The [`DontFragment`] [`Attribute`].
///
/// Requests that the TURN server produce an allocation that does not fragment UDP packets.
///
/// Reference: [RFC5766 Section 14.8](https://datatracker.ietf.org/doc/html/rfc5766#section-14.6).
#[derive(Default, Debug, Clone)]
pub struct DontFragment {}
impl AttributeStaticType for DontFragment {
    const TYPE: AttributeType = AttributeType::new(0x001A);
}

impl Attribute for DontFragment {
    fn get_type(&self) -> AttributeType {
        Self::TYPE
    }

    fn length(&self) -> u16 {
        0
    }
}

impl AttributeWrite for DontFragment {
    fn to_raw(&self) -> RawAttribute<'_> {
        RawAttribute::new(self.get_type(), &[])
    }
    fn write_into_unchecked(&self, dest: &mut [u8]) {
        self.write_header_unchecked(dest);
    }
}

impl AttributeFromRaw<'_> for DontFragment {
    fn from_raw_ref(raw: &RawAttribute) -> Result<Self, StunParseError>
    where
        Self: Sized,
    {
        Self::try_from(raw)
    }
}

impl TryFrom<&RawAttribute<'_>> for DontFragment {
    type Error = StunParseError;
    fn try_from(raw: &RawAttribute) -> Result<Self, Self::Error> {
        raw.check_type_and_len(Self::TYPE, 0..=0)?;
        Ok(Self {})
    }
}

impl DontFragment {
    /// Create a new [`DontFragment`] [`Attribute`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use turn_types::attribute::*;
    /// let dont_fragment = DontFragment::new();
    /// ```
    pub fn new() -> Self {
        Self {}
    }
}

impl core::fmt::Display for DontFragment {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.get_type())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{vec, vec::Vec};
    use byteorder::{BigEndian, ByteOrder};
    use tracing::trace;

    #[test]
    fn dont_fragment() {
        let _log = crate::tests::test_init_log();
        let frag = DontFragment::new();
        assert_eq!(frag.get_type(), DontFragment::TYPE);
    }

    #[test]
    fn dont_fragment_raw() {
        let _log = crate::tests::test_init_log();
        let frag = DontFragment::new();
        let raw: RawAttribute = frag.to_raw();
        trace!("{}", raw);
        assert_eq!(raw.get_type(), DontFragment::TYPE);
        let frag2 = DontFragment::try_from(&raw).unwrap();
        assert_eq!(frag2.get_type(), DontFragment::TYPE);
    }

    #[test]
    fn dont_fragment_raw_wrong_type() {
        let _log = crate::tests::test_init_log();
        let frag = DontFragment::new();
        let raw: RawAttribute = frag.to_raw();
        assert_eq!(raw.get_type(), DontFragment::TYPE);
        let frag2 = DontFragment::try_from(&raw).unwrap();
        assert_eq!(frag2.get_type(), DontFragment::TYPE);
        // provide incorrectly typed data
        let mut data: Vec<_> = raw.into();
        BigEndian::write_u16(&mut data[0..2], 0);
        assert!(matches!(
            DontFragment::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
            Err(StunParseError::WrongAttributeImplementation)
        ));
    }

    #[test]
    fn dont_fragment_write_into() {
        let _log = crate::tests::test_init_log();
        let frag = DontFragment::new();
        let raw: RawAttribute = frag.to_raw();
        let mut dest = vec![0; raw.padded_len()];
        frag.write_into(&mut dest).unwrap();
        let raw = RawAttribute::from_bytes(&dest).unwrap();
        let frag2 = DontFragment::try_from(&raw).unwrap();
        assert_eq!(frag2.get_type(), DontFragment::TYPE);
    }

    #[test]
    #[should_panic = "out of range"]
    fn dont_fragment_write_into_unchcked() {
        let _log = crate::tests::test_init_log();
        let frag = DontFragment::new();
        let raw: RawAttribute = frag.to_raw();
        let mut dest = vec![0; raw.padded_len() - 1];
        frag.write_into_unchecked(&mut dest);
    }
}