Skip to main content

oxihuman_core/
command_bus_ddd.rs

1// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
2// SPDX-License-Identifier: Apache-2.0
3#![allow(dead_code)]
4
5pub struct DddCommand {
6    pub name: String,
7    pub payload: String,
8}
9
10pub struct CommandLog {
11    pub commands: Vec<DddCommand>,
12}
13
14pub fn new_ddd_command(name: &str, payload: &str) -> DddCommand {
15    DddCommand {
16        name: name.to_string(),
17        payload: payload.to_string(),
18    }
19}
20
21pub fn new_command_log() -> CommandLog {
22    CommandLog {
23        commands: Vec::new(),
24    }
25}
26
27pub fn log_dispatch(l: &mut CommandLog, cmd: DddCommand) {
28    l.commands.push(cmd);
29}
30
31pub fn log_command_count(l: &CommandLog) -> usize {
32    l.commands.len()
33}
34
35pub fn log_commands_by_name<'a>(l: &'a CommandLog, name: &str) -> Vec<&'a DddCommand> {
36    l.commands.iter().filter(|c| c.name == name).collect()
37}
38
39pub fn log_clear(l: &mut CommandLog) {
40    l.commands.clear();
41}
42
43pub fn log_last_command(l: &CommandLog) -> Option<&DddCommand> {
44    l.commands.last()
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_dispatch_and_count() {
53        /* dispatch adds command and increments count */
54        let mut l = new_command_log();
55        log_dispatch(&mut l, new_ddd_command("Create", "{}"));
56        assert_eq!(log_command_count(&l), 1);
57    }
58
59    #[test]
60    fn test_commands_by_name() {
61        /* filter commands by name */
62        let mut l = new_command_log();
63        log_dispatch(&mut l, new_ddd_command("Create", "a"));
64        log_dispatch(&mut l, new_ddd_command("Delete", "b"));
65        log_dispatch(&mut l, new_ddd_command("Create", "c"));
66        assert_eq!(log_commands_by_name(&l, "Create").len(), 2);
67    }
68
69    #[test]
70    fn test_log_clear() {
71        /* clear removes all commands */
72        let mut l = new_command_log();
73        log_dispatch(&mut l, new_ddd_command("X", ""));
74        log_clear(&mut l);
75        assert_eq!(log_command_count(&l), 0);
76    }
77
78    #[test]
79    fn test_log_last_command() {
80        /* last command returned correctly */
81        let mut l = new_command_log();
82        log_dispatch(&mut l, new_ddd_command("First", ""));
83        log_dispatch(&mut l, new_ddd_command("Last", ""));
84        let last = log_last_command(&l).expect("should succeed");
85        assert_eq!(last.name, "Last");
86    }
87
88    #[test]
89    fn test_empty_log() {
90        /* empty log has no last command */
91        let l = new_command_log();
92        assert!(log_last_command(&l).is_none());
93    }
94
95    #[test]
96    fn test_new_command_fields() {
97        /* command fields set correctly */
98        let cmd = new_ddd_command("Update", "data");
99        assert_eq!(cmd.name, "Update");
100        assert_eq!(cmd.payload, "data");
101    }
102
103    #[test]
104    fn test_commands_by_name_none() {
105        /* no match returns empty */
106        let l = new_command_log();
107        assert!(log_commands_by_name(&l, "X").is_empty());
108    }
109}