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
use bytes::BytesMut;

/// A trait to be implemented by types that encode [`Item`] values into a
/// buffer of type [`BytesMut`](bytes::BytesMut).
pub trait Encoder<Item = Self> {
    /// The type of error that can occur if encoding fails.
    type Error;

    /// Encodes the given input into the output buffer.
    ///
    /// # Arguments
    /// * `item` - The input to encode.
    /// * `buf` - The output buffer to write the encoded input to.
    fn encode(item: &Item, buf: &mut BytesMut) -> Result<(), Self::Error>;

    /// Returns the number of bytes required to encode the given input.
    ///
    /// This method is useful for pre-allocating buffers that will be used for
    /// encoding.
    ///
    /// # Arguments
    /// * `item` - The input to encode.
    /// * `buf` - The output buffer used for encoding.
    ///
    /// # Returns
    /// The number of bytes required to encode the given input.
    fn size_of(item: &Item) -> usize;
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;

    use crate as recode;
    use crate::{codec::LengthPrefixed, util::EncoderExt, Encoder};

    #[test]
    fn basic_test() {
        #[derive(Encoder)]
        #[recode(encoder(buffer_name = "buf", error = "crate::Error"))]
        struct TestType {
            age: u32,
            salary: u64,
            #[recode(encoder(with = "LengthPrefixed::<u8>"))]
            first_name: Bytes,
            #[recode(encoder(with = "LengthPrefixed::<u16>"))]
            last_name: Bytes,
            #[recode(encoder(with = "LengthPrefixed::<u32>"))]
            image: Bytes,
        }

        const IMAGE_BUF: [u8; 32] = [
            0xBA, 0x3E, 0x9D, 0x6B, 0xAE, 0xF5, 0x91, 0xCC, 0xE2, 0xF0, 0xCB,
            0x4F, 0xBB, 0x5B, 0x2B, 0xBE, 0xA7, 0xF4, 0x9D, 0xFB, 0x87, 0x43,
            0x0E, 0xDF, 0x30, 0x6F, 0x7D, 0x6E, 0x22, 0xAB, 0xCC, 0x47,
        ];

        let test_item = TestType {
            age: 0x01234567,
            salary: 0x1122334455667788,
            first_name: "ayman".into(),
            last_name: "القاضي".into(),
            image: IMAGE_BUF.as_ref().into(),
        };

        let mut buf = bytes::BytesMut::new();

        assert_eq!(test_item.age.size(), 4);
        assert_eq!(test_item.salary.size(), 8);
        assert_eq!(LengthPrefixed::<u8>::size_of(&test_item.first_name), 1 + 5);
        assert_eq!(
            LengthPrefixed::<u16>::size_of(&test_item.last_name),
            2 + 12
        );
        assert_eq!(LengthPrefixed::<u32>::size_of(&test_item.image), 4 + 32);
        assert_eq!(test_item.size(), 4 + 8 + (1 + 5) + (2 + 12) + (4 + 32));

        TestType::encode(&test_item, &mut buf).unwrap();

        const BUF: [u8; 68] = [
            // age (4 bytes)
            0x01, 0x23, 0x45, 0x67, // salary (8 bytes)
            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
            // first name (1 bytes prefixed ascii; 6 bytes)
            0x05, b'a', b'y', b'm', b'a', b'n',
            // last name (2 bytes prefixed utf8; 6 bytes)
            0x00, 0x0C, 0xD8, 0xA7, 0xD9, 0x84, 0xD9, 0x82, 0xD8, 0xA7, 0xD8,
            0xB6, 0xD9, 0x8A,
            // image (4 bytes prefixed buffer; 36 bytes)
            0x00, 0x00, 0x00, 0x20, 0xBA, 0x3E, 0x9D, 0x6B, 0xAE, 0xF5, 0x91,
            0xCC, 0xE2, 0xF0, 0xCB, 0x4F, 0xBB, 0x5B, 0x2B, 0xBE, 0xA7, 0xF4,
            0x9D, 0xFB, 0x87, 0x43, 0x0E, 0xDF, 0x30, 0x6F, 0x7D, 0x6E, 0x22,
            0xAB, 0xCC, 0x47,
        ];

        assert_eq!(buf, BUF.as_ref());
    }
}