nautilus_model/identifiers/
venue_order_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 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::{FAILED, check_valid_string};
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 = "posei_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    /// Returns an error if `value` is not a valid string.
41    pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
42        let value = value.as_ref();
43        check_valid_string(value, stringify!(value))?;
44        Ok(Self(Ustr::from(value)))
45    }
46
47    /// Creates a new [`VenueOrderId`] instance.
48    ///
49    /// # Panics
50    ///
51    /// Panics if `value` is not a valid string.
52    pub fn new<T: AsRef<str>>(value: T) -> Self {
53        Self::new_checked(value).expect(FAILED)
54    }
55
56    /// Sets the inner identifier value.
57    #[allow(dead_code)]
58    pub(crate) fn set_inner(&mut self, value: &str) {
59        self.0 = Ustr::from(value);
60    }
61
62    /// Returns the inner identifier value.
63    #[must_use]
64    pub fn inner(&self) -> Ustr {
65        self.0
66    }
67
68    /// Returns the inner identifier value as a string slice.
69    #[must_use]
70    pub fn as_str(&self) -> &str {
71        self.0.as_str()
72    }
73}
74
75impl Debug for VenueOrderId {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        write!(f, "{:?}", self.0)
78    }
79}
80
81impl Display for VenueOrderId {
82    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}", self.0)
84    }
85}
86
87////////////////////////////////////////////////////////////////////////////////
88// Tests
89////////////////////////////////////////////////////////////////////////////////
90#[cfg(test)]
91mod tests {
92    use rstest::rstest;
93
94    use crate::identifiers::{stubs::*, venue_order_id::VenueOrderId};
95
96    #[rstest]
97    fn test_string_reprs(venue_order_id: VenueOrderId) {
98        assert_eq!(venue_order_id.as_str(), "001");
99        assert_eq!(format!("{venue_order_id}"), "001");
100    }
101}