etherparse/transport/
tcp_option_write_error.rs

1/// Errors that can occour when setting the options of a tcp header.
2#[derive(Clone, Debug, Eq, PartialEq)]
3pub enum TcpOptionWriteError {
4    /// There is not enough memory to store all options in the options section of the header (maximum 40 bytes).
5    ///
6    /// The options size is limited by the 4 bit data_offset field in the header which describes
7    /// the total tcp header size in multiple of 4 bytes. This leads to a maximum size for the options
8    /// part of the header of 4*(15 - 5) (minus 5 for the size of the tcp header itself).
9    NotEnoughSpace(usize),
10}
11
12#[cfg(feature = "std")]
13#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
14impl std::error::Error for TcpOptionWriteError {
15    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
16        None
17    }
18}
19
20impl core::fmt::Display for TcpOptionWriteError {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        use TcpOptionWriteError::*;
23        match self {
24            NotEnoughSpace(size) => {
25                write!(f, "TcpOptionWriteError: Not enough memory to store all options in the options section of a tcp header (maximum 40 bytes can be stored, the options would have needed {} bytes).", size)
26            }
27        }
28    }
29}
30
31#[cfg(test)]
32mod test {
33    use crate::*;
34    use alloc::format;
35    use proptest::prelude::*;
36
37    #[test]
38    fn debug() {
39        use TcpOptionWriteError::*;
40        assert_eq!("NotEnoughSpace(0)", format!("{:?}", NotEnoughSpace(0)));
41    }
42
43    #[test]
44    fn clone_eq() {
45        use TcpOptionWriteError::*;
46        let value = NotEnoughSpace(123);
47        assert_eq!(value, value.clone());
48    }
49
50    #[cfg(feature = "std")]
51    proptest! {
52        #[test]
53        fn source(arg_usize in any::<usize>()) {
54            use std::error::Error;
55            use crate::TcpOptionWriteError::*;
56
57            assert!(NotEnoughSpace(arg_usize).source().is_none());
58        }
59    }
60
61    proptest! {
62        #[test]
63        fn fmt(arg_usize in any::<usize>()) {
64            use crate::TcpOptionWriteError::*;
65
66            assert_eq!(
67                &format!("TcpOptionWriteError: Not enough memory to store all options in the options section of a tcp header (maximum 40 bytes can be stored, the options would have needed {} bytes).", arg_usize),
68                &format!("{}", NotEnoughSpace(arg_usize))
69            );
70        }
71    }
72}