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// -------------------------------------------------------------------------------------------------
1516//! Python bindings from [PyO3](https://pyo3.rs).
1718pub mod casing;
19pub mod datetime;
20pub mod serialization;
21pub mod uuid;
22pub mod version;
2324use pyo3::{
25 conversion::IntoPyObjectExt,
26 exceptions::{PyRuntimeError, PyTypeError, PyValueError},
27 prelude::*,
28 types::PyString,
29 wrap_pyfunction,
30};
3132use crate::{
33 UUID4,
34 consts::{NAUTILUS_USER_AGENT, NAUTILUS_VERSION},
35 datetime::{
36 MILLISECONDS_IN_SECOND, NANOSECONDS_IN_MICROSECOND, NANOSECONDS_IN_MILLISECOND,
37 NANOSECONDS_IN_SECOND,
38 },
39};
4041/// Extend `IntoPyObjectExt` helper trait to unwrap `PyObject` after conversion.
42pub trait IntoPyObjectNautilusExt<'py>: IntoPyObjectExt<'py> {
43#[inline]
44fn into_py_any_unwrap(self, py: Python<'py>) -> PyObject {
45self.into_py_any(py)
46 .expect("Failed to convert type to PyObject")
47 }
48}
4950impl<'py, T> IntoPyObjectNautilusExt<'py> for T where T: IntoPyObjectExt<'py> {}
5152/// Gets the type name for the given Python `obj`.
53///
54/// # Errors
55///
56/// Returns a error if accessing the type name fails.
57pub fn get_pytype_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyString>> {
58 obj.get_type().name()
59}
6061/// Converts any type that implements `Display` to a Python `ValueError`.
62///
63/// # Errors
64///
65/// Returns a Python error with the error string.
66pub fn to_pyvalue_err(e: impl std::fmt::Display) -> PyErr {
67 PyValueError::new_err(e.to_string())
68}
6970/// Converts any type that implements `Display` to a Python `TypeError`.
71///
72/// # Errors
73///
74/// Returns a Python error with the error string.
75pub fn to_pytype_err(e: impl std::fmt::Display) -> PyErr {
76 PyTypeError::new_err(e.to_string())
77}
7879/// Converts any type that implements `Display` to a Python `RuntimeError`.
80///
81/// # Errors
82///
83/// Returns a Python error with the error string.
84pub fn to_pyruntime_err(e: impl std::fmt::Display) -> PyErr {
85 PyRuntimeError::new_err(e.to_string())
86}
8788#[pyfunction]
89fn is_pycapsule(obj: PyObject) -> bool {
90unsafe {
91// PyCapsule_CheckExact checks if the object is exactly a PyCapsule
92pyo3::ffi::PyCapsule_CheckExact(obj.as_ptr()) != 0
93}
94}
9596/// Loaded as nautilus_pyo3.core
97///
98/// # Errors
99///
100/// Returns a `PyErr` if registering any module components fails.
101#[pymodule]
102#[rustfmt::skip]
103pub fn core(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
104 m.add(stringify!(NAUTILUS_VERSION), NAUTILUS_VERSION)?;
105 m.add(stringify!(NAUTILUS_USER_AGENT), NAUTILUS_USER_AGENT)?;
106 m.add(stringify!(MILLISECONDS_IN_SECOND), MILLISECONDS_IN_SECOND)?;
107 m.add(stringify!(NANOSECONDS_IN_SECOND), NANOSECONDS_IN_SECOND)?;
108 m.add(stringify!(NANOSECONDS_IN_MILLISECOND), NANOSECONDS_IN_MILLISECOND)?;
109 m.add(stringify!(NANOSECONDS_IN_MICROSECOND), NANOSECONDS_IN_MICROSECOND)?;
110 m.add_class::<UUID4>()?;
111 m.add_function(wrap_pyfunction!(is_pycapsule, m)?)?;
112 m.add_function(wrap_pyfunction!(casing::py_convert_to_snake_case, m)?)?;
113 m.add_function(wrap_pyfunction!(datetime::py_secs_to_nanos, m)?)?;
114 m.add_function(wrap_pyfunction!(datetime::py_secs_to_millis, m)?)?;
115 m.add_function(wrap_pyfunction!(datetime::py_millis_to_nanos, m)?)?;
116 m.add_function(wrap_pyfunction!(datetime::py_micros_to_nanos, m)?)?;
117 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_secs, m)?)?;
118 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_millis, m)?)?;
119 m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
120 m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
121 m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
122 m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
123Ok(())
124}