Skip to content

Commit

Permalink
Reorganize core model crate
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Oct 21, 2023
1 parent 04024ff commit 3758d3d
Show file tree
Hide file tree
Showing 43 changed files with 1,849 additions and 1,557 deletions.
94 changes: 94 additions & 0 deletions nautilus_core/model/src/ffi/identifiers/account_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ffi::c_char;

use nautilus_core::ffi::string::cstr_to_str;

use crate::identifiers::account_id::AccountId;

/// Returns a Nautilus identifier from a C string pointer.
///
/// # Safety
///
/// - Assumes `ptr` is a valid C string pointer.
#[no_mangle]
pub unsafe extern "C" fn account_id_new(ptr: *const c_char) -> AccountId {
AccountId::from(cstr_to_str(ptr))
}

#[no_mangle]
pub extern "C" fn account_id_hash(id: &AccountId) -> u64 {
id.value.precomputed_hash()
}

////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use std::ffi::{CStr, CString};

use rstest::rstest;

use super::*;

#[rstest]
fn test_account_id_round_trip() {
let s = "IB-U123456789";
let c_string = CString::new(s).unwrap();
let ptr = c_string.as_ptr();
let account_id = unsafe { account_id_new(ptr) };
let char_ptr = account_id.value.as_char_ptr();
let account_id_2 = unsafe { account_id_new(char_ptr) };
assert_eq!(account_id, account_id_2);
}

#[rstest]
fn test_account_id_to_cstr_and_back() {
let s = "IB-U123456789";
let c_string = CString::new(s).unwrap();
let ptr = c_string.as_ptr();
let account_id = unsafe { account_id_new(ptr) };
let cstr_ptr = account_id.value.as_char_ptr();
let c_str = unsafe { CStr::from_ptr(cstr_ptr) };
assert_eq!(c_str.to_str().unwrap(), s);
}

#[rstest]
fn test_account_id_hash_c() {
let s1 = "IB-U123456789";
let c_string1 = CString::new(s1).unwrap();
let ptr1 = c_string1.as_ptr();
let account_id1 = unsafe { account_id_new(ptr1) };

let s2 = "IB-U123456789";
let c_string2 = CString::new(s2).unwrap();
let ptr2 = c_string2.as_ptr();
let account_id2 = unsafe { account_id_new(ptr2) };

let hash1 = account_id_hash(&account_id1);
let hash2 = account_id_hash(&account_id2);

let s3 = "IB-U987456789";
let c_string3 = CString::new(s3).unwrap();
let ptr3 = c_string3.as_ptr();
let account_id3 = unsafe { account_id_new(ptr3) };

let hash3 = account_id_hash(&account_id3);
assert_eq!(hash1, hash2);
assert_ne!(hash1, hash3);
}
}
65 changes: 65 additions & 0 deletions nautilus_core/model/src/ffi/identifiers/client_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ffi::c_char;

use nautilus_core::ffi::string::cstr_to_str;

use crate::identifiers::client_id::ClientId;

/// Returns a Nautilus identifier from C string pointer.
///
/// # Safety
///
/// - Assumes `ptr` is a valid C string pointer.
#[no_mangle]
pub unsafe extern "C" fn client_id_new(ptr: *const c_char) -> ClientId {
ClientId::from(cstr_to_str(ptr))
}

#[no_mangle]
pub extern "C" fn client_id_hash(id: &ClientId) -> u64 {
id.value.precomputed_hash()
}

////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use std::ffi::CStr;

use rstest::rstest;

use super::*;
use crate::identifiers::client_id::stubs::{client_id_binance, client_id_dydx};

#[rstest]
fn test_client_id_to_cstr_c() {
let id = ClientId::from("BINANCE");
let c_string = id.value.as_char_ptr();
let rust_string = unsafe { CStr::from_ptr(c_string) }.to_str().unwrap();
assert_eq!(rust_string, "BINANCE");
}

#[rstest]
fn test_client_id_hash_c() {
let id1 = client_id_binance();
let id2 = client_id_binance();
let id3 = client_id_dydx();
assert_eq!(client_id_hash(&id1), client_id_hash(&id2));
assert_ne!(client_id_hash(&id1), client_id_hash(&id3));
}
}
35 changes: 35 additions & 0 deletions nautilus_core/model/src/ffi/identifiers/client_order_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ffi::c_char;

use nautilus_core::ffi::string::cstr_to_str;

use crate::identifiers::client_order_id::ClientOrderId;

/// Returns a Nautilus identifier from a C string pointer.
///
/// # Safety
///
/// - Assumes `ptr` is a valid C string pointer.
#[no_mangle]
pub unsafe extern "C" fn client_order_id_new(ptr: *const c_char) -> ClientOrderId {
ClientOrderId::from(cstr_to_str(ptr))
}

#[no_mangle]
pub extern "C" fn client_order_id_hash(id: &ClientOrderId) -> u64 {
id.value.precomputed_hash()
}
35 changes: 35 additions & 0 deletions nautilus_core/model/src/ffi/identifiers/component_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ffi::c_char;

use nautilus_core::ffi::string::cstr_to_str;

use crate::identifiers::component_id::ComponentId;

/// Returns a Nautilus identifier from a C string pointer.
///
/// # Safety
///
/// - Assumes `ptr` is a valid C string pointer.
#[no_mangle]
pub unsafe extern "C" fn component_id_new(ptr: *const c_char) -> ComponentId {
ComponentId::from(cstr_to_str(ptr))
}

#[no_mangle]
pub extern "C" fn component_id_hash(id: &ComponentId) -> u64 {
id.value.precomputed_hash()
}
35 changes: 35 additions & 0 deletions nautilus_core/model/src/ffi/identifiers/exec_algorithm_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2023 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::ffi::c_char;

use nautilus_core::ffi::string::cstr_to_str;

use crate::identifiers::exec_algorithm_id::ExecAlgorithmId;

/// Returns a Nautilus identifier from a C string pointer.
///
/// # Safety
///
/// - Assumes `ptr` is a valid C string pointer.
#[no_mangle]
pub unsafe extern "C" fn exec_algorithm_id_new(ptr: *const c_char) -> ExecAlgorithmId {
ExecAlgorithmId::from(cstr_to_str(ptr))
}

#[no_mangle]
pub extern "C" fn exec_algorithm_id_hash(id: &ExecAlgorithmId) -> u64 {
id.value.precomputed_hash()
}
Loading

0 comments on commit 3758d3d

Please sign in to comment.