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// -------------------------------------------------------------------------------------------------
1516use std::{ffi::c_char, str::FromStr};
1718use nautilus_core::ffi::string::{cstr_as_str, str_to_cstr};
1920use crate::enums::{ComponentState, ComponentTrigger, LogColor, LogLevel};
2122#[unsafe(no_mangle)]
23pub extern "C" fn component_state_to_cstr(value: ComponentState) -> *const c_char {
24 str_to_cstr(&value.to_string())
25}
2627/// Returns an enum from a Python string.
28///
29/// # Safety
30///
31/// - Assumes `ptr` is a valid C string pointer.
32#[unsafe(no_mangle)]
33pub unsafe extern "C" fn component_state_from_cstr(ptr: *const c_char) -> ComponentState {
34let value = unsafe { cstr_as_str(ptr) };
35 ComponentState::from_str(value)
36 .unwrap_or_else(|_| panic!("invalid `ComponentState` enum string value, was '{value}'"))
37}
3839#[unsafe(no_mangle)]
40pub extern "C" fn component_trigger_to_cstr(value: ComponentTrigger) -> *const c_char {
41 str_to_cstr(&value.to_string())
42}
4344/// Returns an enum from a Python string.
45///
46/// # Safety
47///
48/// - Assumes `ptr` is a valid C string pointer.
49#[unsafe(no_mangle)]
50pub unsafe extern "C" fn component_trigger_from_cstr(ptr: *const c_char) -> ComponentTrigger {
51let value = unsafe { cstr_as_str(ptr) };
52 ComponentTrigger::from_str(value)
53 .unwrap_or_else(|_| panic!("invalid `ComponentTrigger` enum string value, was '{value}'"))
54}
5556#[unsafe(no_mangle)]
57pub extern "C" fn log_level_to_cstr(value: LogLevel) -> *const c_char {
58 str_to_cstr(&value.to_string())
59}
6061/// Returns an enum from a Python string.
62///
63/// # Safety
64///
65/// - Assumes `ptr` is a valid C string pointer.
66#[unsafe(no_mangle)]
67pub unsafe extern "C" fn log_level_from_cstr(ptr: *const c_char) -> LogLevel {
68let value = unsafe { cstr_as_str(ptr) };
69 LogLevel::from_str(value)
70 .unwrap_or_else(|_| panic!("invalid `LogLevel` enum string value, was '{value}'"))
71}
7273#[unsafe(no_mangle)]
74pub extern "C" fn log_color_to_cstr(value: LogColor) -> *const c_char {
75 str_to_cstr(&value.to_string())
76}
7778/// Returns an enum from a Python string.
79///
80/// # Safety
81///
82/// - Assumes `ptr` is a valid C string pointer.
83#[unsafe(no_mangle)]
84pub unsafe extern "C" fn log_color_from_cstr(ptr: *const c_char) -> LogColor {
85let value = unsafe { cstr_as_str(ptr) };
86 LogColor::from_str(value)
87 .unwrap_or_else(|_| panic!("invalid `LogColor` enum string value, was '{value}'"))
88}