nautilus_model/identifiers/
venue_order_id.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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 venue order ID (assigned by a trading venue).
17
18use std::{
19    fmt::{Debug, Display, Formatter},
20    hash::Hash,
21};
22
23use nautilus_core::correctness::{check_valid_string, FAILED};
24use ustr::Ustr;
25
26/// Represents a valid venue order ID (assigned by a trading venue).
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")
32)]
33pub struct VenueOrderId(Ustr);
34
35impl VenueOrderId {
36    /// Creates a new [`VenueOrderId`] instance with correctness checking.
37    ///
38    /// # Errors
39    ///
40    /// This function returns an error:
41    /// - If `value` is not a valid string.
42    pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
43        let value = value.as_ref();
44        check_valid_string(value, stringify!(value))?;
45        Ok(Self(Ustr::from(value)))
46    }
47
48    /// Creates a new [`VenueOrderId`] instance.
49    ///
50    /// # Panics
51    ///
52    /// This function panics:
53    /// - If `value` is not a valid string.
54    pub fn new<T: AsRef<str>>(value: T) -> Self {
55        Self::new_checked(value).expect(FAILED)
56    }
57
58    /// Sets the inner identifier value.
59    pub(crate) fn set_inner(&mut self, value: &str) {
60        self.0 = Ustr::from(value);
61    }
62
63    /// Returns the inner identifier value.
64    #[must_use]
65    pub fn inner(&self) -> Ustr {
66        self.0
67    }
68
69    /// Returns the inner identifier value as a string slice.
70    #[must_use]
71    pub fn as_str(&self) -> &str {
72        self.0.as_str()
73    }
74}
75
76impl Debug for VenueOrderId {
77    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
78        write!(f, "{:?}", self.0)
79    }
80}
81
82impl Display for VenueOrderId {
83    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
84        write!(f, "{}", self.0)
85    }
86}
87
88////////////////////////////////////////////////////////////////////////////////
89// Tests
90////////////////////////////////////////////////////////////////////////////////
91#[cfg(test)]
92mod tests {
93    use rstest::rstest;
94
95    use crate::identifiers::{stubs::*, venue_order_id::VenueOrderId};
96
97    #[rstest]
98    fn test_string_reprs(venue_order_id: VenueOrderId) {
99        assert_eq!(venue_order_id.as_str(), "001");
100        assert_eq!(format!("{venue_order_id}"), "001");
101    }
102}