Skip to main content

nautilus_model/identifiers/
exec_algorithm_id.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//! Represents a valid execution algorithm ID.
17
18use std::{
19    fmt::{Debug, Display},
20    hash::Hash,
21};
22
23use nautilus_core::correctness::{FAILED, check_valid_string_ascii};
24use ustr::Ustr;
25
26/// Represents a valid execution algorithm ID.
27#[repr(C)]
28#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
29#[cfg_attr(
30    feature = "python",
31    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
32)]
33#[cfg_attr(
34    feature = "python",
35    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
36)]
37pub struct ExecAlgorithmId(Ustr);
38
39impl ExecAlgorithmId {
40    /// Creates a new [`ExecAlgorithmId`] instance with correctness checking.
41    ///
42    /// # Errors
43    ///
44    /// Returns an error if `value` is not a valid string.
45    ///
46    /// # Notes
47    ///
48    /// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
49    pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
50        let value = value.as_ref();
51        check_valid_string_ascii(value, stringify!(value))?;
52        Ok(Self(Ustr::from(value)))
53    }
54
55    /// Creates a new [`ExecAlgorithmId`] instance.
56    ///
57    /// # Panics
58    ///
59    /// Panics if `value` is not a valid string.
60    pub fn new<T: AsRef<str>>(value: T) -> Self {
61        Self::new_checked(value).expect(FAILED)
62    }
63
64    /// Sets the inner identifier value.
65    #[cfg_attr(not(feature = "python"), allow(dead_code))]
66    pub(crate) fn set_inner(&mut self, value: &str) {
67        self.0 = Ustr::from(value);
68    }
69
70    /// Returns the inner identifier value.
71    #[must_use]
72    pub fn inner(&self) -> Ustr {
73        self.0
74    }
75
76    /// Returns the inner identifier value as a string slice.
77    #[must_use]
78    pub fn as_str(&self) -> &str {
79        self.0.as_str()
80    }
81}
82
83impl Debug for ExecAlgorithmId {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(f, "\"{}\"", self.0)
86    }
87}
88
89impl Display for ExecAlgorithmId {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        write!(f, "{}", self.0)
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use rstest::rstest;
98
99    use super::*;
100    use crate::identifiers::stubs::*;
101
102    #[rstest]
103    fn test_string_reprs(exec_algorithm_id: ExecAlgorithmId) {
104        assert_eq!(exec_algorithm_id.as_str(), "001");
105        assert_eq!(format!("{exec_algorithm_id}"), "001");
106    }
107}