// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use thiserror::Error;

use crate::MatcherError;

/// Error type for Bincode operations
///
/// This enum represents the various errors that can occur during Bincode serialization
/// and deserialization operations. It wraps errors from various sources to provide
/// a unified error handling approach for binary data operations.
#[derive(Debug, Error)]
pub enum BincodeError {
    /// Error during Bincode serialization
    ///
    /// This occurs when the data structure cannot be serialized to Bincode format,
    /// typically due to unsupported types or data that exceeds format limits.
    #[error("Bincode serialization error: {0}")]
    SerializationError(#[source] bincode::Error),

    /// Error during Bincode deserialization
    ///
    /// This occurs when the binary data cannot be deserialized, typically due to
    /// corrupted data, version mismatches, or attempting to deserialize into an
    /// incompatible type.
    #[error("Bincode deserialization error: {0}")]
    DeserializationError(#[source] bincode::Error),

    /// I/O error during read/write operations
    ///
    /// This occurs when there are issues reading from or writing to the underlying
    /// storage medium (files, network streams, etc.).
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    /// Error related to invalid event structure
    ///
    /// This occurs when the event data is structurally invalid or doesn't conform
    /// to the expected event format, even if the binary data is valid Bincode.
    #[error("Invalid event: {0}")]
    InvalidEvent(String),
}

impl From<MatcherError> for BincodeError {
    fn from(error: MatcherError) -> Self {
        BincodeError::InvalidEvent(error.to_string())
    }
}
