acton_core/message/
message_address.rs

1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 *   * Apache License, Version 2.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 http://www.apache.org/licenses/LICENSE-2.0
8 *   * MIT license: http://opensource.org/licenses/MIT
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16
17use acton_ern::prelude::*;
18use derive_new::new; // Keep using derive_new for the constructor
19
20use crate::common::AgentSender;
21
22/// Represents the addressable endpoint of an agent, combining its identity and inbox channel.
23///
24/// A `MessageAddress` contains the necessary information to send a message to a specific
25/// agent: its unique identifier (`sender`, an [`Ern`]) and the sender half (`address`)
26/// of the MPSC channel connected to its inbox.
27///
28/// This struct is typically used within message envelopes ([`OutboundEnvelope`]) to specify
29/// the sender and recipient of a message.
30#[derive(new, Clone, Debug)]
31pub struct MessageAddress {
32    /// The sender part of the MPSC channel for the agent's inbox.
33    pub(crate) address: AgentSender,
34    /// The unique identifier (`Ern`) of the agent associated with this address.
35    pub(crate) sender: Ern,
36}
37
38impl MessageAddress {
39    /// Returns the root name component of the agent's identifier (`Ern`).
40    ///
41    /// This provides a simple string representation of the agent's base name.
42    #[inline]
43    pub fn name(&self) -> &str {
44        self.sender.root.as_str()
45    }
46}
47
48impl Default for MessageAddress {
49    /// Creates a default `MessageAddress` with a default `Ern` and a closed channel sender.
50    ///
51    /// This is primarily useful for placeholder initialization before a real address is known.
52    /// Messages cannot be successfully sent using the default address's channel sender.
53    fn default() -> Self {
54        let (outbox, _) = tokio::sync::mpsc::channel(1); // Create a dummy, likely closed sender
55        Self::new(outbox, Ern::default())
56    }
57}
58
59// Unit tests remain unchanged and undocumented
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_return_address() {
66        let return_address = MessageAddress::default();
67        // The default sender might not *always* be closed immediately,
68        // depending on MPSC channel implementation details, but it's not usable.
69        // A better test might involve trying to send and expecting an error,
70        // but that requires more setup. This assertion is okay for basic check.
71        assert!(return_address.address.is_closed());
72    }
73}