1use crate::party_command::PartyCommand;
3
4pub type CommandBatch = Vec<PartyCommand>;
6
7pub fn schedule_commands(commands: Vec<PartyCommand>) -> Vec<CommandBatch> {
13 let mut batches = vec![];
14
15 let mut batch = vec![];
16 for command in commands {
17 if command.is_parallel {
18 batch.push(command);
19 } else {
20 if !batch.is_empty() {
22 batches.push(batch);
23 }
24
25 batches.push(vec![command]);
27
28 batch = vec![];
30 }
31 }
32
33 if !batch.is_empty() {
35 batches.push(batch);
36 }
37
38 batches
39}
40
41#[cfg(test)]
42pub mod test {
43 use crate::party_command::PartyCommand;
44
45 use super::schedule_commands;
46
47 fn parallel_command() -> PartyCommand {
48 PartyCommand::new("".into(), vec![], true)
49 }
50
51 fn sequential_command() -> PartyCommand {
52 PartyCommand::new("".into(), vec![], false)
53 }
54
55 #[test]
56 fn test_scheduler_ending_with_sequential() {
57 let commands = vec![
59 parallel_command(),
60 parallel_command(),
61 sequential_command(),
62 parallel_command(),
63 parallel_command(),
64 parallel_command(),
65 sequential_command(),
66 sequential_command(),
67 ];
68
69 let batches = schedule_commands(commands);
71
72 assert_eq!(batches.len(), 5);
74 assert_eq!(batches[0].len(), 2);
75 assert_eq!(batches[1].len(), 1);
76 assert_eq!(batches[2].len(), 3);
77 assert_eq!(batches[3].len(), 1);
78 assert_eq!(batches[4].len(), 1);
79 }
80
81 #[test]
82 fn test_scheduler_ending_with_parallel() {
83 let commands = vec![
85 parallel_command(),
86 parallel_command(),
87 sequential_command(),
88 parallel_command(),
89 parallel_command(),
90 parallel_command(),
91 ];
92
93 let batches = schedule_commands(commands);
95
96 assert_eq!(batches.len(), 3);
98 assert_eq!(batches[0].len(), 2);
99 assert_eq!(batches[1].len(), 1);
100 assert_eq!(batches[2].len(), 3);
101 }
102}