sim-logic 0.1.0

A code-first application and logic layer for Sim;Engine
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
//! Sequential, per-World System construction and execution.

use std::{error::Error, fmt, marker::PhantomData, sync::Arc};

use bevy_ecs::{
    component::{Component, Mutable},
    query::{QueryData, QueryFilter},
    resource::Resource,
    system::{BoxedSystem, IntoSystem, IsFunctionSystem, Res, ResMut, SystemParamFunction},
    world::World,
};

use crate::{
    collision::{CircleOverlapEntities, RectangleOverlapEntities},
    commands::LogicCommands,
    events::{EventReader, EventRegistry, EventWriter, WorldEvent, event_stage_failure},
    identity::LogicEntityRef,
    input::{Action, FixedInput, FrameInput},
    query::{Query, Single},
    render::FrameViewport,
    resources::{AppRes, AppResMut, ApplicationResourceRegistry},
    time::{FixedTime, FrameTime},
    visual::{ActiveCamera2d, Transform2d},
};

mod supported {
    use crate::{events::EventRegistry, resources::ApplicationResourceRegistry};

    pub trait QueryData {}
    #[allow(private_interfaces)]
    pub trait Param {
        fn register_events(_registry: &mut EventRegistry) {}
        fn register_application_resources(
            _registry: &mut ApplicationResourceRegistry,
            _stage: super::Stage,
            _system: &'static str,
        ) {
        }
        fn writes_events() -> bool {
            false
        }
        fn uses_application_resources() -> bool {
            false
        }
    }
    #[allow(private_interfaces)]
    pub trait Tuple {
        fn register_events(registry: &mut EventRegistry);
        fn register_application_resources(
            registry: &mut ApplicationResourceRegistry,
            stage: super::Stage,
            system: &'static str,
        );
        fn writes_events() -> bool;
        fn uses_application_resources() -> bool;
    }
}

/// Compile-time marker for the System-parameter tuples accepted by the managed
/// first-slice scheduler.
///
/// Sim;Logic implements this sealed trait for generation-checked
/// [`crate::query::Query`], [`crate::query::Single`],
/// [`crate::collision::CircleOverlapEntities`],
/// [`crate::collision::RectangleOverlapEntities`], `Res`, `ResMut`,
/// [`crate::resources::AppRes`], [`crate::resources::AppResMut`], its own
/// read-only frame parameters, and [`LogicCommands`]. Raw Bevy `Query` or
/// `Single`, raw `Entity`, and parameters with
/// initialization-time World access, including Bevy `Local<T: FromWorld>`, are
/// intentionally excluded. Typed event parameters register their channels
/// automatically when a System is added. Application Resource parameters
/// register a required type and are forbidden in Startup.
pub trait SupportedSystemParamTuple: supported::Tuple {}

impl<T: supported::Tuple> SupportedSystemParamTuple for T {}

impl supported::QueryData for () {}
impl<T: Component> supported::QueryData for &T {}
impl<T: Component> supported::QueryData for &mut T {}
impl supported::QueryData for LogicEntityRef {}
impl<D> supported::QueryData for Option<D> where D: QueryData + supported::QueryData {}

macro_rules! impl_supported_query_tuple {
    ($($parameter:ident),+) => {
        impl<$($parameter),+> supported::QueryData for ($($parameter,)+)
        where
            $($parameter: QueryData + supported::QueryData,)+
        {}
    };
}

impl_supported_query_tuple!(P0);
impl_supported_query_tuple!(P0, P1);
impl_supported_query_tuple!(P0, P1, P2);
impl_supported_query_tuple!(P0, P1, P2, P3);
impl_supported_query_tuple!(P0, P1, P2, P3, P4);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12);
impl_supported_query_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13);
impl_supported_query_tuple!(
    P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14
);
impl_supported_query_tuple!(
    P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15
);

impl<D, F> supported::Param for Query<'_, '_, D, F>
where
    D: QueryData + supported::QueryData,
    F: QueryFilter,
{
}
impl<D, F> supported::Param for Single<'_, '_, D, F>
where
    D: bevy_ecs::query::IterQueryData + supported::QueryData,
    F: QueryFilter,
{
}
impl<R: Resource> supported::Param for Res<'_, R> {}
impl<R: Resource<Mutability = Mutable>> supported::Param for ResMut<'_, R> {}
impl<R: Resource> supported::Param for Option<Res<'_, R>> {}
impl<R: Resource<Mutability = Mutable>> supported::Param for Option<ResMut<'_, R>> {}
#[allow(
    private_interfaces,
    reason = "this implementation registers a private resource requirement through a sealed hook"
)]
impl<T: Send + Sync + 'static> supported::Param for AppRes<'_, T> {
    fn register_application_resources(
        registry: &mut ApplicationResourceRegistry,
        stage: Stage,
        system: &'static str,
    ) {
        registry.require::<T>(stage, system);
    }

    fn uses_application_resources() -> bool {
        true
    }
}
#[allow(
    private_interfaces,
    reason = "this implementation registers a private resource requirement through a sealed hook"
)]
impl<T: Send + Sync + 'static> supported::Param for AppResMut<'_, T> {
    fn register_application_resources(
        registry: &mut ApplicationResourceRegistry,
        stage: Stage,
        system: &'static str,
    ) {
        registry.require::<T>(stage, system);
    }

    fn uses_application_resources() -> bool {
        true
    }
}
impl<A: Action> supported::Param for FrameInput<'_, A> {}
impl<A: Action> supported::Param for FixedInput<'_, A> {}
impl<F: QueryFilter> supported::Param for CircleOverlapEntities<'_, '_, F> {}
impl<F: QueryFilter> supported::Param for RectangleOverlapEntities<'_, '_, F> {}
impl supported::Param for FrameTime<'_> {}
impl supported::Param for FixedTime<'_> {}
impl supported::Param for FrameViewport<'_> {}
impl supported::Param for LogicCommands<'_> {}
#[allow(
    private_interfaces,
    reason = "this implementation registers events through a sealed private hook"
)]
impl<E: WorldEvent> supported::Param for EventWriter<'_, E> {
    fn register_events(registry: &mut EventRegistry) {
        registry.register::<E>();
    }

    fn writes_events() -> bool {
        true
    }
}
#[allow(
    private_interfaces,
    reason = "this implementation registers events through a sealed private hook"
)]
impl<E: WorldEvent> supported::Param for EventReader<'_, E> {
    fn register_events(registry: &mut EventRegistry) {
        registry.register::<E>();
    }
}

#[allow(
    private_interfaces,
    reason = "this implementation is hidden with the sealed tuple trait"
)]
impl supported::Tuple for () {
    fn register_events(_registry: &mut EventRegistry) {}

    fn register_application_resources(
        _registry: &mut ApplicationResourceRegistry,
        _stage: Stage,
        _system: &'static str,
    ) {
    }

    fn writes_events() -> bool {
        false
    }

    fn uses_application_resources() -> bool {
        false
    }
}

macro_rules! impl_supported_param_tuple {
    ($($parameter:ident),+) => {
        #[allow(
            private_interfaces,
            reason = "this implementation is hidden with the sealed tuple trait"
        )]
        impl<$($parameter),+> supported::Tuple for ($($parameter,)+)
        where
            $($parameter: supported::Param,)+
        {
            fn register_events(registry: &mut EventRegistry) {
                $(<$parameter as supported::Param>::register_events(registry);)+
            }

            fn register_application_resources(
                registry: &mut ApplicationResourceRegistry,
                stage: Stage,
                system: &'static str,
            ) {
                $(<$parameter as supported::Param>::register_application_resources(
                    registry,
                    stage,
                    system,
                );)+
            }

            fn writes_events() -> bool {
                false $(|| <$parameter as supported::Param>::writes_events())+
            }

            fn uses_application_resources() -> bool {
                false $(|| <$parameter as supported::Param>::uses_application_resources())+
            }
        }
    };
}

impl_supported_param_tuple!(P0);
impl_supported_param_tuple!(P0, P1);
impl_supported_param_tuple!(P0, P1, P2);
impl_supported_param_tuple!(P0, P1, P2, P3);
impl_supported_param_tuple!(P0, P1, P2, P3, P4);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12);
impl_supported_param_tuple!(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13);
impl_supported_param_tuple!(
    P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14
);
impl_supported_param_tuple!(
    P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15
);

pub(crate) fn register_system_events<P: SupportedSystemParamTuple>(registry: &mut EventRegistry) {
    <P as supported::Tuple>::register_events(registry);
}

pub(crate) fn register_system_application_resources<P: SupportedSystemParamTuple>(
    registry: &mut ApplicationResourceRegistry,
    stage: Stage,
    system: &'static str,
) {
    <P as supported::Tuple>::register_application_resources(registry, stage, system);
}

/// A user-facing System stage in the first Sim;Logic execution model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Stage {
    /// Runs once while a World candidate is still isolated.
    Startup,
    /// Runs zero or more times using the configured fixed step.
    FixedUpdate,
    /// Runs once per successful application frame when no fixed-stage
    /// transition is pending.
    FrameUpdate,
}

/// A System cannot participate in Sim;Logic's managed schedule.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SystemSetupError {
    /// The System owns a Bevy deferred buffer such as `bevy_ecs::Commands`.
    Deferred {
        /// Bevy's diagnostic System name.
        system: String,
    },
    /// The System requests exclusive access such as `&mut bevy_ecs::World`.
    Exclusive {
        /// Bevy's diagnostic System name.
        system: String,
    },
    /// A System requests write access to interpolation-owned transforms in
    /// FrameUpdate.
    FrameTransformWrite {
        /// Bevy's diagnostic System name.
        system: String,
    },
    /// A System requests write access to the interpolation-owned camera in
    /// FrameUpdate.
    FrameCameraWrite {
        /// Bevy's diagnostic System name.
        system: String,
    },
    /// Startup requested live Application state while its World was provisional.
    StartupApplicationResourceAccess {
        /// Bevy's diagnostic System name.
        system: String,
    },
}

impl fmt::Display for SystemSetupError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Deferred { system } => write!(
                formatter,
                "system `{system}` uses a deferred Bevy buffer; use Sim;Logic Commands"
            ),
            Self::Exclusive { system } => write!(
                formatter,
                "system `{system}` requests exclusive World access, which bypasses Sim;Logic"
            ),
            Self::FrameTransformWrite { system } => write!(
                formatter,
                "FrameUpdate system `{system}` requests mutable Transform2d access"
            ),
            Self::FrameCameraWrite { system } => write!(
                formatter,
                "FrameUpdate system `{system}` requests mutable ActiveCamera2d access"
            ),
            Self::StartupApplicationResourceAccess { system } => write!(
                formatter,
                "Startup system `{system}` requests an Application Resource; candidate Startup is isolated from live Application state"
            ),
        }
    }
}

impl Error for SystemSetupError {}

/// A typed failure returned while running one managed System.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SystemRunFailure {
    system: String,
    reason: String,
}

impl SystemRunFailure {
    /// Returns Bevy's diagnostic name for the failed System.
    pub fn system(&self) -> &str {
        &self.system
    }

    /// Returns the parameter-validation, execution, or user-returned failure text.
    pub fn reason(&self) -> &str {
        &self.reason
    }
}

impl fmt::Display for SystemRunFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "system `{}` could not run: {}",
            self.system, self.reason
        )
    }
}

impl Error for SystemRunFailure {}

type ManagedSystemOutcome = Result<(), String>;
type ManagedBoxedSystem = BoxedSystem<(), ManagedSystemOutcome>;

fn completed_system((): ()) -> ManagedSystemOutcome {
    Ok(())
}

fn returned_system<E: fmt::Display>(result: Result<(), E>) -> ManagedSystemOutcome {
    result.map_err(|error| format!("returned an error: {error}"))
}

trait RecreateSystem: Send + Sync + 'static {
    fn create(&self) -> ManagedBoxedSystem;
    fn diagnostic_name(&self) -> &'static str;
    fn writes_events(&self) -> bool;
    fn uses_application_resources(&self) -> bool;
}

struct ClonedSystemFactory<S, FunctionMarker> {
    template: S,
    diagnostic_name: &'static str,
    marker: PhantomData<fn() -> FunctionMarker>,
}

impl<S, FunctionMarker> RecreateSystem for ClonedSystemFactory<S, FunctionMarker>
where
    S: SystemParamFunction<FunctionMarker, In = (), Out = ()> + Clone + Send + Sync + 'static,
    S::Param: SupportedSystemParamTuple,
    FunctionMarker: 'static,
{
    fn create(&self) -> ManagedBoxedSystem {
        let system = IntoSystem::<(), (), (IsFunctionSystem, FunctionMarker)>::into_system(
            self.template.clone(),
        );
        Box::new(IntoSystem::into_system(system.map(completed_system)))
    }

    fn diagnostic_name(&self) -> &'static str {
        self.diagnostic_name
    }

    fn writes_events(&self) -> bool {
        <S::Param as supported::Tuple>::writes_events()
    }

    fn uses_application_resources(&self) -> bool {
        <S::Param as supported::Tuple>::uses_application_resources()
    }
}

struct FallibleClonedSystemFactory<S, FunctionMarker, E> {
    template: S,
    diagnostic_name: &'static str,
    marker: PhantomData<fn() -> (FunctionMarker, E)>,
}

impl<S, FunctionMarker, E> RecreateSystem for FallibleClonedSystemFactory<S, FunctionMarker, E>
where
    S: SystemParamFunction<FunctionMarker, In = (), Out = Result<(), E>>
        + Clone
        + Send
        + Sync
        + 'static,
    S::Param: SupportedSystemParamTuple,
    FunctionMarker: 'static,
    E: fmt::Display + 'static,
{
    fn create(&self) -> ManagedBoxedSystem {
        let system =
            IntoSystem::<(), Result<(), E>, (IsFunctionSystem, FunctionMarker)>::into_system(
                self.template.clone(),
            );
        Box::new(IntoSystem::into_system(system.map(returned_system::<E>)))
    }

    fn diagnostic_name(&self) -> &'static str {
        self.diagnostic_name
    }

    fn writes_events(&self) -> bool {
        <S::Param as supported::Tuple>::writes_events()
    }

    fn uses_application_resources(&self) -> bool {
        <S::Param as supported::Tuple>::uses_application_resources()
    }
}

#[derive(Clone, Default)]
pub(crate) struct StageFactories {
    factories: Vec<Arc<dyn RecreateSystem>>,
}

impl StageFactories {
    pub(crate) fn first_application_resource_system(&self) -> Option<String> {
        self.factories
            .iter()
            .find(|factory| factory.uses_application_resources())
            .map(|factory| factory.diagnostic_name().to_owned())
    }

    pub(crate) fn add<FunctionMarker, S>(&mut self, system: S)
    where
        S: SystemParamFunction<FunctionMarker, In = (), Out = ()> + Clone + Send + Sync + 'static,
        S::Param: SupportedSystemParamTuple,
        FunctionMarker: 'static,
    {
        self.factories.push(Arc::new(ClonedSystemFactory {
            template: system,
            diagnostic_name: std::any::type_name::<S>(),
            marker: PhantomData,
        }));
    }

    pub(crate) fn add_fallible<FunctionMarker, S, E>(&mut self, system: S)
    where
        S: SystemParamFunction<FunctionMarker, In = (), Out = Result<(), E>>
            + Clone
            + Send
            + Sync
            + 'static,
        S::Param: SupportedSystemParamTuple,
        FunctionMarker: 'static,
        E: fmt::Display + 'static,
    {
        self.factories.push(Arc::new(FallibleClonedSystemFactory {
            template: system,
            diagnostic_name: std::any::type_name::<S>(),
            marker: PhantomData,
        }));
    }

    pub(crate) fn instantiate(
        &self,
        world: &mut World,
        stage: Stage,
    ) -> Result<SequentialStage, SystemSetupError> {
        let mut systems = Vec::with_capacity(self.factories.len());

        for factory in &self.factories {
            let mut system = factory.create();
            let writes_events = factory.writes_events();
            let name = system.name().to_string();

            if stage == Stage::Startup && factory.uses_application_resources() {
                return Err(SystemSetupError::StartupApplicationResourceAccess { system: name });
            }

            if system.has_deferred() {
                return Err(SystemSetupError::Deferred { system: name });
            }
            if system.is_exclusive() {
                return Err(SystemSetupError::Exclusive { system: name });
            }

            let access = system.initialize(world);
            if stage == Stage::FrameUpdate
                && world
                    .component_id::<Transform2d>()
                    .is_some_and(|component| access.combined_access().has_write(component))
            {
                return Err(SystemSetupError::FrameTransformWrite { system: name });
            }
            if stage == Stage::FrameUpdate
                && world
                    .component_id::<ActiveCamera2d>()
                    .is_some_and(|component| access.combined_access().has_write(component))
            {
                return Err(SystemSetupError::FrameCameraWrite { system: name });
            }

            systems.push(ManagedSystem {
                name,
                system,
                writes_events,
            });
        }

        let writes_events = systems.iter().any(|system| system.writes_events);
        Ok(SequentialStage {
            systems,
            writes_events,
        })
    }
}

pub(crate) struct SequentialStage {
    systems: Vec<ManagedSystem>,
    writes_events: bool,
}

struct ManagedSystem {
    name: String,
    system: ManagedBoxedSystem,
    writes_events: bool,
}

impl SequentialStage {
    pub(crate) fn is_empty(&self) -> bool {
        self.systems.is_empty()
    }

    pub(crate) const fn writes_events(&self) -> bool {
        self.writes_events
    }

    pub(crate) fn run(&mut self, world: &mut World) -> Result<(), SystemRunFailure> {
        for managed in &mut self.systems {
            let outcome = managed
                .system
                .run_without_applying_deferred((), world)
                .map_err(|error| SystemRunFailure {
                    system: managed.name.clone(),
                    reason: error.to_string(),
                })?;
            outcome.map_err(|reason| SystemRunFailure {
                system: managed.name.clone(),
                reason,
            })?;
            if !managed.writes_events {
                continue;
            }
            match event_stage_failure(world) {
                Ok(Some(failure)) => {
                    return Err(SystemRunFailure {
                        system: managed.name.clone(),
                        reason: failure.to_string(),
                    });
                }
                Ok(None) => {}
                Err(()) => {
                    return Err(SystemRunFailure {
                        system: managed.name.clone(),
                        reason: "event failure diagnostics are unavailable".to_owned(),
                    });
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use bevy_ecs::prelude::{ResMut, Resource};

    use super::*;

    #[derive(Resource, Default)]
    struct Trace(Vec<u8>);

    fn first(mut trace: ResMut<Trace>) {
        trace.0.push(1);
    }

    fn second(mut trace: ResMut<Trace>) {
        trace.0.push(2);
    }

    fn fallible_first(mut trace: ResMut<Trace>) -> Result<(), &'static str> {
        trace.0.push(1);
        Ok(())
    }

    fn fallible_stop(mut trace: ResMut<Trace>) -> Result<(), &'static str> {
        trace.0.push(2);
        Err("deliberate failure")
    }

    fn fallible_write_transform(
        mut transforms: Query<&mut Transform2d>,
    ) -> Result<(), &'static str> {
        for _transform in &mut transforms {}
        Ok(())
    }

    fn write_transform(mut transforms: Query<&mut Transform2d>) {
        for _transform in &mut transforms {}
    }

    fn write_single_transform(_transform: Single<&mut Transform2d>) {}

    fn write_camera(mut cameras: Query<&mut ActiveCamera2d>) {
        for _camera in &mut cameras {}
    }

    fn write_single_camera(_camera: Single<&mut ActiveCamera2d>) {}

    fn fallible_write_camera(mut cameras: Query<&mut ActiveCamera2d>) -> Result<(), &'static str> {
        for _camera in &mut cameras {}
        Ok(())
    }

    fn read_camera(cameras: Query<&ActiveCamera2d>) {
        for _camera in &cameras {}
    }

    #[test]
    fn executes_in_registration_order() {
        let mut factories = StageFactories::default();
        factories.add(first);
        factories.add(second);

        let mut world = World::new();
        world.insert_resource(Trace::default());
        EventRegistry::default().install(&mut world, 1);
        let stage = factories.instantiate(&mut world, Stage::FixedUpdate);
        assert!(stage.is_ok());
        let mut stage = match stage {
            Ok(stage) => stage,
            Err(error) => panic!("unexpected setup error: {error}"),
        };

        assert!(stage.run(&mut world).is_ok());
        assert_eq!(world.resource::<Trace>().0, [1, 2]);
    }

    #[test]
    fn sequential_stage_reports_whether_it_contains_any_systems() {
        let empty = StageFactories::default();
        let mut world = World::new();
        let stage = empty
            .instantiate(&mut world, Stage::FixedUpdate)
            .expect("empty stage should instantiate");
        assert!(stage.is_empty());

        let mut populated = StageFactories::default();
        populated.add(first);
        let stage = populated
            .instantiate(&mut world, Stage::FixedUpdate)
            .expect("populated stage should instantiate");
        assert!(!stage.is_empty());
    }

    #[test]
    fn fallible_error_stops_later_systems_and_retains_diagnostics() {
        let mut factories = StageFactories::default();
        factories.add_fallible(fallible_first);
        factories.add_fallible(fallible_stop);
        factories.add(second);

        let mut world = World::new();
        world.insert_resource(Trace::default());
        let stage = factories.instantiate(&mut world, Stage::FixedUpdate);
        assert!(stage.is_ok());
        let mut stage = match stage {
            Ok(stage) => stage,
            Err(error) => panic!("unexpected setup error: {error}"),
        };

        let error = stage
            .run(&mut world)
            .expect_err("second System should fail");

        assert!(
            error.system().ends_with("fallible_stop"),
            "unexpected System name: {}",
            error.system()
        );
        assert_eq!(error.reason(), "returned an error: deliberate failure");
        assert_eq!(world.resource::<Trace>().0, [1, 2]);
    }

    #[test]
    fn frame_system_transform_writes_are_rejected_at_setup() {
        let mut factories = StageFactories::default();
        factories.add(write_transform);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameTransformWrite { .. })
        ));
    }

    #[test]
    fn fallible_adapter_preserves_frame_transform_access() {
        let mut factories = StageFactories::default();
        factories.add_fallible(fallible_write_transform);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameTransformWrite { .. })
        ));
    }

    #[test]
    fn fixed_system_transform_writes_remain_supported() {
        let mut factories = StageFactories::default();
        factories.add(write_transform);
        let mut world = World::new();

        assert!(
            factories
                .instantiate(&mut world, Stage::FixedUpdate)
                .is_ok()
        );
    }

    #[test]
    fn frame_single_transform_writes_are_rejected_at_setup() {
        let mut factories = StageFactories::default();
        factories.add(write_single_transform);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameTransformWrite { .. })
        ));
    }

    #[test]
    fn fixed_single_transform_writes_remain_supported() {
        let mut factories = StageFactories::default();
        factories.add(write_single_transform);
        let mut world = World::new();

        assert!(
            factories
                .instantiate(&mut world, Stage::FixedUpdate)
                .is_ok()
        );
    }

    #[test]
    fn frame_system_camera_writes_are_rejected_at_setup() {
        let mut factories = StageFactories::default();
        factories.add(write_camera);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameCameraWrite { .. })
        ));
    }

    #[test]
    fn fixed_system_camera_writes_remain_supported() {
        let mut factories = StageFactories::default();
        factories.add(write_camera);
        let mut world = World::new();

        assert!(
            factories
                .instantiate(&mut world, Stage::FixedUpdate)
                .is_ok()
        );
    }

    #[test]
    fn frame_single_camera_writes_are_rejected_at_setup() {
        let mut factories = StageFactories::default();
        factories.add(write_single_camera);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameCameraWrite { .. })
        ));
    }

    #[test]
    fn fixed_single_camera_writes_remain_supported() {
        let mut factories = StageFactories::default();
        factories.add(write_single_camera);
        let mut world = World::new();

        assert!(
            factories
                .instantiate(&mut world, Stage::FixedUpdate)
                .is_ok()
        );
    }

    #[test]
    fn fallible_adapter_preserves_frame_camera_access() {
        let mut factories = StageFactories::default();
        factories.add_fallible(fallible_write_camera);
        let mut world = World::new();

        let result = factories.instantiate(&mut world, Stage::FrameUpdate);

        assert!(matches!(
            result,
            Err(SystemSetupError::FrameCameraWrite { .. })
        ));
    }

    #[test]
    fn frame_system_camera_reads_remain_supported() {
        let mut factories = StageFactories::default();
        factories.add(read_camera);
        let mut world = World::new();

        assert!(
            factories
                .instantiate(&mut world, Stage::FrameUpdate)
                .is_ok()
        );
    }
}