nautilus_model/identifiers/
account_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 account ID.
17
18use std::{
19    fmt::{Debug, Display},
20    hash::Hash,
21};
22
23use nautilus_core::correctness::{FAILED, check_string_contains, check_valid_string_ascii};
24use ustr::Ustr;
25
26use super::Venue;
27
28/// Represents a valid account ID.
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")
34)]
35pub struct AccountId(Ustr);
36
37impl AccountId {
38    /// Creates a new [`AccountId`] instance with correctness checking.
39    ///
40    /// Must be correctly formatted with two valid strings either side of a hyphen '-'.
41    ///
42    /// It is expected an account ID is the name of the issuer with an account number
43    /// separated by a hyphen.
44    ///
45    /// # Errors
46    ///
47    /// Returns an error if:
48    /// - `value` is not a valid ASCII string.
49    /// - `value` does not contain a hyphen '-' separator.
50    /// - Either the issuer or account part (before/after the hyphen) is empty.
51    ///
52    /// # Notes
53    ///
54    /// PyO3 requires a `Result` type for proper error handling and stacktrace printing in Python.
55    pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
56        let value = value.as_ref();
57        check_valid_string_ascii(value, stringify!(value))?;
58        check_string_contains(value, "-", stringify!(value))?;
59
60        if let Some((issuer, account)) = value.split_once('-') {
61            anyhow::ensure!(
62                !issuer.is_empty(),
63                "`value` issuer part (before '-') cannot be empty"
64            );
65            anyhow::ensure!(
66                !account.is_empty(),
67                "`value` account part (after '-') cannot be empty"
68            );
69        }
70
71        Ok(Self(Ustr::from(value)))
72    }
73
74    /// Creates a new [`AccountId`] instance.
75    ///
76    /// # Panics
77    ///
78    /// Panics if `value` is not a valid string, or value length is greater than 36.
79    pub fn new<T: AsRef<str>>(value: T) -> Self {
80        Self::new_checked(value).expect(FAILED)
81    }
82
83    /// Sets the inner identifier value.
84    #[cfg_attr(not(feature = "python"), allow(dead_code))]
85    pub(crate) fn set_inner(&mut self, value: &str) {
86        self.0 = Ustr::from(value);
87    }
88
89    /// Returns the inner identifier value.
90    #[must_use]
91    pub fn inner(&self) -> Ustr {
92        self.0
93    }
94
95    /// Returns the inner identifier value as a string slice.
96    #[must_use]
97    pub fn as_str(&self) -> &str {
98        self.0.as_str()
99    }
100
101    /// Returns the account issuer for this identifier.
102    ///
103    /// # Panics
104    ///
105    /// Panics if the internal ID does not contain a hyphen separator.
106    #[must_use]
107    pub fn get_issuer(&self) -> Venue {
108        // SAFETY: Account ID is guaranteed to have chars either side of a hyphen
109        Venue::from_str_unchecked(self.0.split_once('-').unwrap().0)
110    }
111
112    /// Returns the account ID assigned by the issuer.
113    ///
114    /// # Panics
115    ///
116    /// Panics if the internal ID does not contain a hyphen separator.
117    #[must_use]
118    pub fn get_issuers_id(&self) -> &str {
119        // SAFETY: Account ID is guaranteed to have chars either side of a hyphen
120        self.0.split_once('-').unwrap().1
121    }
122}
123
124impl Debug for AccountId {
125    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126        write!(f, "\"{}\"", self.0)
127    }
128}
129
130impl Display for AccountId {
131    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132        write!(f, "{}", self.0)
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use rstest::rstest;
139
140    use super::*;
141    use crate::identifiers::stubs::*;
142
143    #[rstest]
144    #[should_panic]
145    fn test_account_id_new_invalid_string() {
146        AccountId::new("");
147    }
148
149    #[rstest]
150    #[should_panic]
151    fn test_account_id_new_missing_hyphen() {
152        AccountId::new("123456789");
153    }
154
155    #[rstest]
156    fn test_account_id_fmt() {
157        let s = "IB-U123456789";
158        let account_id = AccountId::new(s);
159        let formatted = format!("{account_id}");
160        assert_eq!(formatted, s);
161    }
162
163    #[rstest]
164    fn test_string_reprs(account_ib: AccountId) {
165        assert_eq!(account_ib.as_str(), "IB-1234567890");
166    }
167
168    #[rstest]
169    fn test_get_issuer(account_ib: AccountId) {
170        assert_eq!(account_ib.get_issuer(), Venue::new("IB"));
171    }
172
173    #[rstest]
174    fn test_get_issuers_id(account_ib: AccountId) {
175        assert_eq!(account_ib.get_issuers_id(), "1234567890");
176    }
177
178    #[rstest]
179    #[should_panic(expected = "issuer part (before '-') cannot be empty")]
180    fn test_new_with_empty_issuer_panics() {
181        let _ = AccountId::new("-123456");
182    }
183
184    #[rstest]
185    #[should_panic(expected = "account part (after '-') cannot be empty")]
186    fn test_new_with_empty_account_panics() {
187        let _ = AccountId::new("IB-");
188    }
189
190    #[rstest]
191    fn test_new_checked_with_empty_issuer_returns_error() {
192        assert!(AccountId::new_checked("-123456").is_err());
193    }
194
195    #[rstest]
196    fn test_new_checked_with_empty_account_returns_error() {
197        assert!(AccountId::new_checked("IB-").is_err());
198    }
199}