nautilus_model/identifiers/
exec_algorithm_id.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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, Formatter},
20    hash::Hash,
21};
22
23use nautilus_core::correctness::{FAILED, check_valid_string};
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 = "posei_trader.core.nautilus_pyo3.model")
32)]
33pub struct ExecAlgorithmId(Ustr);
34
35impl ExecAlgorithmId {
36    /// Creates a new [`ExecAlgorithmId`] instance with correctness checking.
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if `value` is not a valid string.
41    ///
42    /// # Notes
43    ///
44    /// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
45    pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
46        let value = value.as_ref();
47        check_valid_string(value, stringify!(value))?;
48        Ok(Self(Ustr::from(value)))
49    }
50
51    /// Creates a new [`ExecAlgorithmId`] instance.
52    ///
53    /// # Panics
54    ///
55    /// Panics if `value` is not a valid string.
56    pub fn new<T: AsRef<str>>(value: T) -> Self {
57        Self::new_checked(value).expect(FAILED)
58    }
59
60    /// Sets the inner identifier value.
61    #[allow(dead_code)]
62    pub(crate) fn set_inner(&mut self, value: &str) {
63        self.0 = Ustr::from(value);
64    }
65
66    /// Returns the inner identifier value.
67    #[must_use]
68    pub fn inner(&self) -> Ustr {
69        self.0
70    }
71
72    /// Returns the inner identifier value as a string slice.
73    #[must_use]
74    pub fn as_str(&self) -> &str {
75        self.0.as_str()
76    }
77}
78
79impl Debug for ExecAlgorithmId {
80    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
81        write!(f, "{:?}", self.0)
82    }
83}
84
85impl Display for ExecAlgorithmId {
86    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87        write!(f, "{}", self.0)
88    }
89}
90
91////////////////////////////////////////////////////////////////////////////////
92// Tests
93////////////////////////////////////////////////////////////////////////////////
94#[cfg(test)]
95mod tests {
96    use rstest::rstest;
97
98    use super::*;
99    use crate::identifiers::stubs::*;
100
101    #[rstest]
102    fn test_string_reprs(exec_algorithm_id: ExecAlgorithmId) {
103        assert_eq!(exec_algorithm_id.as_str(), "001");
104        assert_eq!(format!("{exec_algorithm_id}"), "001");
105    }
106}