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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use bytes::BytesMut;

use crate::decoding::Parsable;
use crate::encoding::Writable;
use crate::ProtocolError;

/// An email body part received by the milter client
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Body {
    body: BytesMut,
}

impl From<Body> for Vec<u8> {
    fn from(value: Body) -> Self {
        value.body.to_vec()
    }
}

impl From<&[u8]> for Body {
    fn from(value: &[u8]) -> Self {
        Self {
            body: BytesMut::from_iter(value),
        }
    }
}

impl Body {
    const CODE: u8 = b'B';

    /// Access the contained body bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.body
    }

    /// Access the contained body bytes mutably.
    #[must_use]
    pub fn as_mut_bytes(&mut self) -> &mut [u8] {
        &mut self.body
    }

    /// Convert this body to a `Vec<u8>`
    #[must_use]
    pub fn to_vec(self) -> Vec<u8> {
        self.into()
    }
}

impl Parsable for Body {
    const CODE: u8 = Self::CODE;

    fn parse(buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self { body: buffer })
    }
}

impl Writable for Body {
    fn write(&self, buffer: &mut BytesMut) {
        buffer.extend_from_slice(&self.body);
    }

    fn len(&self) -> usize {
        self.body.len()
    }

    fn code(&self) -> u8 {
        Self::CODE
    }

    fn is_empty(&self) -> bool {
        self.body.is_empty()
    }
}

/// No more body parts will be received after this
#[derive(Clone, PartialEq, Debug, Default)]
pub struct EndOfBody;

impl EndOfBody {
    const CODE: u8 = b'E';
}

impl Parsable for EndOfBody {
    const CODE: u8 = Self::CODE;

    fn parse(_buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

impl Writable for EndOfBody {
    fn write(&self, _buffer: &mut BytesMut) {}

    fn len(&self) -> usize {
        0
    }

    fn code(&self) -> u8 {
        Self::CODE
    }

    fn is_empty(&self) -> bool {
        false
    }
}

#[cfg(all(test, feature = "count-allocations"))]
mod test {
    use super::*;

    #[test]
    fn test_parse_body() {
        let buffer = BytesMut::from("Random body...");
        let info = allocation_counter::measure(|| {
            let res = Body::parse(buffer);
            allocation_counter::opt_out(|| {
                println!("{:?}", res);
                assert!(res.is_ok());
            });
        });
        // Verify that no memory allocations are made:
        assert_eq!(info.count_total, 0);
    }
}