roopes_core/aggregates/observing_command/
mod.rs

1//!
2#![cfg_attr(feature = "doc-images",
3  cfg_attr(
4    all(),
5    doc = ::embed_doc_image::embed_image!(
6        "observing-command-diagram",
7        "src/aggregates/executable_command/executable_command.svg"
8)))]
9#![cfg_attr(
10    not(feature = "doc-images"),
11    doc = "**Doc images not enabled**. Compile with feature `doc-images` and \
12           Rust version >= 1.54 to enable."
13)]
14//!  The [`observing_command`] module creates
15//! [`Observer`]s from arbitrary [`Command`]s.
16//!
17//! ![observing command diagram][observing-command-diagram]
18
19use crate::prelude::*;
20use std::hash::Hash;
21
22#[cfg(test)]
23mod tests;
24
25/// Exposes the [`ObservingCommand`] type at the
26/// library level.
27pub mod prelude
28{
29    pub use super::ObservingCommand;
30}
31
32/// Provides the [`Observer`] and [`Command`]
33/// traits for a given command.
34pub struct ObservingCommand<C>
35where
36    C: Command,
37{
38    command: C,
39}
40
41impl<C> ObservingCommand<C>
42where
43    C: Command,
44{
45    /// Creates an [`ObservingCommand`] for a
46    /// [`Command`].
47    pub fn new(command: C) -> ObservingCommand<C>
48    {
49        ObservingCommand { command }
50    }
51}
52
53impl<C> From<C> for ObservingCommand<C>
54where
55    C: Command,
56{
57    fn from(command: C) -> Self
58    {
59        ObservingCommand::new(command)
60    }
61}
62
63impl<C> Observer for ObservingCommand<C>
64where
65    C: Command,
66{
67    fn notify(&self)
68    {
69        self.command.execute();
70    }
71}
72
73impl<C> Command for ObservingCommand<C>
74where
75    C: Command,
76{
77    fn execute(&self)
78    {
79        self.command.execute();
80    }
81}
82
83impl<C> PartialEq for ObservingCommand<C>
84where
85    C: PartialEq + Command,
86{
87    fn eq(
88        &self,
89        other: &Self,
90    ) -> bool
91    {
92        self.command.eq(&other.command)
93    }
94}
95
96impl<C> Eq for ObservingCommand<C> where C: Eq + Command {}
97
98impl<C> Hash for ObservingCommand<C>
99where
100    C: Command + Hash,
101{
102    fn hash<H: std::hash::Hasher>(
103        &self,
104        state: &mut H,
105    )
106    {
107        self.command.hash(state);
108    }
109}