diff --git a/nautilus_core/common/src/enums.rs b/nautilus_core/common/src/enums.rs index 3f56d766ac7e..907f0ffd6356 100644 --- a/nautilus_core/common/src/enums.rs +++ b/nautilus_core/common/src/enums.rs @@ -13,9 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- -use std::{ffi::c_char, fmt::Debug, str::FromStr}; +use std::fmt::Debug; -use nautilus_core::ffi::string::{cstr_to_string, str_to_cstr}; use serde::{Deserialize, Serialize}; use strum::{Display, EnumIter, EnumString, FromRepr}; @@ -253,75 +252,3 @@ pub enum LogFormat { #[strum(serialize = "\x1b[4m")] Underline, } - -#[cfg(feature = "ffi")] -#[no_mangle] -pub extern "C" fn component_state_to_cstr(value: ComponentState) -> *const c_char { - str_to_cstr(&value.to_string()) -} - -/// Returns an enum from a Python string. -/// -/// # Safety -/// - Assumes `ptr` is a valid C string pointer. -#[cfg(feature = "ffi")] -#[no_mangle] -pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState { - let value = cstr_to_string(ptr); - ComponentState::from_str(&value) - .unwrap_or_else(|_| panic!("invalid `ComponentState` enum string value, was '{value}'")) -} - -#[cfg(feature = "ffi")] -#[no_mangle] -pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c_char { - str_to_cstr(&value.to_string()) -} - -/// Returns an enum from a Python string. -/// -/// # Safety -/// - Assumes `ptr` is a valid C string pointer. -#[cfg(feature = "ffi")] -#[no_mangle] -pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger { - let value = cstr_to_string(ptr); - ComponentTrigger::from_str(&value) - .unwrap_or_else(|_| panic!("invalid `ComponentTrigger` enum string value, was '{value}'")) -} - -#[cfg(feature = "ffi")] -#[no_mangle] -pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char { - str_to_cstr(&value.to_string()) -} - -/// Returns an enum from a Python string. -/// -/// # Safety -/// - Assumes `ptr` is a valid C string pointer. -#[cfg(feature = "ffi")] -#[no_mangle] -pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel { - let value = cstr_to_string(ptr); - LogLevel::from_str(&value) - .unwrap_or_else(|_| panic!("invalid `LogLevel` enum string value, was '{value}'")) -} - -#[cfg(feature = "ffi")] -#[no_mangle] -pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char { - str_to_cstr(&value.to_string()) -} - -/// Returns an enum from a Python string. -/// -/// # Safety -/// - Assumes `ptr` is a valid C string pointer. -#[cfg(feature = "ffi")] -#[no_mangle] -pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor { - let value = cstr_to_string(ptr); - LogColor::from_str(&value) - .unwrap_or_else(|_| panic!("invalid `LogColor` enum string value, was '{value}'")) -} diff --git a/nautilus_core/common/src/ffi/enums.rs b/nautilus_core/common/src/ffi/enums.rs new file mode 100644 index 000000000000..b205f82047ab --- /dev/null +++ b/nautilus_core/common/src/ffi/enums.rs @@ -0,0 +1,84 @@ +// ------------------------------------------------------------------------------------------------- +// 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, str::FromStr}; + +use nautilus_core::ffi::string::{cstr_to_string, str_to_cstr}; + +use crate::enums::{ComponentState, ComponentTrigger, LogColor, LogLevel}; + +#[no_mangle] +pub extern "C" fn component_state_to_cstr(value: ComponentState) -> *const c_char { + str_to_cstr(&value.to_string()) +} + +/// Returns an enum from a Python string. +/// +/// # Safety +/// - Assumes `ptr` is a valid C string pointer. +#[no_mangle] +pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState { + let value = cstr_to_string(ptr); + ComponentState::from_str(&value) + .unwrap_or_else(|_| panic!("invalid `ComponentState` enum string value, was '{value}'")) +} + +#[no_mangle] +pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c_char { + str_to_cstr(&value.to_string()) +} + +/// Returns an enum from a Python string. +/// +/// # Safety +/// - Assumes `ptr` is a valid C string pointer. +#[no_mangle] +pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger { + let value = cstr_to_string(ptr); + ComponentTrigger::from_str(&value) + .unwrap_or_else(|_| panic!("invalid `ComponentTrigger` enum string value, was '{value}'")) +} + +#[no_mangle] +pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char { + str_to_cstr(&value.to_string()) +} + +/// Returns an enum from a Python string. +/// +/// # Safety +/// - Assumes `ptr` is a valid C string pointer. +#[no_mangle] +pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel { + let value = cstr_to_string(ptr); + LogLevel::from_str(&value) + .unwrap_or_else(|_| panic!("invalid `LogLevel` enum string value, was '{value}'")) +} + +#[no_mangle] +pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char { + str_to_cstr(&value.to_string()) +} + +/// Returns an enum from a Python string. +/// +/// # Safety +/// - Assumes `ptr` is a valid C string pointer. +#[no_mangle] +pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor { + let value = cstr_to_string(ptr); + LogColor::from_str(&value) + .unwrap_or_else(|_| panic!("invalid `LogColor` enum string value, was '{value}'")) +} diff --git a/nautilus_core/common/src/ffi/mod.rs b/nautilus_core/common/src/ffi/mod.rs index 23f8f3946b84..705e0b70cb4d 100644 --- a/nautilus_core/common/src/ffi/mod.rs +++ b/nautilus_core/common/src/ffi/mod.rs @@ -14,6 +14,7 @@ // ------------------------------------------------------------------------------------------------- pub mod clock; +pub mod enums; pub mod logging; pub mod msgbus; pub mod timer; diff --git a/nautilus_trader/core/includes/common.h b/nautilus_trader/core/includes/common.h index 3b753a7dd221..149e4a731a77 100644 --- a/nautilus_trader/core/includes/common.h +++ b/nautilus_trader/core/includes/common.h @@ -322,46 +322,6 @@ typedef struct TimeEventHandler_t { PyObject *callback_ptr; } TimeEventHandler_t; -const char *component_state_to_cstr(enum ComponentState value); - -/** - * Returns an enum from a Python string. - * - * # Safety - * - Assumes `ptr` is a valid C string pointer. - */ -enum ComponentState component_state_from_cstr(const char *ptr); - -const char *component_trigger_to_cstr(enum ComponentTrigger value); - -/** - * Returns an enum from a Python string. - * - * # Safety - * - Assumes `ptr` is a valid C string pointer. - */ -enum ComponentTrigger component_trigger_from_cstr(const char *ptr); - -const char *log_level_to_cstr(enum LogLevel value); - -/** - * Returns an enum from a Python string. - * - * # Safety - * - Assumes `ptr` is a valid C string pointer. - */ -enum LogLevel log_level_from_cstr(const char *ptr); - -const char *log_color_to_cstr(enum LogColor value); - -/** - * Returns an enum from a Python string. - * - * # Safety - * - Assumes `ptr` is a valid C string pointer. - */ -enum LogColor log_color_from_cstr(const char *ptr); - struct TestClock_API test_clock_new(void); void test_clock_drop(struct TestClock_API clock); @@ -448,6 +408,46 @@ uint64_t live_clock_timestamp_us(struct LiveClock_API *clock); uint64_t live_clock_timestamp_ns(struct LiveClock_API *clock); +const char *component_state_to_cstr(enum ComponentState value); + +/** + * Returns an enum from a Python string. + * + * # Safety + * - Assumes `ptr` is a valid C string pointer. + */ +enum ComponentState component_state_from_cstr(const char *ptr); + +const char *component_trigger_to_cstr(enum ComponentTrigger value); + +/** + * Returns an enum from a Python string. + * + * # Safety + * - Assumes `ptr` is a valid C string pointer. + */ +enum ComponentTrigger component_trigger_from_cstr(const char *ptr); + +const char *log_level_to_cstr(enum LogLevel value); + +/** + * Returns an enum from a Python string. + * + * # Safety + * - Assumes `ptr` is a valid C string pointer. + */ +enum LogLevel log_level_from_cstr(const char *ptr); + +const char *log_color_to_cstr(enum LogColor value); + +/** + * Returns an enum from a Python string. + * + * # Safety + * - Assumes `ptr` is a valid C string pointer. + */ +enum LogColor log_color_from_cstr(const char *ptr); + /** * Creates a new logger. * diff --git a/nautilus_trader/core/rust/common.pxd b/nautilus_trader/core/rust/common.pxd index f9376f2b9ff4..7b98642e735d 100644 --- a/nautilus_trader/core/rust/common.pxd +++ b/nautilus_trader/core/rust/common.pxd @@ -199,38 +199,6 @@ cdef extern from "../includes/common.h": # The event ID. PyObject *callback_ptr; - const char *component_state_to_cstr(ComponentState value); - - # Returns an enum from a Python string. - # - # # Safety - # - Assumes `ptr` is a valid C string pointer. - ComponentState component_state_from_cstr(const char *ptr); - - const char *component_trigger_to_cstr(ComponentTrigger value); - - # Returns an enum from a Python string. - # - # # Safety - # - Assumes `ptr` is a valid C string pointer. - ComponentTrigger component_trigger_from_cstr(const char *ptr); - - const char *log_level_to_cstr(LogLevel value); - - # Returns an enum from a Python string. - # - # # Safety - # - Assumes `ptr` is a valid C string pointer. - LogLevel log_level_from_cstr(const char *ptr); - - const char *log_color_to_cstr(LogColor value); - - # Returns an enum from a Python string. - # - # # Safety - # - Assumes `ptr` is a valid C string pointer. - LogColor log_color_from_cstr(const char *ptr); - TestClock_API test_clock_new(); void test_clock_drop(TestClock_API clock); @@ -305,6 +273,38 @@ cdef extern from "../includes/common.h": uint64_t live_clock_timestamp_ns(LiveClock_API *clock); + const char *component_state_to_cstr(ComponentState value); + + # Returns an enum from a Python string. + # + # # Safety + # - Assumes `ptr` is a valid C string pointer. + ComponentState component_state_from_cstr(const char *ptr); + + const char *component_trigger_to_cstr(ComponentTrigger value); + + # Returns an enum from a Python string. + # + # # Safety + # - Assumes `ptr` is a valid C string pointer. + ComponentTrigger component_trigger_from_cstr(const char *ptr); + + const char *log_level_to_cstr(LogLevel value); + + # Returns an enum from a Python string. + # + # # Safety + # - Assumes `ptr` is a valid C string pointer. + LogLevel log_level_from_cstr(const char *ptr); + + const char *log_color_to_cstr(LogColor value); + + # Returns an enum from a Python string. + # + # # Safety + # - Assumes `ptr` is a valid C string pointer. + LogColor log_color_from_cstr(const char *ptr); + # Creates a new logger. # # # Safety