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
//! Adds a simple `Bundle` trait that makes it easier to add
//! a group of systems to a `DispatcherBuilder`.
#![deny(missing_docs)]
use world_dispatcher::*;

/// A trait allowing the creation of bundles.
/// Bundles are groups of `System`s that are added together and
/// in order in a `DispatcherBuilder`.
/// # Example
/// ```rust
/// use world_dispatcher::*;
/// use planck_ecs_bundle::*;
/// struct TestBundle;
/// impl Bundle for TestBundle {
///     fn systems() -> Vec<System> {
///         vec![
///             (|| {Ok(())}).system(),
///             (|| {Ok(())}).system(),
///             (|| {println!("hello!"); Ok(())}).system(),
///         ]
///     }
/// }
/// let mut builder = DispatcherBuilder::default();
/// #[allow(unused_assignments)]
/// {
///     builder = TestBundle::insert(builder);
/// }
/// ```
pub trait Bundle {
    /// Returns all the systems of this bundle.
    /// This should normally be the only function you need to implement.
    fn systems() -> Vec<System>;
    /// Inserts the systems of this bundle into the provided `DispatcherBuilder`.
    /// A default implementation is already provided for you.
    fn insert(builder: DispatcherBuilder) -> DispatcherBuilder {
        let mut builder = builder;
        for sys in Self::systems() {
            builder = builder.add_system(sys);
        }
        builder
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    #[test]
    fn bundle() {
        struct TestBundle;
        impl Bundle for TestBundle {
            fn systems() -> Vec<System> {
                vec![
                    (|| Ok(())).system(),
                    (|| Ok(())).system(),
                    (|| {
                        println!("hello!");
                        Ok(())
                    })
                    .system(),
                ]
            }
        }
        let mut builder = DispatcherBuilder::default();
        #[allow(unused_assignments)]
        {
            builder = TestBundle::insert(builder);
        }
    }
}