Skip to main content

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