nautilus_core/python/
mod.rs#![allow(warnings)] use std::borrow::Cow;
use pyo3::{
exceptions::{PyRuntimeError, PyTypeError, PyValueError},
prelude::*,
wrap_pyfunction,
};
use crate::uuid::UUID4;
pub mod casing;
pub mod datetime;
pub mod serialization;
pub mod uuid;
pub fn get_pytype_name<'p>(obj: &'p PyObject, py: Python<'p>) -> PyResult<&'p str> {
match obj.as_ref(py).get_type().name()? {
Cow::Borrowed(s) => Ok(s),
Cow::Owned(s) => Ok(py.allow_threads(|| Box::leak(s.into_boxed_str()))),
}
}
pub fn to_pyvalue_err(e: impl std::fmt::Display) -> PyErr {
PyValueError::new_err(e.to_string())
}
pub fn to_pytype_err(e: impl std::fmt::Display) -> PyErr {
PyTypeError::new_err(e.to_string())
}
pub fn to_pyruntime_err(e: impl std::fmt::Display) -> PyErr {
PyRuntimeError::new_err(e.to_string())
}
#[pymodule]
pub fn core(_: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<UUID4>()?;
m.add_function(wrap_pyfunction!(casing::py_convert_to_snake_case, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_secs_to_nanos, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_secs_to_millis, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_millis_to_nanos, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_micros_to_nanos, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_nanos_to_secs, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_nanos_to_millis, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
Ok(())
}