nautilus_model/ffi/identifiers/
account_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
16use std::ffi::c_char;
17
18use nautilus_core::ffi::string::cstr_as_str;
19
20use crate::identifiers::AccountId;
21
22/// Returns a Nautilus identifier from a C string pointer.
23///
24/// # Safety
25///
26/// Assumes `ptr` is a valid C string pointer.
27#[unsafe(no_mangle)]
28pub unsafe extern "C" fn account_id_new(ptr: *const c_char) -> AccountId {
29    let value = unsafe { cstr_as_str(ptr) };
30    AccountId::from(value)
31}
32
33#[unsafe(no_mangle)]
34pub extern "C" fn account_id_hash(id: &AccountId) -> u64 {
35    id.inner().precomputed_hash()
36}
37
38#[cfg(test)]
39mod tests {
40    use std::ffi::{CStr, CString};
41
42    use rstest::rstest;
43
44    use super::*;
45
46    #[rstest]
47    fn test_account_id_round_trip() {
48        let s = "IB-U123456789";
49        let c_string = CString::new(s).unwrap();
50        let ptr = c_string.as_ptr();
51        let account_id = unsafe { account_id_new(ptr) };
52        let char_ptr = account_id.inner().as_char_ptr();
53        let account_id_2 = unsafe { account_id_new(char_ptr) };
54        assert_eq!(account_id, account_id_2);
55    }
56
57    #[rstest]
58    fn test_account_id_to_cstr_and_back() {
59        let s = "IB-U123456789";
60        let c_string = CString::new(s).unwrap();
61        let ptr = c_string.as_ptr();
62        let account_id = unsafe { account_id_new(ptr) };
63        let cstr_ptr = account_id.inner().as_char_ptr();
64        let c_str = unsafe { CStr::from_ptr(cstr_ptr) };
65        assert_eq!(c_str.to_str().unwrap(), s);
66    }
67
68    #[rstest]
69    fn test_account_id_hash_c() {
70        let s1 = "IB-U123456789";
71        let c_string1 = CString::new(s1).unwrap();
72        let ptr1 = c_string1.as_ptr();
73        let account_id1 = unsafe { account_id_new(ptr1) };
74
75        let s2 = "IB-U123456789";
76        let c_string2 = CString::new(s2).unwrap();
77        let ptr2 = c_string2.as_ptr();
78        let account_id2 = unsafe { account_id_new(ptr2) };
79
80        let hash1 = account_id_hash(&account_id1);
81        let hash2 = account_id_hash(&account_id2);
82
83        let s3 = "IB-U987456789";
84        let c_string3 = CString::new(s3).unwrap();
85        let ptr3 = c_string3.as_ptr();
86        let account_id3 = unsafe { account_id_new(ptr3) };
87
88        let hash3 = account_id_hash(&account_id3);
89        assert_eq!(hash1, hash2);
90        assert_ne!(hash1, hash3);
91    }
92}