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////////////////////////////////////////////////////////////////////////////////
39// Tests
40////////////////////////////////////////////////////////////////////////////////
41#[cfg(test)]
42mod tests {
43    use std::ffi::{CStr, CString};
44
45    use rstest::rstest;
46
47    use super::*;
48
49    #[rstest]
50    fn test_account_id_round_trip() {
51        let s = "IB-U123456789";
52        let c_string = CString::new(s).unwrap();
53        let ptr = c_string.as_ptr();
54        let account_id = unsafe { account_id_new(ptr) };
55        let char_ptr = account_id.inner().as_char_ptr();
56        let account_id_2 = unsafe { account_id_new(char_ptr) };
57        assert_eq!(account_id, account_id_2);
58    }
59
60    #[rstest]
61    fn test_account_id_to_cstr_and_back() {
62        let s = "IB-U123456789";
63        let c_string = CString::new(s).unwrap();
64        let ptr = c_string.as_ptr();
65        let account_id = unsafe { account_id_new(ptr) };
66        let cstr_ptr = account_id.inner().as_char_ptr();
67        let c_str = unsafe { CStr::from_ptr(cstr_ptr) };
68        assert_eq!(c_str.to_str().unwrap(), s);
69    }
70
71    #[rstest]
72    fn test_account_id_hash_c() {
73        let s1 = "IB-U123456789";
74        let c_string1 = CString::new(s1).unwrap();
75        let ptr1 = c_string1.as_ptr();
76        let account_id1 = unsafe { account_id_new(ptr1) };
77
78        let s2 = "IB-U123456789";
79        let c_string2 = CString::new(s2).unwrap();
80        let ptr2 = c_string2.as_ptr();
81        let account_id2 = unsafe { account_id_new(ptr2) };
82
83        let hash1 = account_id_hash(&account_id1);
84        let hash2 = account_id_hash(&account_id2);
85
86        let s3 = "IB-U987456789";
87        let c_string3 = CString::new(s3).unwrap();
88        let ptr3 = c_string3.as_ptr();
89        let account_id3 = unsafe { account_id_new(ptr3) };
90
91        let hash3 = account_id_hash(&account_id3);
92        assert_eq!(hash1, hash2);
93        assert_ne!(hash1, hash3);
94    }
95}