nautilus_common/ffi/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.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#[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#[no_mangle]
33pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState {
34    let value = cstr_as_str(ptr);
35    ComponentState::from_str(value)
36        .unwrap_or_else(|_| panic!("invalid `ComponentState` enum string value, was '{value}'"))
37}
38
39#[no_mangle]
40pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c_char {
41    str_to_cstr(&value.to_string())
42}
43
44/// Returns an enum from a Python string.
45///
46/// # Safety
47///
48/// - Assumes `ptr` is a valid C string pointer.
49#[no_mangle]
50pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger {
51    let value = cstr_as_str(ptr);
52    ComponentTrigger::from_str(value)
53        .unwrap_or_else(|_| panic!("invalid `ComponentTrigger` enum string value, was '{value}'"))
54}
55
56#[no_mangle]
57pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char {
58    str_to_cstr(&value.to_string())
59}
60
61/// Returns an enum from a Python string.
62///
63/// # Safety
64///
65/// - Assumes `ptr` is a valid C string pointer.
66#[no_mangle]
67pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel {
68    let value = cstr_as_str(ptr);
69    LogLevel::from_str(value)
70        .unwrap_or_else(|_| panic!("invalid `LogLevel` enum string value, was '{value}'"))
71}
72
73#[no_mangle]
74pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char {
75    str_to_cstr(&value.to_string())
76}
77
78/// Returns an enum from a Python string.
79///
80/// # Safety
81///
82/// - Assumes `ptr` is a valid C string pointer.
83#[no_mangle]
84pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor {
85    let value = cstr_as_str(ptr);
86    LogColor::from_str(value)
87        .unwrap_or_else(|_| panic!("invalid `LogColor` enum string value, was '{value}'"))
88}