Skip to content

Commit

Permalink
Move common FFI enum functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Nov 12, 2023
1 parent d8b7763 commit 9ff7a6d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 146 deletions.
75 changes: 1 addition & 74 deletions nautilus_core/common/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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}'"))
}
84 changes: 84 additions & 0 deletions nautilus_core/common/src/ffi/enums.rs
Original file line number Diff line number Diff line change
@@ -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}'"))
}
1 change: 1 addition & 0 deletions nautilus_core/common/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// -------------------------------------------------------------------------------------------------

pub mod clock;
pub mod enums;
pub mod logging;
pub mod msgbus;
pub mod timer;
80 changes: 40 additions & 40 deletions nautilus_trader/core/includes/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
64 changes: 32 additions & 32 deletions nautilus_trader/core/rust/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ff7a6d

Please sign in to comment.