nautilus_common/ffi/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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, str::FromStr};
17
18use nautilus_core::ffi::string::{cstr_as_str, str_to_cstr};
19
20use crate::enums::{ComponentState, ComponentTrigger, LogColor, LogLevel};
21
22#[unsafe(no_mangle)]
23pub extern "C" fn component_state_to_cstr(value: ComponentState) -> *const c_char {
24    str_to_cstr(&value.to_string())
25}
26
27/// Returns an enum from a Python string.
28///
29/// # Safety
30///
31/// Assumes `ptr` is a valid C string pointer.
32///
33/// # Panics
34///
35/// Panics if the input C string does not match a valid enum variant.
36#[unsafe(no_mangle)]
37pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState {
38    let value = unsafe { cstr_as_str(ptr) };
39    ComponentState::from_str(value)
40        .unwrap_or_else(|_| panic!("invalid `ComponentState` enum string value, was '{value}'"))
41}
42
43#[unsafe(no_mangle)]
44pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c_char {
45    str_to_cstr(&value.to_string())
46}
47
48/// Returns an enum from a Python string.
49///
50/// # Safety
51///
52/// Assumes `ptr` is a valid C string pointer.
53///
54/// # Panics
55///
56/// Panics if the input C string does not match a valid enum variant.
57#[unsafe(no_mangle)]
58pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger {
59    let value = unsafe { cstr_as_str(ptr) };
60    ComponentTrigger::from_str(value)
61        .unwrap_or_else(|_| panic!("invalid `ComponentTrigger` enum string value, was '{value}'"))
62}
63
64#[unsafe(no_mangle)]
65pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char {
66    str_to_cstr(&value.to_string())
67}
68
69/// Returns an enum from a Python string.
70///
71/// # Safety
72///
73/// Assumes `ptr` is a valid C string pointer.
74///
75/// # Panics
76///
77/// Panics if the input C string does not match a valid enum variant.
78#[unsafe(no_mangle)]
79pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel {
80    let value = unsafe { cstr_as_str(ptr) };
81    LogLevel::from_str(value)
82        .unwrap_or_else(|_| panic!("invalid `LogLevel` enum string value, was '{value}'"))
83}
84
85#[unsafe(no_mangle)]
86pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char {
87    str_to_cstr(&value.to_string())
88}
89
90/// Returns an enum from a Python string.
91///
92/// # Safety
93///
94/// Assumes `ptr` is a valid C string pointer.
95///
96/// # Panics
97///
98/// Panics if the input C string does not match a valid enum variant.
99#[unsafe(no_mangle)]
100pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor {
101    let value = unsafe { cstr_as_str(ptr) };
102    LogColor::from_str(value)
103        .unwrap_or_else(|_| panic!("invalid `LogColor` enum string value, was '{value}'"))
104}