use pyo3::prelude::*;
use pyo3::sync::PyOnceLock;
use pyo3::types::{
PyBool, PyBytes, PyDict, PyFloat, PyInt, PyList, PyNone, PyString, PyTuple, PyType,
};
use uuid::Uuid;
#[inline]
fn is_clean_primitive(obj: &Bound<'_, PyAny>) -> bool {
obj.is_instance_of::<PyString>()
|| obj.is_instance_of::<PyBool>()
|| obj.is_instance_of::<PyInt>()
|| obj.is_instance_of::<PyFloat>()
|| obj.is_instance_of::<PyNone>()
}
const MAX_DEPTH: usize = 128;
static DECIMAL_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
fn is_decimal(obj: &Bound<'_, PyAny>) -> PyResult<bool> {
let decimal = DECIMAL_TYPE.import(obj.py(), "decimal", "Decimal")?;
obj.is_instance(decimal)
}
fn is_json_ready(obj: &Bound<'_, PyAny>, depth: usize) -> bool {
if is_clean_primitive(obj) {
return true;
}
if depth >= MAX_DEPTH {
return false;
}
if let Ok(list) = obj.cast::<PyList>() {
return list.iter().all(|item| is_json_ready(&item, depth + 1));
}
if let Ok(dict) = obj.cast::<PyDict>() {
return dict.iter().all(|(_, v)| is_json_ready(&v, depth + 1));
}
false
}
fn enter_container(obj: &Bound<'_, PyAny>, path: &mut Vec<usize>) -> PyResult<()> {
let id = obj.as_ptr() as usize;
if path.contains(&id) {
return Err(pyo3::exceptions::PyValueError::new_err(
"cannot convert a value that contains a reference cycle",
));
}
if path.len() >= MAX_DEPTH {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"cannot convert a value nested deeper than {MAX_DEPTH} levels"
)));
}
path.push(id);
Ok(())
}
fn convert_value(py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
convert_at(py, obj, &mut Vec::new())
}
fn convert_at(
py: Python<'_>,
obj: &Bound<'_, PyAny>,
path: &mut Vec<usize>,
) -> PyResult<Py<PyAny>> {
if is_clean_primitive(obj) {
return Ok(obj.clone().unbind());
}
if let Ok(dict) = obj.cast::<PyDict>() {
if is_json_ready(obj, path.len()) {
return Ok(dict.clone().into_any().unbind());
}
enter_container(obj, path)?;
let new_dict = PyDict::new(py);
for (k, v) in dict.iter() {
let converted = convert_at(py, &v, path)?;
new_dict.set_item(k, converted)?;
}
path.pop();
return Ok(new_dict.into_any().unbind());
}
if let Ok(pybytes) = obj.cast::<PyBytes>() {
let raw: &[u8] = pybytes.as_bytes();
if let Ok(s) = std::str::from_utf8(raw) {
return Ok(PyString::new(py, s).into_any().unbind());
}
let s: String = raw.iter().map(|&b| b as char).collect();
return Ok(PyString::new(py, &s).into_any().unbind());
}
if obj.hasattr("hex")?
&& let Ok(hex_val) = obj.getattr("hex")
&& let Ok(hex_str) = hex_val.extract::<String>()
&& hex_str.len() == 32
{
let s = obj.str()?;
return Ok(s.into_any().unbind());
}
if obj.hasattr("isoformat")? && obj.hasattr("year")? {
let iso = obj.call_method0("isoformat")?;
return Ok(iso.unbind());
}
if is_decimal(obj)? {
return Ok(obj.str()?.into_any().unbind());
}
if obj.hasattr("value")? && obj.hasattr("name")? {
enter_container(obj, path)?;
let converted = convert_at(py, &obj.getattr("value")?, path)?;
path.pop();
return Ok(converted);
}
if let Ok(list) = obj.cast::<PyList>() {
if is_json_ready(obj, path.len()) {
return Ok(list.clone().into_any().unbind());
}
enter_container(obj, path)?;
let new_list = PyList::empty(py);
for item in list.iter() {
let converted = convert_at(py, &item, path)?;
new_list.append(converted)?;
}
path.pop();
return Ok(new_list.into_any().unbind());
}
if let Ok(tuple) = obj.cast::<PyTuple>() {
enter_container(obj, path)?;
let new_list = PyList::empty(py);
for item in tuple.iter() {
let converted = convert_at(py, &item, path)?;
new_list.append(converted)?;
}
path.pop();
return Ok(new_list.into_any().unbind());
}
if obj.hasattr("__dict__")? {
let dunder_dict = obj.getattr("__dict__")?;
if let Ok(dict) = dunder_dict.cast::<PyDict>() {
enter_container(obj, path)?;
let converted = convert_at(py, dict.as_any(), path)?;
path.pop();
return Ok(converted);
}
}
Ok(obj.clone().unbind())
}
fn ensure_str(py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(pybytes) = obj.cast::<PyBytes>() {
let raw = pybytes.as_bytes();
if let Ok(s) = std::str::from_utf8(raw) {
return Ok(PyString::new(py, s).into_any().unbind());
}
let s: String = raw.iter().map(|&b| b as char).collect();
return Ok(PyString::new(py, &s).into_any().unbind());
}
if obj.hasattr("hex")?
&& let Ok(hex_val) = obj.getattr("hex")
&& let Ok(hex_str) = hex_val.extract::<String>()
&& hex_str.len() == 32
{
let s = obj.str()?;
return Ok(s.into_any().unbind());
}
if obj.hasattr("isoformat")? && obj.hasattr("year")? {
let iso = obj.call_method0("isoformat")?;
return Ok(iso.unbind());
}
if obj.hasattr("value")? && obj.hasattr("name")? {
let val = obj.getattr("value")?;
return Ok(val.unbind());
}
Ok(obj.clone().unbind())
}
#[inline]
fn dict_get_str(dict: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<String>> {
match dict.get_item(key)? {
Some(val) if !val.is_none() => Ok(Some(val.str()?.to_string())),
_ => Ok(None),
}
}
#[inline]
fn dict_get<'py>(dict: &Bound<'py, PyDict>, key: &str) -> PyResult<Option<Bound<'py, PyAny>>> {
match dict.get_item(key)? {
Some(val) if !val.is_none() => Ok(Some(val)),
_ => Ok(None),
}
}
fn compute_latency_ms(timestamp_str: &str) -> Option<i64> {
let then = crate::rfc3339::parse_millis(timestamp_str)?;
Some(crate::rfc3339::now_millis() - then)
}
fn extract_payload<'py>(
py: Python<'py>,
event: &Bound<'py, PyDict>,
) -> PyResult<Bound<'py, PyDict>> {
let source = match dict_get(event, "payload")? {
Some(val) => {
if let Ok(d) = val.cast::<PyDict>() {
d.clone()
} else {
event.clone()
}
}
None => event.clone(),
};
let result = PyDict::new(py);
for (k, v) in source.iter() {
let key_str = k.str()?.to_string();
if key_str == "_metadata" {
continue;
}
let converted = convert_value(py, &v)?;
result.set_item(k, converted)?;
}
let has_event_type = result.contains("event_type")?;
if !has_event_type && let Some(type_val) = dict_get(&result, "type")? {
result.set_item("event_type", type_val)?;
}
Ok(result)
}
#[pyfunction]
pub fn rust_transform_event<'py>(
py: Python<'py>,
event: &Bound<'py, PyDict>,
sequence: i64,
event_type_map: &Bound<'py, PyDict>,
) -> PyResult<Bound<'py, PyDict>> {
let has_v = event.contains("v")?;
let has_t = event.contains("t")?;
let has_p = event.contains("p")?;
let has_id = event.contains("id")?;
let has_ts = event.contains("ts")?;
if has_v && has_t && has_p && has_id && has_ts {
if event.contains("seq")? {
return Ok(event.clone());
}
let stamped = event.copy()?;
stamped.set_item("seq", sequence)?;
return Ok(stamped);
}
let raw_event_type: String = {
let val = dict_get(event, "event_type")?.or(dict_get(event, "type")?);
match val {
Some(v) => {
let converted: Py<PyAny> = ensure_str(py, &v)?;
converted.bind(py).str()?.to_string()
}
None => "unknown".to_string(),
}
};
let ws_event_type: String = match event_type_map.get_item(&raw_event_type)? {
Some(mapped) => mapped.str()?.to_string(),
None => raw_event_type.to_lowercase(),
};
let metadata: Option<Bound<'py, PyDict>> = match dict_get(event, "_metadata")? {
Some(val) => val.cast::<PyDict>().ok().cloned(),
None => None,
};
let event_timestamp_str: Option<String> = {
let from_meta = metadata
.as_ref()
.and_then(|m| dict_get_str(m, "timestamp").ok().flatten());
let from_event = dict_get_str(event, "timestamp").ok().flatten();
from_meta.or(from_event)
};
let event_latency_ms: Option<i64> = event_timestamp_str.as_deref().and_then(compute_latency_ms);
let event_id: String = {
let from_meta = metadata
.as_ref()
.and_then(|m| dict_get(m, "event_id").ok().flatten());
let from_event = dict_get(event, "event_id")?.filter(|v| !v.is_none());
match from_meta.or(from_event) {
Some(val) => {
let converted: Py<PyAny> = ensure_str(py, &val)?;
converted.bind(py).str()?.to_string()
}
None => Uuid::now_v7().to_string(),
}
};
let timestamp: String = {
let from_meta = metadata
.as_ref()
.and_then(|m| dict_get(m, "timestamp").ok().flatten());
let from_event = dict_get(event, "timestamp")?.filter(|v| !v.is_none());
match from_meta.or(from_event) {
Some(val) => {
let converted: Py<PyAny> = ensure_str(py, &val)?;
converted.bind(py).str()?.to_string()
}
None => crate::rfc3339::now(6),
}
};
let payload = extract_payload(py, event)?;
let payload_converted = convert_value(py, payload.as_any())?;
let envelope = PyDict::new(py);
if let Some(msg_cat) = dict_get(event, "c")? {
envelope.set_item("c", msg_cat)?;
} else if ws_event_type.contains("_snapshot") || ws_event_type.contains("snapshot_") {
envelope.set_item("c", "S")?;
} else {
const WSE_TYPES: &[&str] = &[
"server_ready",
"snapshot_complete",
"client_hello_ack",
"connection_state_change",
"error",
"pong",
];
if WSE_TYPES.contains(&ws_event_type.as_str()) {
envelope.set_item("c", "WSE")?;
} else {
envelope.set_item("c", "U")?;
}
}
envelope.set_item("t", PyString::new(py, &ws_event_type))?;
envelope.set_item("id", PyString::new(py, &event_id))?;
envelope.set_item("ts", PyString::new(py, ×tamp))?;
envelope.set_item("seq", sequence)?;
envelope.set_item("p", payload_converted)?;
envelope.set_item("v", 1)?;
if raw_event_type != ws_event_type {
envelope.set_item("original_event_type", PyString::new(py, &raw_event_type))?;
}
if let Some(ms) = event_latency_ms {
envelope.set_item("latency_ms", ms)?;
}
let cid_from_meta = metadata
.as_ref()
.and_then(|m| dict_get_str(m, "correlation_id").ok().flatten());
let cid_from_event = dict_get_str(event, "correlation_id")?.filter(|s| !s.is_empty());
if let Some(cid) = cid_from_meta.or(cid_from_event) {
envelope.set_item("cid", PyString::new(py, &cid))?;
}
let pri_from_meta = metadata
.as_ref()
.and_then(|m| dict_get(m, "priority").ok().flatten());
let pri_from_event = dict_get(event, "pri")?.filter(|v| !v.is_none());
if let Some(pri) = pri_from_meta.or(pri_from_event) {
envelope.set_item("pri", pri)?;
}
{
let ver_from_event = dict_get(event, "version")?;
let ver_from_meta = metadata
.as_ref()
.and_then(|m| dict_get(m, "version").ok().flatten());
match ver_from_event.or(ver_from_meta) {
Some(v) => {
if let Ok(n) = v.extract::<i64>() {
envelope.set_item("event_version", n)?;
} else {
envelope.set_item("event_version", 1)?;
}
}
None => {
envelope.set_item("event_version", 1)?;
}
}
}
{
let trace_from_meta = metadata
.as_ref()
.and_then(|m| dict_get_str(m, "trace_id").ok().flatten());
let trace_from_event = dict_get_str(event, "trace_id")?.filter(|s| !s.is_empty());
if let Some(trace_id) = trace_from_meta.or(trace_from_event) {
envelope.set_item("trace_id", PyString::new(py, &trace_id))?;
}
}
Ok(envelope)
}