Skip to main content

mcap2arrow_core/
decoder.rs

1//! Decoder trait and encoding key used to register pluggable message decoders.
2
3use crate::{
4    error::DecoderError, message_encoding::MessageEncoding, schema::FieldDefs,
5    schema_encoding::SchemaEncoding, value::Value,
6};
7
8/// Key identifying a (schema_encoding, message_encoding) pair.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct EncodingKey {
11    pub schema_encoding: SchemaEncoding,
12    pub message_encoding: MessageEncoding,
13}
14
15impl EncodingKey {
16    pub fn new(schema_encoding: SchemaEncoding, message_encoding: MessageEncoding) -> Self {
17        Self {
18            schema_encoding,
19            message_encoding,
20        }
21    }
22}
23
24/// Topic-local decoder built from MCAP schema metadata.
25///
26/// Implementations are created by [`MessageDecoder`] once per schema/topic and
27/// reused for all messages in that topic.
28pub trait TopicDecoder: Send + Sync {
29    /// Decode a single message payload into a [`Value`].
30    fn decode(&self, message_data: &[u8]) -> Result<Value, DecoderError>;
31
32    /// Return the Arrow-independent schema for decoded values.
33    fn field_defs(&self) -> &FieldDefs;
34}
35
36/// Factory trait that builds topic-local decoders from MCAP schema metadata.
37///
38/// Implementations are registered with `mcap2arrow::McapReader` and
39/// dispatched based on [`EncodingKey`].
40pub trait MessageDecoder: Send + Sync {
41    /// Returns the encoding pair this decoder handles.
42    fn encoding_key(&self) -> EncodingKey;
43
44    /// Build a topic-local decoder for the given MCAP schema.
45    ///
46    /// Returns `Err` if the schema cannot be parsed or is structurally invalid.
47    fn build_topic_decoder(
48        &self,
49        schema_name: &str,
50        schema_data: &[u8],
51    ) -> Result<Box<dyn TopicDecoder>, DecoderError>;
52}