Skip to main content

nautilus_core/
message.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Common message types.
17//!
18//! The [`Params`] type uses `IndexMap<String, Value>` for consistent ordering
19//! and JSON value support.
20
21// Re-export Params from the centralized params module
22pub use crate::params::Params;
23use crate::{UUID4, UnixNanos};
24
25/// Represents different types of messages in the system.
26#[derive(Debug, Clone)]
27pub enum Message {
28    /// A command message with an identifier and initialization timestamp.
29    Command {
30        /// The unique identifier for this command.
31        id: UUID4,
32        /// The initialization timestamp.
33        ts_init: UnixNanos,
34    },
35    /// A document message with an identifier and initialization timestamp.
36    Document {
37        /// The unique identifier for this document.
38        id: UUID4,
39        /// The initialization timestamp.
40        ts_init: UnixNanos,
41    },
42    /// An event message with identifiers and timestamps.
43    Event {
44        /// The unique identifier for this event.
45        id: UUID4,
46        /// The initialization timestamp.
47        ts_init: UnixNanos,
48        /// The event timestamp.
49        ts_event: UnixNanos,
50    },
51    /// A request message with an identifier and initialization timestamp.
52    Request {
53        /// The unique identifier for this request.
54        id: UUID4,
55        /// The initialization timestamp.
56        ts_init: UnixNanos,
57    },
58    /// A response message with identifiers, timestamps, and correlation.
59    Response {
60        /// The unique identifier for this response.
61        id: UUID4,
62        /// The initialization timestamp.
63        ts_init: UnixNanos,
64        /// The correlation identifier linking this response to a request.
65        correlation_id: UUID4,
66    },
67}
68
69#[cfg(test)]
70mod tests {
71    use rstest::rstest;
72
73    use super::*;
74
75    #[rstest]
76    fn test_message_command_construction() {
77        let msg = Message::Command {
78            id: UUID4::new(),
79            ts_init: UnixNanos::from(1_000),
80        };
81        assert!(matches!(msg, Message::Command { .. }));
82    }
83
84    #[rstest]
85    fn test_message_document_construction() {
86        let msg = Message::Document {
87            id: UUID4::new(),
88            ts_init: UnixNanos::from(2_000),
89        };
90        assert!(matches!(msg, Message::Document { .. }));
91    }
92
93    #[rstest]
94    fn test_message_event_construction() {
95        let msg = Message::Event {
96            id: UUID4::new(),
97            ts_init: UnixNanos::from(3_000),
98            ts_event: UnixNanos::from(2_500),
99        };
100        assert!(matches!(msg, Message::Event { .. }));
101    }
102
103    #[rstest]
104    fn test_message_request_construction() {
105        let msg = Message::Request {
106            id: UUID4::new(),
107            ts_init: UnixNanos::from(4_000),
108        };
109        assert!(matches!(msg, Message::Request { .. }));
110    }
111
112    #[rstest]
113    fn test_message_response_construction() {
114        let id = UUID4::new();
115        let correlation_id = UUID4::new();
116        let msg = Message::Response {
117            id,
118            ts_init: UnixNanos::from(5_000),
119            correlation_id,
120        };
121        assert!(matches!(msg, Message::Response { .. }));
122    }
123
124    #[rstest]
125    #[expect(clippy::redundant_clone, reason = "Clone is the behavior under test")]
126    fn test_message_clone() {
127        let msg = Message::Command {
128            id: UUID4::new(),
129            ts_init: UnixNanos::from(1_000),
130        };
131        let cloned = msg.clone();
132        assert!(matches!(cloned, Message::Command { .. }));
133    }
134}