from abc import ABC, abstractmethod
from typing import Any, Callable, Union


class DataSerializer(ABC):
    """Serialization contract for converting the domain result to a sendable dict."""

    @abstractmethod
    def serialize(self, result: Any) -> dict:
        """Converts the call_business result into a serializable dict.

        Args:
            result: Object returned by the processor's call_business.
        """

    @abstractmethod
    def serialize_error(self, input_obj: Any, exception: Exception, original_payload: dict) -> dict | str:
        """Converts a business exception into a serializable response.

        Args:
            input_obj: The parsed request object the business step received.
            exception: The exception raised during the business step.
            original_payload: The original decoded payload.
        """

    @abstractmethod
    def serialize_parser_error(self, data: bytes, exception: Exception) -> dict | str:
        """Converts a parse exception into a serializable response.

        Args:
            data: Raw bytes that failed to parse.
            exception: The exception raised during parsing.
        """


SerializerLike = Union["DataSerializer", Callable[[Any], dict]]
