package dev.inmo.postssystem.features.common.common

import dev.inmo.micro_utils.common.ByteArrayAllocatorSerializer
import io.ktor.utils.io.core.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

typealias SimpleInputProvider = () -> Input

object SimpleInputProviderSerializer : KSerializer<SimpleInputProvider> {
    override val descriptor: SerialDescriptor
        get() = ByteArrayAllocatorSerializer.descriptor
    override fun deserialize(decoder: Decoder): SimpleInputProvider {
        val allocator = ByteArrayAllocatorSerializer.deserialize(decoder)
        return {
            ByteReadPacket(allocator.invoke())
        }
    }

    override fun serialize(encoder: Encoder, value: SimpleInputProvider) {
        ByteArrayAllocatorSerializer.serialize(
            encoder
        ) {
            value().readBytes()
        }
    }

}
