gear_core/message/signal.rs
1// This file is part of Gear.
2
3// Copyright (C) 2022-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::{
20 ids::{ActorId, MessageId, prelude::*},
21 message::{Dispatch, DispatchKind, Message, SignalDetails},
22};
23use gear_core_errors::SignalCode;
24
25/// Message for signal entry point.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct SignalMessage {
28 /// Message id.
29 id: MessageId,
30 /// Reply status code.
31 code: SignalCode,
32}
33
34impl SignalMessage {
35 /// Creates a new [`SignalMessage`].
36 pub fn new(origin_msg_id: MessageId, code: SignalCode) -> Self {
37 let id = MessageId::generate_signal(origin_msg_id);
38
39 Self { id, code }
40 }
41
42 /// Convert [`SignalMessage`] into [`Message`].
43 pub fn into_message(self, origin_msg_id: MessageId, destination: ActorId) -> Message {
44 Message::new(
45 self.id,
46 ActorId::SYSTEM,
47 destination,
48 Default::default(),
49 None,
50 0,
51 Some(SignalDetails::new(origin_msg_id, self.code).into()),
52 )
53 }
54
55 /// Convert [`SignalMessage`] into [`Dispatch`].
56 pub fn into_dispatch(self, origin_msg_id: MessageId, destination: ActorId) -> Dispatch {
57 Dispatch::new(
58 DispatchKind::Signal,
59 self.into_message(origin_msg_id, destination),
60 )
61 }
62
63 /// Message id.
64 pub fn id(&self) -> MessageId {
65 self.id
66 }
67
68 /// Status code of the reply message.
69 pub fn code(&self) -> SignalCode {
70 self.code
71 }
72}