re_mcap 0.32.2

Convert MCAP into Rerun-compatible data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
mod attachments;
mod metadata;
mod protobuf;
mod raw;
mod recording_info;
mod ros2;
mod ros2_reflection;
mod schema;
mod stats;

use std::collections::{BTreeMap, BTreeSet};

#[cfg(not(target_arch = "wasm32"))]
use re_chunk::RowId;
use re_chunk::external::nohash_hasher::IntMap;
use re_chunk::{Chunk, EntityPath};
use re_log_types::TimeType;

pub use self::attachments::McapAttachmentsDecoder;
pub use self::metadata::McapMetadataDecoder;
pub use self::protobuf::McapProtobufDecoder;
pub use self::raw::McapRawDecoder;
pub use self::recording_info::McapRecordingInfoDecoder;
pub use self::ros2::McapRos2Decoder;
pub use self::ros2_reflection::McapRos2ReflectionDecoder;
pub use self::schema::McapSchemaDecoder;
pub use self::stats::McapStatisticDecoder;
use crate::Error;
use crate::parsers::{ChannelId, MessageParser, ParserContext};
use crate::util::collect_empty_channels;

// Write MCAP file info & stats to a dedicated static entity.
// This keeps the general RRD `__properties` clean for user-specific property layers.
const MCAP_PROPERTIES_ENTITY_PATH: &str = "__mcap_properties";

/// Globally unique identifier for a decoder.
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
#[repr(transparent)]
pub struct DecoderIdentifier(String);

impl From<&'static str> for DecoderIdentifier {
    fn from(value: &'static str) -> Self {
        Self(value.to_owned())
    }
}

impl From<String> for DecoderIdentifier {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl std::fmt::Display for DecoderIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

/// A decoder describes information that can be extracted from an MCAP file.
///
/// It is the most general level at which we can interpret an MCAP file and can
/// be used to either output general information about the MCAP file or to call
/// into decoders that work on a per-message basis via the [`MessageDecoder`] trait.
pub trait Decoder {
    /// Globally unique identifier for this decoder.
    ///
    /// [`DecoderIdentifier`]s are also be used to select only a subset of active decoders.
    fn identifier() -> DecoderIdentifier
    where
        Self: Sized;

    /// The processing that needs to happen for this decoder.
    ///
    /// This function has access to all file-level processing inputs via [`DecoderContext`].
    // TODO(#10862): Consider abstracting over `Summary` to allow more convenient / performant indexing.
    // For example, we probably don't want to store the entire file in memory.
    fn process(
        &mut self,
        ctx: &DecoderContext<'_>,
        emit: &(dyn Fn(Chunk) + Send + Sync),
    ) -> Result<(), Error>;
}

/// Shared processing context of a decode run.
pub struct DecoderContext<'a> {
    mcap_bytes: &'a [u8],
    summary: &'a ::mcap::Summary,
    topic_filter: &'a TopicFilter,
    empty_channels: BTreeSet<ChannelId>,
}

impl<'a> DecoderContext<'a> {
    pub fn new(
        mcap_bytes: &'a [u8],
        summary: &'a ::mcap::Summary,
        topic_filter: &'a TopicFilter,
        empty_channels: BTreeSet<ChannelId>,
    ) -> Self {
        Self {
            mcap_bytes,
            summary,
            topic_filter,
            empty_channels,
        }
    }

    pub fn summary(&self) -> &'a ::mcap::Summary {
        self.summary
    }

    /// Returns an iterator over all MCAP channels that are relevant for processing
    /// in this context. I.e. all channels after removing empties, applying filters etc.
    pub fn relevant_channels(&self) -> impl Iterator<Item = &std::sync::Arc<mcap::Channel<'a>>> {
        self.summary.channels.values().filter(|channel| {
            !self.empty_channels.contains(&ChannelId(channel.id))
                && self.topic_filter.matches(&channel.topic)
        })
    }

    /// Iterates metadata records referenced by the summary metadata index.
    pub fn metadata_records(
        &self,
    ) -> impl Iterator<
        Item = (
            &'a mcap::records::MetadataIndex,
            Result<mcap::records::Metadata, mcap::McapError>,
        ),
    > + '_ {
        self.summary
            .metadata_indexes
            .iter()
            .map(|index| (index, mcap::read::metadata(self.mcap_bytes, index)))
    }

    /// Iterates attachment records referenced by the summary attachment index.
    pub fn attachment_records(
        &self,
    ) -> impl Iterator<
        Item = (
            &'a mcap::records::AttachmentIndex,
            Result<mcap::Attachment<'a>, mcap::McapError>,
        ),
    > + '_ {
        self.summary
            .attachment_indexes
            .iter()
            .map(|index| (index, mcap::read::attachment(self.mcap_bytes, index)))
    }
}

/// An emitter to use in tests and pass to [`Decoder::process`]
#[cfg(not(target_arch = "wasm32"))]
pub struct TestEmitter {
    rx: std::sync::mpsc::Receiver<Chunk>,
    emitter: Box<dyn Fn(Chunk) + Send + Sync>,
}

#[cfg(not(target_arch = "wasm32"))]
impl Default for TestEmitter {
    fn default() -> Self {
        #[expect(clippy::disallowed_methods)] // This is intended to use in tests
        let (tx, rx) = std::sync::mpsc::channel();
        let emitter = Box::new(move |chunk| {
            let _res = tx.send(chunk);
        });
        Self { rx, emitter }
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl std::ops::Deref for TestEmitter {
    type Target = dyn Fn(Chunk) + Send + Sync;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.emitter
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl TestEmitter {
    pub fn finish(self) -> Vec<Chunk> {
        let Self { rx, emitter } = self;
        drop(emitter);
        rx.iter().collect()
    }
}

/// Can be used to extract per-message information from an MCAP file.
///
/// This is a specialization of [`Decoder`] that allows defining [`MessageParser`]s.
/// to interpret the contents of MCAP chunks.
pub trait MessageDecoder: Send + Sync {
    fn identifier() -> DecoderIdentifier
    where
        Self: Sized;

    fn init(&mut self, _summary: &::mcap::Summary) -> Result<(), Error> {
        Ok(())
    }

    /// Returns `true` if this decoder can handle the given channel.
    ///
    /// This method is used to determine which channels should be processed by which decoders,
    /// particularly for implementing fallback behavior where one decoder handles channels
    /// that other decoders cannot process.
    fn supports_channel(&self, channel: &mcap::Channel<'_>) -> bool;

    /// Instantites a new [`MessageParser`] that expects `num_rows` if it is interested in the current channel.
    ///
    /// Otherwise returns `None`.
    ///
    /// The `num_rows` argument allows parsers to pre-allocate storage with the
    /// correct capacity, avoiding reallocations during message processing.
    fn message_parser(
        &self,
        channel: &mcap::Channel<'_>,
        num_rows: usize,
    ) -> Option<Box<dyn MessageParser>>;
}

type Parser = (ParserContext, Box<dyn MessageParser>);

/// Decodes batches of messages from an MCAP into Rerun chunks using previously registered parsers.
struct McapChunkDecoder {
    parsers: IntMap<ChannelId, Parser>,
    time_type: TimeType,
}

impl McapChunkDecoder {
    pub fn new(parsers: IntMap<ChannelId, Parser>, time_type: TimeType) -> Self {
        Self { parsers, time_type }
    }

    /// Decode the next message in the chunk
    pub fn decode_next(&mut self, msg: &::mcap::Message<'_>) -> Result<(), Error> {
        re_tracing::profile_function!();

        let channel = msg.channel.as_ref();
        let channel_id = ChannelId(channel.id);

        if let Some((ctx, parser)) = self.parsers.get_mut(&channel_id) {
            // If the parser fails, we should _not_ append the timepoint
            parser.append(ctx, msg)?;
            for timepoint in parser.get_log_and_publish_timepoints(msg, self.time_type)? {
                ctx.add_timepoint(timepoint);
            }
        } else {
            // TODO(#10862): If we encounter a message that we can't parse at all we should emit a warning.
            // Note that this quite easy to achieve when using decoders and only selecting a subset.
            // However, to not overwhelm the user this should be reported in a _single_ static chunk,
            // so this is not the right place for this. Maybe we need to introduce something like a "report".
        }
        Ok(())
    }

    /// Finish the decoding process and return the chunks.
    pub fn finish(self) -> impl Iterator<Item = Result<Chunk, Error>> {
        self.parsers
            .into_values()
            .flat_map(|(ctx, parser)| match parser.finalize(ctx) {
                Ok(chunks) => chunks.into_iter().map(Ok).collect::<Vec<_>>(),
                Err(err) => vec![Err(Error::Other(err))],
            })
    }
}

/// Used to select certain decoders.
#[derive(Clone, Debug)]
pub enum SelectedDecoders {
    All,
    Subset(BTreeSet<DecoderIdentifier>),
}

impl SelectedDecoders {
    /// Checks if a decoder is part of the current selection.
    pub fn contains(&self, value: &DecoderIdentifier) -> bool {
        match self {
            Self::All => true,
            Self::Subset(subset) => subset.contains(value),
        }
    }
}

/// Regex-based filter selecting which MCAP topics to decode.
///
/// Patterns use [RE2 syntax](https://github.com/google/re2/wiki/Syntax).
///
/// A topic is kept if:
/// - `include` is empty, **or** any pattern in `include` matches; **and**
/// - no pattern in `exclude` matches.
///
/// Patterns are not implicitly anchored; use `^` / `$` if you need anchoring.
#[derive(Default, Clone, Debug)]
pub struct TopicFilter {
    include: Vec<regex_lite::Regex>,
    exclude: Vec<regex_lite::Regex>,
}

impl TopicFilter {
    pub fn with_include_patterns(mut self, include: &[String]) -> Result<Self, regex_lite::Error> {
        self.include = include
            .iter()
            .map(|pattern| regex_lite::Regex::new(pattern))
            .collect::<Result<Vec<_>, _>>()?;
        Ok(self)
    }

    pub fn with_exclude_patterns(mut self, exclude: &[String]) -> Result<Self, regex_lite::Error> {
        self.exclude = exclude
            .iter()
            .map(|pattern| regex_lite::Regex::new(pattern))
            .collect::<Result<Vec<_>, _>>()?;
        Ok(self)
    }

    /// Returns `true` if the given topic passes the filter.
    pub fn matches(&self, topic: &str) -> bool {
        let included = self.include.is_empty() || self.include.iter().any(|r| r.is_match(topic));
        let excluded = self.exclude.iter().any(|r| r.is_match(topic));
        included && !excluded
    }

    /// Returns `true` if no patterns are configured (i.e. all topics pass).
    pub fn is_empty(&self) -> bool {
        self.include.is_empty() && self.exclude.is_empty()
    }
}

/// Registry fallback strategy.
#[derive(Clone, Debug, Default)]
pub enum Fallback {
    /// No fallback — channels without a handler are simply unassigned.
    #[default]
    None,

    /// Single global fallback message decoder (e.g. `raw`).
    Global(DecoderIdentifier),
}

/// A runner that constrains a [`MessageDecoder`] to a specific set of channels.
pub struct MessageDecoderRunner {
    inner: Box<dyn MessageDecoder>,
    allowed: BTreeSet<ChannelId>,
}

impl MessageDecoderRunner {
    fn new(inner: Box<dyn MessageDecoder>, allowed: BTreeSet<ChannelId>) -> Self {
        Self { inner, allowed }
    }

    fn process(
        &mut self,
        mcap_bytes: &[u8],
        summary: &mcap::Summary,
        time_type: TimeType,
        emit: &(dyn Fn(Chunk) + Send + Sync),
    ) -> Result<(), Error> {
        self.inner.init(summary)?;

        let allowed = &self.allowed;
        let inner = &*self.inner;

        let decode_chunk = |chunk: &::mcap::records::ChunkIndex| -> Result<Vec<Chunk>, Error> {
            let parsers = summary
                .read_message_indexes(mcap_bytes, chunk)?
                .iter()
                .filter_map(|(channel, msg_offsets)| {
                    let channel_id = ChannelId::from(channel.id);
                    if !allowed.contains(&channel_id) {
                        return None;
                    }

                    let parser = inner.message_parser(channel, msg_offsets.len())?;
                    let entity_path = EntityPath::from(channel.topic.as_str());
                    let ctx = ParserContext::new(entity_path, channel.topic.clone(), time_type);
                    Some((channel_id, (ctx, parser)))
                })
                .collect::<IntMap<_, _>>();

            let mut decoder = McapChunkDecoder::new(parsers, time_type);

            for msg in summary.stream_chunk(mcap_bytes, chunk)? {
                match msg {
                    Ok(message) => {
                        if let Err(err) = decoder.decode_next(&message) {
                            re_log::error_once!(
                                "Failed to decode message on channel {}: {err}",
                                message.channel.topic
                            );
                        }
                    }
                    Err(err) => {
                        re_log::error!("Failed to read message from MCAP file: {err}");
                    }
                }
            }

            let mut batch = Vec::new();
            for mut chunk in decoder.finish() {
                if let Ok(chunk) = &mut chunk {
                    chunk.sort_if_unsorted();
                }

                match chunk {
                    Ok(c) => batch.push(c),
                    Err(err) => re_log::error!("Failed to decode chunk: {err}"),
                }
            }

            Ok(batch)
        };

        #[cfg(target_arch = "wasm32")]
        let workers = 1;
        #[cfg(not(target_arch = "wasm32"))]
        let workers = rayon::current_num_threads().max(1);

        if workers <= 2 {
            // Serial path. Used on wasm32 and on small worker counts.
            for chunk in &summary.chunk_indexes {
                for c in decode_chunk(chunk)? {
                    emit(c);
                }
            }
            return Ok(());
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            // Count-bounded channel: cap how many decoded batches can sit in the queue ahead of
            // the consumer. Workers block on `send` once the queue is full, which gives the
            // reordering time to process.
            let max_in_flight = workers * 2;

            let (batch_tx, batch_rx) =
                crossbeam::channel::bounded::<(usize, Vec<Chunk>)>(max_in_flight);

            // Decode chunks in parallel on a producer thread, and re-order them
            // to be deterministic on another thread.
            //
            // Workers pull chunk indices in FIFO order from a shared atomic counter.
            let (producer_result, ()) = rayon::join(
                || {
                    let next_idx = std::sync::atomic::AtomicUsize::new(0);
                    let total = summary.chunk_indexes.len();
                    let chunk_indexes = &summary.chunk_indexes;

                    let result: Result<(), Error> = std::thread::scope(|scope| {
                        let handles: Vec<_> = (0..workers)
                            .map(|_| {
                                let batch_tx = batch_tx.clone();
                                let next_idx = &next_idx;
                                let decode_chunk = &decode_chunk;
                                scope.spawn(move || -> Result<(), Error> {
                                    loop {
                                        let idx = next_idx
                                            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                                        if idx >= total {
                                            return Ok(());
                                        }
                                        let batch = decode_chunk(&chunk_indexes[idx])?;
                                        // Blocks once `max_in_flight` batches are queued.
                                        re_quota_channel::send_crossbeam(&batch_tx, (idx, batch))
                                            .map_err(|err| {
                                            Error::Other(anyhow::format_err!(
                                                "Failed to send batch: {err}"
                                            ))
                                        })?;
                                    }
                                })
                            })
                            .collect();

                        let mut first_err: Result<(), Error> = Ok(());
                        for handle in handles {
                            match handle.join().expect("decoder worker panicked") {
                                Err(err) if first_err.is_ok() => first_err = Err(err),
                                Ok(()) | Err(_) => {}
                            }
                        }
                        first_err
                    });
                    // Close the channel so the consumer's iteration terminates.
                    drop(batch_tx);
                    result
                },
                || {
                    // Re-create `RowId`s on this single thread so they are monotonically
                    // increasing in emission order. Workers generate `RowId`s from
                    // thread-local counters that are not comparable across threads.
                    let emit_with_new_row_ids = |chunk: Chunk| {
                        let chunk = chunk.clone_as(chunk.id(), RowId::new());
                        emit(chunk);
                    };

                    let mut buffer: BTreeMap<usize, Vec<Chunk>> = BTreeMap::new();
                    let mut current_idx = 0usize;
                    for (idx, batch) in &batch_rx {
                        if idx == current_idx {
                            for chunk in batch {
                                emit_with_new_row_ids(chunk);
                            }
                            current_idx += 1;
                            while let Some(b) = buffer.remove(&current_idx) {
                                for chunk in b {
                                    emit_with_new_row_ids(chunk);
                                }
                                current_idx += 1;
                            }
                        } else {
                            buffer.insert(idx, batch);
                        }
                    }

                    re_log::debug_assert!(buffer.is_empty(), "All batches should've been consumed");
                },
            );

            producer_result?;
        }

        Ok(())
    }
}

/// A printable assignment used for dry-runs / UI.
#[derive(Clone, Debug)]
pub struct DecoderAssignment {
    pub channel_id: ChannelId,
    pub topic: String,
    pub encoding: String,
    pub schema_name: Option<String>,
    pub decoder: DecoderIdentifier,
}

/// A concrete execution plan for a given MCAP source.
pub struct ExecutionPlan {
    pub file_decoders: Vec<Box<dyn Decoder>>,
    pub runners: Vec<MessageDecoderRunner>,
    pub assignments: Vec<DecoderAssignment>,
    pub topic_filter: TopicFilter,
}

impl ExecutionPlan {
    pub fn run(
        mut self,
        mcap_bytes: &[u8],
        summary: &mcap::Summary,
        time_type: TimeType,
        emit: &(dyn Fn(Chunk) + Send + Sync),
    ) -> anyhow::Result<()> {
        let empty_channels = collect_empty_channels(mcap_bytes, summary)?;
        let ctx = DecoderContext::new(mcap_bytes, summary, &self.topic_filter, empty_channels);

        for mut decoder in self.file_decoders {
            decoder.process(&ctx, emit)?;
        }

        for runner in &mut self.runners {
            runner.process(mcap_bytes, summary, time_type, emit)?;
        }
        Ok(())
    }
}

/// Holds a set of all known decoders, split into file-scoped and message-scoped.
pub struct DecoderRegistry {
    file_factories: BTreeMap<DecoderIdentifier, fn() -> Box<dyn Decoder>>,
    msg_factories: BTreeMap<DecoderIdentifier, fn() -> Box<dyn MessageDecoder>>,
    msg_order: Vec<DecoderIdentifier>,
    fallback: Fallback,
}

impl DecoderRegistry {
    /// Creates an empty registry.
    pub fn empty() -> Self {
        Self {
            file_factories: Default::default(),
            msg_factories: Default::default(),
            msg_order: Vec::new(),
            fallback: Fallback::None,
        }
    }

    /// Creates a registry with all builtin decoders and raw fallback enabled.
    pub fn all_with_raw_fallback() -> Self {
        Self::all_builtin(true)
    }

    /// Creates a registry with all builtin decoders and raw fallback disabled.
    pub fn all_without_raw_fallback() -> Self {
        Self::all_builtin(false)
    }

    /// Creates a registry with all builtin decoders with configurable raw fallback.
    pub fn all_builtin(raw_fallback_enabled: bool) -> Self {
        let mut registry = Self::empty()
            // file decoders:
            .register_file_decoder::<McapRecordingInfoDecoder>()
            .register_file_decoder::<McapAttachmentsDecoder>()
            .register_file_decoder::<McapMetadataDecoder>()
            .register_file_decoder::<McapSchemaDecoder>()
            .register_file_decoder::<McapStatisticDecoder>()
            // message decoders (priority order):
            .register_message_decoder::<McapRos2Decoder>()
            .register_message_decoder::<McapRos2ReflectionDecoder>()
            .register_message_decoder::<McapProtobufDecoder>();

        if raw_fallback_enabled {
            registry = registry
                .register_message_decoder::<McapRawDecoder>()
                .with_global_fallback::<McapRawDecoder>();
        } else {
            // still register raw so users can explicitly select it, just no fallback
            registry = registry.register_message_decoder::<McapRawDecoder>();
        }

        registry
    }

    /// Register a file-scoped decoder (runs once over the file/summary).
    pub fn register_file_decoder<L: Decoder + Default + 'static>(mut self) -> Self {
        let id = L::identifier();
        if self
            .file_factories
            .insert(id.clone(), || Box::new(L::default()))
            .is_some()
        {
            re_log::warn_once!("Inserted file decoder {} twice.", id);
        }
        self
    }

    /// Register a message-scoped decoder (eligible to handle channels).
    pub fn register_message_decoder<M: MessageDecoder + Default + 'static>(mut self) -> Self {
        let id = <M as MessageDecoder>::identifier();
        if self
            .msg_factories
            .insert(id.clone(), || Box::new(M::default()))
            .is_some()
        {
            re_log::warn_once!("Inserted message decoder {} twice.", id);
        }
        self.msg_order.push(id);
        self
    }

    /// Configure a global fallback message decoder (e.g. `raw`).
    pub fn with_global_fallback<M: MessageDecoder + 'static>(mut self) -> Self {
        self.fallback = Fallback::Global(<M as MessageDecoder>::identifier());
        self
    }

    /// Returns all registered decoder identifiers (file + message) as strings.
    pub fn all_identifiers(&self) -> Vec<String> {
        self.file_factories
            .keys()
            .chain(self.msg_factories.keys())
            .map(|id| id.to_string())
            .collect()
    }

    /// Produce a filtered registry that only contains `selected` decoders.
    pub fn select(&self, selected: &SelectedDecoders) -> Self {
        let file_factories = self
            .file_factories
            .iter()
            .filter(|(id, _)| selected.contains(id))
            .map(|(k, v)| (k.clone(), *v))
            .collect();

        let msg_factories = self
            .msg_factories
            .iter()
            .filter(|(id, _)| selected.contains(id))
            .map(|(k, v)| (k.clone(), *v))
            .collect();

        let msg_order = self
            .msg_order
            .iter()
            .filter(|&id| selected.contains(id))
            .cloned()
            .collect();

        let fallback = self.select_fallback(selected);

        Self {
            file_factories,
            msg_factories,
            msg_order,
            fallback,
        }
    }

    fn select_fallback(&self, selected: &SelectedDecoders) -> Fallback {
        match &self.fallback {
            Fallback::Global(id) if selected.contains(id) => Fallback::Global(id.clone()),
            Fallback::Global(_) | Fallback::None => Fallback::None,
        }
    }

    /// Build a concrete execution plan for a given file.
    pub fn plan(
        &self,
        mcap_bytes: &[u8],
        summary: &mcap::Summary,
        topic_filter: &TopicFilter,
    ) -> anyhow::Result<ExecutionPlan> {
        let file_decoders = self
            .file_factories
            .values()
            .map(|f| f())
            .collect::<Vec<_>>();

        let empty_channels = collect_empty_channels(mcap_bytes, summary)?;

        // instantiate message decoders and init them (supports_channel may depend on init)
        let mut msg_decoders: Vec<(DecoderIdentifier, Box<dyn MessageDecoder>)> = self
            .msg_order
            .iter()
            .filter_map(|id| self.msg_factories.get(id).map(|f| (id.clone(), f())))
            .collect();

        for (_, l) in &mut msg_decoders {
            l.init(summary)?;
        }

        let mut by_decoder: BTreeMap<DecoderIdentifier, BTreeSet<ChannelId>> = BTreeMap::new();
        let mut assignments: Vec<DecoderAssignment> = Vec::new();

        for channel_id in summary.channels.values() {
            let channel_id = ChannelId::from(channel_id.id);
            let channel = summary.channels[&channel_id.0].as_ref();

            if empty_channels.contains(&channel_id) {
                re_log::debug!(
                    "Skipping MCAP channel '{}' (id={}) because it contains no messages.",
                    channel.topic,
                    channel_id.0,
                );
                continue;
            }

            if !topic_filter.matches(&channel.topic) {
                re_log::debug!(
                    "Skipping MCAP channel '{}' because it does not match the topic filter.",
                    channel.topic,
                );
                continue;
            }

            if channel.message_encoding.trim().is_empty() {
                re_log::warn_once!(
                    "MCAP channel '{}' does not specify a message encoding.",
                    channel.topic,
                );
            }

            if channel.schema.is_none() {
                re_log::warn!(
                    "MCAP channel '{}' does not specify a schema.",
                    channel.topic,
                );
            }

            // explicit priority order
            let mut chosen: Option<DecoderIdentifier> = None;
            for (id, decoder) in &msg_decoders {
                if decoder.supports_channel(channel) {
                    chosen = Some(id.clone());
                    break;
                }
            }

            if chosen.is_none() {
                // fallbacks (if any)
                if let Fallback::Global(id) = &self.fallback
                    && self.msg_factories.contains_key(id)
                {
                    chosen = Some(id.clone());
                }
            }

            let schema_name = channel.schema.as_ref().map(|s| s.name.clone());

            let schema_encoding = channel
                .schema
                .as_ref()
                .map(|s| s.encoding.as_str())
                .unwrap_or("Unknown");

            if let Some(id) = chosen {
                by_decoder.entry(id.clone()).or_default().insert(channel_id);

                assignments.push(DecoderAssignment {
                    channel_id,
                    topic: channel.topic.clone(),
                    encoding: schema_encoding.to_owned(),
                    schema_name: channel.schema.as_ref().map(|s| s.name.clone()),
                    decoder: id,
                });
            } else {
                re_log::debug!(
                    "No message decoder selected for topic '{}' (encoding='{}', schema='{:?}')",
                    channel.topic,
                    schema_encoding,
                    schema_name,
                );
            }
        }

        let mut runners = Vec::new();
        for (decoder_id, allowed) in by_decoder {
            if let Some(factory) = self.msg_factories.get(&decoder_id) {
                let inner = factory();
                runners.push(MessageDecoderRunner::new(inner, allowed));
            }
        }

        Ok(ExecutionPlan {
            file_decoders,
            runners,
            assignments,
            topic_filter: topic_filter.clone(),
        })
    }
}

#[cfg(test)]
mod tests {
    use std::io;

    use re_log_types::TimeType;
    use re_sdk_types::archetypes::McapMessage;

    use super::*;

    #[test]
    fn skips_channels_without_messages() {
        let (summary, buffer, empty_channel_id, active_channel_id) = {
            let cursor = io::Cursor::new(Vec::new());
            let mut writer = mcap::Writer::new(cursor).expect("failed to create writer");

            let empty_channel_id = writer
                .add_channel(0, "empty_topic", "raw", &Default::default())
                .expect("failed to add empty channel");
            let active_channel_id = writer
                .add_channel(0, "active_topic", "raw", &Default::default())
                .expect("failed to add active channel");

            writer
                .write_to_known_channel(
                    &mcap::records::MessageHeader {
                        channel_id: active_channel_id,
                        sequence: 0,
                        log_time: 1,
                        publish_time: 1,
                    },
                    &[1, 2, 3],
                )
                .expect("failed to write message");

            let summary = writer.finish().expect("failed to finish writer");
            let buffer = writer.into_inner().into_inner();

            (summary, buffer, empty_channel_id, active_channel_id)
        };

        let plan = DecoderRegistry::empty()
            .register_file_decoder::<McapSchemaDecoder>()
            .register_message_decoder::<McapRawDecoder>()
            .plan(&buffer, &summary, &TopicFilter::default())
            .expect("failed to plan");

        assert_eq!(plan.assignments.len(), 1);
        assert_eq!(plan.assignments[0].channel_id, ChannelId(active_channel_id));
        assert_ne!(plan.assignments[0].channel_id, ChannelId(empty_channel_id));

        let emitter = TestEmitter::default();
        plan.run(&buffer, &summary, TimeType::TimestampNs, &*emitter)
            .expect("failed to run plan");

        let chunks = emitter.finish();

        assert_eq!(chunks.len(), 2);
        assert!(
            chunks
                .iter()
                .all(|chunk| !chunk.entity_path().to_string().ends_with("empty_topic"))
        );
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.entity_path().to_string().ends_with("active_topic"))
        );
    }

    /// Test helper for creating an MCAP summary & blob with a ros2msg-schema channel.
    fn ros2_summary_with_message_encoding(
        schema_name: &str,
        topic: &str,
        message_encoding: &str,
        payload: &[u8],
    ) -> (mcap::Summary, Vec<u8>) {
        let cursor = io::Cursor::new(Vec::new());
        let mut writer = mcap::Writer::new(cursor).expect("failed to create writer");
        let schema_id = writer
            .add_schema(schema_name, "ros2msg", b"string data")
            .expect("failed to add schema");
        let channel_id = writer
            .add_channel(schema_id, topic, message_encoding, &Default::default())
            .expect("failed to add channel");

        writer
            .write_to_known_channel(
                &mcap::records::MessageHeader {
                    channel_id,
                    sequence: 0,
                    log_time: 1,
                    publish_time: 1,
                },
                payload,
            )
            .expect("failed to write message");

        let summary = writer.finish().expect("failed to finish writer");
        let buffer = writer.into_inner().into_inner();
        (summary, buffer)
    }

    /// We expect CDR as encoding for ros2msg-schema messages.
    /// Test that a non-CDR channel that claims to have ros2msg
    /// falls back to raw forwarding instead of message reflection.
    #[test]
    fn non_cdr_ros2msg_channel_is_forwarded_as_raw_blob() {
        let (summary, buffer) = ros2_summary_with_message_encoding(
            "custom_msgs/msg/Foo",
            "non_cdr_topic",
            "json",
            br#"{"data":"hello"}"#,
        );

        let plan = DecoderRegistry::all_with_raw_fallback()
            .plan(&buffer, &summary, &TopicFilter::default())
            .expect("failed to plan");

        let assignment = plan
            .assignments
            .iter()
            .find(|assignment| assignment.topic == "non_cdr_topic")
            .expect("missing assignment");
        assert_eq!(assignment.decoder.to_string(), "raw");

        let test_emitter = TestEmitter::default();
        plan.run(&buffer, &summary, TimeType::TimestampNs, &*test_emitter)
            .expect("failed to run plan");

        let chunks = test_emitter.finish();

        assert!(chunks.iter().any(|chunk| {
            chunk.entity_path().to_string().ends_with("non_cdr_topic")
                && chunk
                    .component_descriptors()
                    .any(|descr| descr.component == McapMessage::descriptor_data().component)
        }));
    }

    /// Tests that an MCAP channel payload without schema is passed through as raw blob (with a warning).
    #[test]
    fn schema_less_channel_is_forwarded_as_raw_blob() {
        let (summary, buffer) = {
            let cursor = io::Cursor::new(Vec::new());
            let mut writer = mcap::Writer::new(cursor).expect("failed to create writer");

            let channel_id = writer
                .add_channel(0, "schema_less_topic", "raw", &Default::default())
                .expect("failed to add channel");

            writer
                .write_to_known_channel(
                    &mcap::records::MessageHeader {
                        channel_id,
                        sequence: 0,
                        log_time: 1,
                        publish_time: 2,
                    },
                    &[1, 2, 3],
                )
                .expect("failed to write message");

            let summary = writer.finish().expect("failed to finish writer");
            let buffer = writer.into_inner().into_inner();
            (summary, buffer)
        };

        let plan = DecoderRegistry::all_with_raw_fallback()
            .plan(&buffer, &summary, &TopicFilter::default())
            .expect("failed to plan");

        let assignment = plan
            .assignments
            .iter()
            .find(|assignment| assignment.topic == "schema_less_topic")
            .expect("missing assignment");
        assert_eq!(assignment.schema_name, None);
        assert_eq!(assignment.decoder.to_string(), "raw");

        let test_emitter = TestEmitter::default();
        plan.run(&buffer, &summary, TimeType::TimestampNs, &*test_emitter)
            .expect("failed to run plan");

        let chunks = test_emitter.finish();
        assert!(chunks.iter().any(|chunk| {
            chunk
                .entity_path()
                .to_string()
                .ends_with("schema_less_topic")
                && chunk.num_rows() == 1
                && chunk
                    .component_descriptors()
                    .any(|descr| descr.component == McapMessage::descriptor_data().component)
                && chunk
                    .timelines()
                    .values()
                    .any(|timeline| timeline.name() == "message_log_time")
                && chunk
                    .timelines()
                    .values()
                    .any(|timeline| timeline.name() == "message_publish_time")
        }));
    }

    /// Tests that semantic ROS 2 parsers also reject non-CDR channels.
    #[test]
    fn semantic_ros2_decoder_does_not_claim_non_cdr_channels() {
        let (summary, buffer) = ros2_summary_with_message_encoding(
            "std_msgs/msg/String",
            "non_cdr_string_topic",
            "json",
            br#"{"data":"hello"}"#,
        );

        let plan = DecoderRegistry::all_with_raw_fallback()
            .plan(&buffer, &summary, &TopicFilter::default())
            .expect("failed to plan");

        let assignment = plan
            .assignments
            .iter()
            .find(|assignment| assignment.topic == "non_cdr_string_topic")
            .expect("missing assignment");
        assert_eq!(assignment.decoder.to_string(), "raw");
    }

    #[test]
    fn topic_filter_matches() {
        // Empty filter accepts everything.
        let filter = TopicFilter::default();
        assert!(filter.is_empty());
        assert!(filter.matches("/anything"));
        assert!(filter.matches("/foo/bar"));

        // Pure include: only matching topics pass.
        let filter = TopicFilter {
            include: vec![regex_lite::Regex::new(r"^/camera/").unwrap()],
            exclude: vec![],
        };
        assert!(!filter.is_empty());
        assert!(filter.matches("/camera/rgb"));
        assert!(filter.matches("/camera/depth"));
        assert!(!filter.matches("/imu"));

        // Pure exclude: empty include means everything passes except excluded.
        let filter = TopicFilter {
            include: vec![],
            exclude: vec![regex_lite::Regex::new(r"^/diagnostics").unwrap()],
        };
        assert!(filter.matches("/camera/rgb"));
        assert!(!filter.matches("/diagnostics/agg"));

        // Combined: exclude takes precedence over include.
        let filter = TopicFilter {
            include: vec![regex_lite::Regex::new(r"^/camera/").unwrap()],
            exclude: vec![regex_lite::Regex::new(r"depth$").unwrap()],
        };
        assert!(filter.matches("/camera/rgb"));
        assert!(!filter.matches("/camera/depth"));
        assert!(!filter.matches("/imu"));

        // Multiple includes: match if ANY matches.
        let filter = TopicFilter {
            include: vec![
                regex_lite::Regex::new(r"^/camera/").unwrap(),
                regex_lite::Regex::new(r"^/imu$").unwrap(),
            ],
            exclude: vec![],
        };
        assert!(filter.matches("/camera/rgb"));
        assert!(filter.matches("/imu"));
        assert!(!filter.matches("/lidar"));
    }

    #[test]
    fn filter_skips_unselected_topics() {
        let (summary, buffer) = {
            let cursor = io::Cursor::new(Vec::new());
            let mut writer = mcap::Writer::new(cursor).expect("failed to create writer");

            let camera_rgb = writer
                .add_channel(0, "/camera/rgb", "raw", &Default::default())
                .expect("failed to add channel");
            let camera_depth = writer
                .add_channel(0, "/camera/depth", "raw", &Default::default())
                .expect("failed to add channel");
            let imu = writer
                .add_channel(0, "/imu", "raw", &Default::default())
                .expect("failed to add channel");

            for channel_id in [camera_rgb, camera_depth, imu] {
                writer
                    .write_to_known_channel(
                        &mcap::records::MessageHeader {
                            channel_id,
                            sequence: 0,
                            log_time: 1,
                            publish_time: 1,
                        },
                        &[1, 2, 3],
                    )
                    .expect("failed to write message");
            }

            let summary = writer.finish().expect("failed to finish writer");
            let buffer = writer.into_inner().into_inner();
            (summary, buffer)
        };

        // Include only /camera/* topics.
        let filter = TopicFilter {
            include: vec![regex_lite::Regex::new(r"^/camera/").unwrap()],
            exclude: vec![],
        };

        let plan = DecoderRegistry::empty()
            .register_message_decoder::<McapRawDecoder>()
            .plan(&buffer, &summary, &filter)
            .expect("failed to plan");

        assert_eq!(plan.assignments.len(), 2);
        let topics: BTreeSet<_> = plan.assignments.iter().map(|a| a.topic.as_str()).collect();
        assert!(topics.contains("/camera/rgb"));
        assert!(topics.contains("/camera/depth"));
        assert!(!topics.contains("/imu"));

        // Exclude /camera/depth.
        let filter = TopicFilter {
            include: vec![],
            exclude: vec![regex_lite::Regex::new(r"depth$").unwrap()],
        };

        let plan = DecoderRegistry::empty()
            .register_message_decoder::<McapRawDecoder>()
            .plan(&buffer, &summary, &filter)
            .expect("failed to plan");

        let topics: BTreeSet<_> = plan.assignments.iter().map(|a| a.topic.as_str()).collect();
        assert_eq!(topics.len(), 2);
        assert!(topics.contains("/camera/rgb"));
        assert!(topics.contains("/imu"));
        assert!(!topics.contains("/camera/depth"));

        // Include + exclude combined.
        let filter = TopicFilter {
            include: vec![regex_lite::Regex::new(r"^/camera/").unwrap()],
            exclude: vec![regex_lite::Regex::new(r"depth$").unwrap()],
        };

        let plan = DecoderRegistry::empty()
            .register_message_decoder::<McapRawDecoder>()
            .plan(&buffer, &summary, &filter)
            .expect("failed to plan");

        assert_eq!(plan.assignments.len(), 1);
        assert_eq!(plan.assignments[0].topic, "/camera/rgb");
    }
}