#ifndef SERIALIZE_NESTED_HPP_
#define SERIALIZE_NESTED_HPP_

#include <Serialize/Base.hpp>
#include <cstring>

/**
 * @brief A special struct to represent "nested payloads" in serialization/deserialization.
 * 
 * When serialized, the `buffer` is simply copied into the serialized block at the location
 * where the `NestedPayload` was placed.
 * 
 * When deserialized, the `buffer` is filled with the remained of the serialized data.
 */
struct NestedPayload
{
    std::vector<uint8_t> buffer;
};

template<>
struct SerialCodec<NestedPayload>
{
    static void encode(std::vector<uint8_t> &outBytes, const NestedPayload &nested)
    {
        outBytes.insert(outBytes.end(), nested.buffer.begin(), nested.buffer.end());
    }

    static size_t decode(const uint8_t *data, size_t size, NestedPayload &nested)
    {
        nested.buffer.clear();
        nested.buffer.insert(nested.buffer.end(), data, data + size);
        return size;
    }
};

#endif