Source code for jam.utils.codec.codec

"""Codec interface for encoding and decoding data types."""

from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Tuple, Union

# Type variable for generic codec implementations
T = TypeVar("T")


[docs] class Codec(ABC, Generic[T]): """Abstract base class defining the interface for encoding and decoding data."""
[docs] @abstractmethod def encode_size(self, value: T) -> int: """Calculate the number of bytes needed to encode the value.""" pass
[docs] @abstractmethod def encode_into(self, value: T, buffer: bytearray, offset: int = 0) -> int: """Encode the value into the provided buffer at the specified offset.""" pass
[docs] def encode(self, value: T) -> bytes: """Encode the value into a new bytes object.""" size = self.encode_size(value) buffer = bytearray(size) written = self.encode_into(value, buffer) return bytes(buffer[:written])
[docs] @abstractmethod def decode_from( self, buffer: Union[bytes, bytearray, memoryview], offset: int = 0 ) -> Tuple[T, int]: """Decode a value from the provided buffer starting at the specified offset.""" pass