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