acton_core/actor/
agent_config.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
17
18use acton_ern::{Ern, ErnParser};
19
20use crate::common::{BrokerRef, ParentRef};
21use crate::traits::Actor;
22
23/// Configuration for creating an actor.
24///
25/// This struct holds the necessary information to configure an actor,
26/// including its ERN (Entity Resource Name), broker, and parent reference.
27#[derive(Default, Debug, Clone)]
28pub struct AgentConfig {
29    ern: Ern,
30    pub(crate) broker: Option<BrokerRef>,
31    parent: Option<ParentRef>,
32}
33
34impl AgentConfig {
35    /// Creates a new `ActorConfig` instance.
36    ///
37    /// # Arguments
38    ///
39    /// * `ern` - The Entity Resource Name for the actor.
40    /// * `parent` - An optional parent reference.
41    /// * `broker` - An optional broker reference.
42    ///
43    /// # Returns
44    ///
45    /// Returns a `Result` containing the new `ActorConfig` instance or an error.
46    pub fn new(
47        ern: Ern,
48        parent: Option<ParentRef>,
49        broker: Option<BrokerRef>,
50    ) -> anyhow::Result<AgentConfig> {
51        if let Some(parent) = parent {
52            // Get the parent ERN
53            let parent_ern = ErnParser::new(parent.id().to_string()).parse()?;
54            let child_ern = parent_ern + ern;
55            Ok(AgentConfig {
56                ern: child_ern,
57                broker,
58                parent: Some(parent),
59            })
60        } else {
61            Ok(AgentConfig {
62                ern,
63                broker,
64                parent,
65            })
66        }
67    }
68
69    /// Creates a new config with an ERN root with the provided name.
70    pub fn new_with_name(
71        name: impl Into<String>,
72    ) -> anyhow::Result<AgentConfig> {
73        Self::new(Ern::with_root(name.into())?, None, None)
74    }
75
76
77    /// Returns the ERN of the actor.
78    pub(crate) fn ern(&self) -> Ern {
79        self.ern.clone()
80    }
81
82    /// Returns a reference to the optional broker.
83    pub(crate) fn get_broker(&self) -> &Option<BrokerRef> {
84        &self.broker
85    }
86
87    /// Returns a reference to the optional parent.
88    pub(crate) fn parent(&self) -> &Option<ParentRef> {
89        &self.parent
90    }
91}