bevy_event_bus 1.1.3

A Bevy plugin that connects Bevy's event system to external message brokers like Kafka
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
//! Kafka-specific configuration objects for type-safe backend inference

use super::{EventBusConfig, Kafka, ProcessingLimits, TopologyMode};
use crate::{
    BusEvent, EventBusError, backends::event_bus_backend::EventBusBackendConfig,
    decoder::DecoderRegistry,
};
use bevy::prelude::{App, Message};
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::time::Duration;

/// Channel capacity configuration for the Kafka backend's internal queues.
#[derive(Clone, Debug)]
pub struct KafkaChannelCapacities {
    pub message: usize,
    pub commit: usize,
    pub result: usize,
}

impl KafkaChannelCapacities {
    pub fn new(message: usize, commit: usize, result: usize) -> Self {
        Self {
            message,
            commit,
            result,
        }
    }

    pub fn message_capacity(mut self, capacity: usize) -> Self {
        self.message = capacity;
        self
    }

    pub fn commit_capacity(mut self, capacity: usize) -> Self {
        self.commit = capacity;
        self
    }

    pub fn result_capacity(mut self, capacity: usize) -> Self {
        self.result = capacity;
        self
    }
}

impl Default for KafkaChannelCapacities {
    fn default() -> Self {
        Self {
            message: 10_000,
            commit: 2_048,
            result: 1_024,
        }
    }
}

/// Connection configuration for Kafka backend initialization
#[derive(Clone, Debug)]
pub struct KafkaConnectionConfig {
    bootstrap_servers: String,
    client_id: Option<String>,
    timeout_ms: i32,
    additional_config: HashMap<String, String>,
}

impl KafkaConnectionConfig {
    /// Create a new connection configuration with required bootstrap servers.
    pub fn new(bootstrap_servers: impl Into<String>) -> Self {
        Self {
            bootstrap_servers: bootstrap_servers.into(),
            client_id: None,
            timeout_ms: 10_000,
            additional_config: HashMap::new(),
        }
    }

    /// Builder-style setter for client id.
    pub fn set_client_id(mut self, client_id: impl Into<String>) -> Self {
        self.client_id = Some(client_id.into());
        self
    }

    /// Builder-style setter for timeout in milliseconds.
    pub fn set_timeout_ms(mut self, timeout_ms: i32) -> Self {
        self.timeout_ms = timeout_ms;
        self
    }

    /// Builder-style setter for additional key/value configuration pairs.
    pub fn insert_additional_config<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.additional_config.insert(key.into(), value.into());
        self
    }

    pub fn bootstrap_servers(&self) -> &str {
        &self.bootstrap_servers
    }

    pub fn client_id(&self) -> Option<&str> {
        self.client_id.as_deref()
    }

    pub fn timeout_ms(&self) -> i32 {
        self.timeout_ms
    }

    pub fn additional_config(&self) -> &HashMap<String, String> {
        &self.additional_config
    }
}

impl Default for KafkaConnectionConfig {
    fn default() -> Self {
        Self::new("localhost:9092")
    }
}

#[derive(Clone, Debug)]
pub struct KafkaTopologyEventBinding {
    topics: Vec<String>,
    register_fn: fn(&mut App, &[String]),
}

impl KafkaTopologyEventBinding {
    pub fn new<T: BusEvent + Message>(topics: Vec<String>) -> Self {
        Self {
            topics,
            register_fn: register_event_binding::<T>,
        }
    }

    pub fn apply(&self, app: &mut App) {
        (self.register_fn)(app, &self.topics);
    }

    pub fn topics(&self) -> &[String] {
        &self.topics
    }
}

fn register_event_binding<T: BusEvent + Message>(app: &mut App, topics: &[String]) {
    App::add_message::<T>(app);
    App::add_message::<EventBusError<T>>(app);

    if !app.world().contains_resource::<DecoderRegistry>() {
        app.world_mut().insert_resource(DecoderRegistry::new());
    }

    let mut registry = app.world_mut().resource_mut::<DecoderRegistry>();
    for topic in topics {
        registry.register_json_decoder::<T>(topic);
    }

    crate::writers::outbound_bridge::ensure_bridge::<T>(app, topics);
}

/// Controls the topics, consumer groups and behaviour that the backend prepares at startup.
#[derive(Clone, Debug, Default)]
pub struct KafkaTopologyConfig {
    topics: Vec<KafkaTopicSpec>,
    consumer_groups: HashMap<String, KafkaConsumerGroupSpec>,
    event_bindings: Vec<KafkaTopologyEventBinding>,
}

impl KafkaTopologyConfig {
    pub fn new(
        topics: Vec<KafkaTopicSpec>,
        consumer_groups: HashMap<String, KafkaConsumerGroupSpec>,
        event_bindings: Vec<KafkaTopologyEventBinding>,
    ) -> Self {
        Self {
            topics,
            consumer_groups,
            event_bindings,
        }
    }

    pub fn builder() -> KafkaTopologyBuilder {
        KafkaTopologyBuilder::default()
    }

    pub fn topics(&self) -> &[KafkaTopicSpec] {
        &self.topics
    }

    pub fn consumer_groups(&self) -> &HashMap<String, KafkaConsumerGroupSpec> {
        &self.consumer_groups
    }

    pub fn topic_names(&self) -> HashSet<String> {
        self.topics.iter().map(|t| t.name.clone()).collect()
    }

    pub fn event_bindings(&self) -> &[KafkaTopologyEventBinding] {
        &self.event_bindings
    }
}

#[derive(Default)]
pub struct KafkaTopologyBuilder {
    topics: Vec<KafkaTopicSpec>,
    consumer_groups: HashMap<String, KafkaConsumerGroupSpec>,
    event_bindings: Vec<KafkaTopologyEventBinding>,
}

impl KafkaTopologyBuilder {
    pub fn add_topic(&mut self, topic: KafkaTopicSpec) -> &mut Self {
        self.topics.push(topic);
        self
    }

    pub fn add_topics<T: IntoIterator<Item = KafkaTopicSpec>>(&mut self, topics: T) -> &mut Self {
        self.topics.extend(topics);
        self
    }

    pub fn add_consumer_group(
        &mut self,
        id: impl Into<String>,
        spec: KafkaConsumerGroupSpec,
    ) -> &mut Self {
        self.consumer_groups.insert(id.into(), spec);
        self
    }

    pub fn add_event<T: BusEvent + Message>(
        &mut self,
        topics: impl IntoIterator<Item = impl Into<String>>,
    ) -> &mut Self {
        let topics_vec: Vec<String> = topics.into_iter().map(Into::into).collect();
        self.event_bindings
            .push(KafkaTopologyEventBinding::new::<T>(topics_vec));
        self
    }

    pub fn add_event_single<T: BusEvent + Message>(
        &mut self,
        topic: impl Into<String>,
    ) -> &mut Self {
        self.add_event::<T>([topic.into()])
    }

    pub fn build(self) -> KafkaTopologyConfig {
        KafkaTopologyConfig::new(self.topics, self.consumer_groups, self.event_bindings)
    }
}

/// Topic configuration used when provisioning Kafka at startup.
#[derive(Clone, Debug)]
pub struct KafkaTopicSpec {
    pub name: String,
    pub partitions: Option<i32>,
    pub replication: Option<i16>,
    pub mode: TopologyMode,
}

impl KafkaTopicSpec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            partitions: None,
            replication: None,
            mode: TopologyMode::Provision,
        }
    }

    pub fn partitions(mut self, partitions: i32) -> Self {
        self.partitions = Some(partitions);
        self
    }

    pub fn replication(mut self, replication: i16) -> Self {
        self.replication = Some(replication);
        self
    }

    /// Override the topology mode used when preparing this topic.
    pub fn mode(mut self, mode: TopologyMode) -> Self {
        self.mode = mode;
        self
    }
}

/// Enumeration describing how consumers should position when starting.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KafkaInitialOffset {
    Earliest,
    Latest,
    None,
}

#[derive(Clone, Debug)]
pub struct KafkaConsumerGroupSpec {
    pub topics: Vec<String>,
    pub manual_commits: bool,
    pub initial_offset: KafkaInitialOffset,
    pub mode: TopologyMode,
}

impl KafkaConsumerGroupSpec {
    pub fn new<T: IntoIterator<Item = impl Into<String>>>(topics: T) -> Self {
        Self {
            topics: topics.into_iter().map(Into::into).collect(),
            manual_commits: false,
            initial_offset: KafkaInitialOffset::Latest,
            mode: TopologyMode::Provision,
        }
    }

    pub fn manual_commits(mut self, manual: bool) -> Self {
        self.manual_commits = manual;
        self
    }

    pub fn initial_offset(mut self, offset: KafkaInitialOffset) -> Self {
        self.initial_offset = offset;
        self
    }

    /// Override the topology mode used when preparing this consumer group.
    pub fn mode(mut self, mode: TopologyMode) -> Self {
        self.mode = mode;
        self
    }
}

/// Aggregate configuration consumed by the Kafka backend during construction.
#[derive(Clone, Debug)]
pub struct KafkaBackendConfig {
    pub connection: KafkaConnectionConfig,
    pub topology: KafkaTopologyConfig,
    pub consumer_lag_poll_interval: Duration,
    pub channel_capacities: KafkaChannelCapacities,
}

impl KafkaBackendConfig {
    pub fn new(
        connection: KafkaConnectionConfig,
        topology: KafkaTopologyConfig,
        consumer_lag_poll_interval: Duration,
    ) -> Self {
        Self {
            connection,
            topology,
            consumer_lag_poll_interval,
            channel_capacities: KafkaChannelCapacities::default(),
        }
    }

    pub fn channel_capacities(mut self, capacities: KafkaChannelCapacities) -> Self {
        self.channel_capacities = capacities;
        self
    }

    pub fn get_channel_capacities(&self) -> &KafkaChannelCapacities {
        &self.channel_capacities
    }
}

impl EventBusBackendConfig for KafkaBackendConfig {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Configuration for Kafka consumers with production-ready options
#[derive(Clone, Debug)]
pub struct KafkaConsumerConfig {
    consumer_group: String,
    topics: Vec<String>,
    auto_offset_reset: String,
    enable_auto_commit: bool,
    session_timeout: Duration,
    max_poll_records: u32,
    processing_limits: ProcessingLimits,
    additional_config: HashMap<String, String>,
}

impl KafkaConsumerConfig {
    /// Create a new Kafka consumer configuration
    pub fn new<I, T>(consumer_group: impl Into<String>, topics: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        Self {
            consumer_group: consumer_group.into(),
            topics: topics.into_iter().map(Into::into).collect(),
            auto_offset_reset: "latest".to_string(),
            enable_auto_commit: true,
            session_timeout: Duration::from_millis(30_000),
            max_poll_records: 500,
            processing_limits: ProcessingLimits::default(),
            additional_config: HashMap::new(),
        }
    }

    /// Set the consumer group for this configuration
    pub fn consumer_group(mut self, group: impl Into<String>) -> Self {
        self.consumer_group = group.into();
        self
    }

    /// Set the topics this configuration applies to
    pub fn topics<I, T>(mut self, topics: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        self.topics = topics.into_iter().map(Into::into).collect();
        self
    }

    /// Set the auto offset reset behavior ("earliest", "latest", "none")
    pub fn auto_offset_reset(mut self, reset: impl Into<String>) -> Self {
        self.auto_offset_reset = reset.into();
        self
    }

    /// Enable or disable auto-commit (for manual commit control)
    pub fn enable_auto_commit(mut self, enable: bool) -> Self {
        self.enable_auto_commit = enable;
        self
    }

    /// Set the consumer session timeout.
    pub fn session_timeout(mut self, timeout: Duration) -> Self {
        self.session_timeout = timeout;
        self
    }

    /// Set maximum records to poll in a single request
    pub fn max_poll_records(mut self, records: u32) -> Self {
        self.max_poll_records = records;
        self
    }

    /// Set frame-level processing limits
    pub fn processing_limits(mut self, limits: ProcessingLimits) -> Self {
        self.processing_limits = limits;
        self
    }

    /// Add additional Kafka consumer configuration
    pub fn additional_config<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.additional_config.insert(key.into(), value.into());
        self
    }

    /// Get whether auto-commit is enabled
    pub fn is_auto_commit_enabled(&self) -> bool {
        self.enable_auto_commit
    }

    /// Get the consumer session timeout.
    pub fn get_session_timeout(&self) -> Duration {
        self.session_timeout
    }

    /// Get max poll records
    pub fn get_max_poll_records(&self) -> u32 {
        self.max_poll_records
    }

    /// Get processing limits
    pub fn get_processing_limits(&self) -> &ProcessingLimits {
        &self.processing_limits
    }

    /// Get additional config
    pub fn get_additional_config(&self) -> &HashMap<String, String> {
        &self.additional_config
    }

    /// Get auto offset reset setting
    pub fn get_auto_offset_reset(&self) -> &str {
        &self.auto_offset_reset
    }

    /// Get consumer group
    pub fn get_consumer_group(&self) -> &str {
        &self.consumer_group
    }
}

impl EventBusConfig for KafkaConsumerConfig {
    type Backend = Kafka;

    fn topics(&self) -> &[String] {
        &self.topics
    }

    fn config_id(&self) -> String {
        format!("kafka_consumer_{}", self.consumer_group)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Configuration for Kafka producers with production-ready options
#[derive(Clone, Debug)]
pub struct KafkaProducerConfig {
    topics: Vec<String>,
    acks: String,
    retries: u32,
    compression_type: String,
    batch_size: u32,
    linger_ms: u32,
    request_timeout_ms: u32,
    partition_key: Option<String>,
    headers: HashMap<String, String>,
    additional_config: HashMap<String, String>,
}

impl KafkaProducerConfig {
    /// Create a new Kafka producer configuration
    pub fn new<I, T>(topics: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        Self {
            topics: topics.into_iter().map(Into::into).collect(),
            acks: "1".to_string(), // Wait for leader acknowledgment
            retries: 3,
            compression_type: "none".to_string(),
            batch_size: 16384,
            linger_ms: 0,
            request_timeout_ms: 30000,
            partition_key: None,
            headers: HashMap::new(),
            additional_config: HashMap::new(),
        }
    }

    /// Set the topics this producer will write to
    pub fn topics<I, T>(mut self, topics: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        self.topics = topics.into_iter().map(Into::into).collect();
        self
    }

    /// Set acknowledgment requirement ("0", "1", "all")
    pub fn acks(mut self, acks: impl Into<String>) -> Self {
        self.acks = acks.into();
        self
    }

    /// Set number of retries
    pub fn retries(mut self, retries: u32) -> Self {
        self.retries = retries;
        self
    }

    /// Set compression type ("none", "gzip", "snappy", "lz4", "zstd")
    pub fn compression_type(mut self, compression: impl Into<String>) -> Self {
        self.compression_type = compression.into();
        self
    }

    /// Set batch size in bytes
    pub fn batch_size(mut self, size: u32) -> Self {
        self.batch_size = size;
        self
    }

    /// Set linger time in milliseconds
    pub fn linger_ms(mut self, linger: u32) -> Self {
        self.linger_ms = linger;
        self
    }

    /// Set request timeout in milliseconds
    pub fn request_timeout_ms(mut self, timeout: u32) -> Self {
        self.request_timeout_ms = timeout;
        self
    }

    /// Set a partition key that will be applied to every message produced with this config.
    pub fn partition_key(mut self, key: impl Into<String>) -> Self {
        self.partition_key = Some(key.into());
        self
    }

    /// Clear any configured partition key.
    pub fn clear_partition_key(mut self) -> Self {
        self.partition_key = None;
        self
    }

    /// Add or replace a single header that will accompany produced messages.
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Replace all headers with the provided map.
    pub fn headers_map<I, K, V>(mut self, headers: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.headers.clear();
        for (key, value) in headers {
            self.headers.insert(key.into(), value.into());
        }
        self
    }

    /// Remove all configured headers.
    pub fn clear_headers(mut self) -> Self {
        self.headers.clear();
        self
    }

    /// Add additional Kafka producer configuration
    pub fn additional_config<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.additional_config.insert(key.into(), value.into());
        self
    }

    /// Get acknowledgment setting
    pub fn get_acks(&self) -> &str {
        &self.acks
    }

    /// Get retries setting
    pub fn get_retries(&self) -> u32 {
        self.retries
    }

    /// Get compression type
    pub fn get_compression_type(&self) -> &str {
        &self.compression_type
    }

    /// Get batch size
    pub fn get_batch_size(&self) -> u32 {
        self.batch_size
    }

    /// Get linger time
    pub fn get_linger_ms(&self) -> u32 {
        self.linger_ms
    }

    /// Get request timeout
    pub fn get_request_timeout_ms(&self) -> u32 {
        self.request_timeout_ms
    }

    /// Get the configured partition key for produced messages, if any.
    pub fn get_partition_key(&self) -> Option<&str> {
        self.partition_key.as_deref()
    }

    /// Get the configured headers that will be attached to produced messages.
    pub fn get_headers(&self) -> &HashMap<String, String> {
        &self.headers
    }

    /// Get additional config
    pub fn get_additional_config(&self) -> &HashMap<String, String> {
        &self.additional_config
    }
}

impl Default for KafkaProducerConfig {
    fn default() -> Self {
        Self::new(Vec::<String>::new())
    }
}

impl EventBusConfig for KafkaProducerConfig {
    type Backend = Kafka;

    fn topics(&self) -> &[String] {
        &self.topics
    }

    fn config_id(&self) -> String {
        "kafka_producer".to_string()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Kafka-specific event metadata for manual commit functionality
#[derive(Debug, Clone)]
pub struct KafkaMessageMetadata {
    pub topic: String,
    pub partition: i32,
    pub offset: i64,
    pub timestamp: Option<i64>,
    pub key: Option<String>,
    pub headers: HashMap<String, String>,
}

/// Event with manual commit capability (Kafka-only)
pub struct UncommittedEvent<T> {
    event: T,
    metadata: KafkaMessageMetadata,
    commit_fn: Option<Box<dyn FnOnce() -> Result<(), String> + Send + Sync>>,
}

impl<T> std::fmt::Debug for UncommittedEvent<T>
where
    T: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UncommittedEvent")
            .field("event", &self.event)
            .field("metadata", &self.metadata)
            .field("commit_fn", &self.commit_fn.is_some())
            .finish()
    }
}

impl<T> UncommittedEvent<T> {
    /// Create a new uncommitted event
    pub fn new(
        event: T,
        metadata: KafkaMessageMetadata,
        commit_fn: impl FnOnce() -> Result<(), String> + Send + Sync + 'static,
    ) -> Self {
        Self {
            event,
            metadata,
            commit_fn: Some(Box::new(commit_fn)),
        }
    }

    /// Get the event data
    pub fn event(&self) -> &T {
        &self.event
    }

    /// Get the Kafka metadata
    pub fn metadata(&self) -> &KafkaMessageMetadata {
        &self.metadata
    }

    /// Manually commit this event's offset
    pub fn commit(mut self) -> Result<(), String> {
        if let Some(commit_fn) = self.commit_fn.take() {
            commit_fn()
        } else {
            Err("Event already committed".to_string())
        }
    }

    /// Check if this event needs manual commit
    pub fn needs_commit(&self) -> bool {
        self.commit_fn.is_some()
    }
}

impl<T> std::ops::Deref for UncommittedEvent<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.event
    }
}