mod json;
mod protobuf;

use crate::error::KnafehError;

pub use json::JsonCodec;
pub use protobuf::{ProtobufCodec, RawProtobufCodec};

/// Trait for encoding/decoding RPC payloads.
///
/// The codec is responsible for validating and transforming request and
/// response bodies as they cross the RPC boundary.
pub trait Codec: Send + Sync + 'static {
    /// Encode a payload for transmission.
    fn encode(&self, value: &[u8]) -> Result<Vec<u8>, KnafehError>;

    /// Decode a received payload.
    fn decode(&self, data: &[u8]) -> Result<Vec<u8>, KnafehError>;

    /// The content-type header value for this codec.
    fn content_type(&self) -> &str;

    /// The codec name (for logging / debugging).
    fn name(&self) -> &str;
}

/// The default codec for Knafeh — protobuf.
///
/// Use [`JsonCodec`] for human-readable payloads, or implement [`Codec`]
/// for custom serialization.
pub type DefaultCodec = ProtobufCodec;
