use pyo3::{exceptions::PyTypeError, prelude::*};
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum EventType {
Graph,
Vector,
QueryResult,
Success,
Failure,
}
impl<'a> FromPyObject<'a> for EventType {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
if let Ok(event_type) = ob.extract::<&str>() {
match event_type {
"Graph" => Ok(EventType::Graph),
"Vector" => Ok(EventType::Vector),
"QueryResult" => Ok(EventType::QueryResult),
_ => Err(PyErr::new::<PyTypeError, _>("Invalid event type")),
}
} else {
Err(PyErr::new::<PyTypeError, _>("Invalid event type"))
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct EventState {
pub event_type: EventType,
pub timestamp: f64,
pub payload: String,
pub file: String,
pub doc_source: String,
pub image_id: Option<String>,
}
impl<'a> FromPyObject<'a> for EventState {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let event_type = ob.get_item("event_type")?.extract()?;
let timestamp = ob.get_item("timestamp")?.extract()?;
let payload = ob.get_item("payload")?.extract()?;
let file = ob.get_item("file")?.extract()?;
let doc_source = ob.get_item("doc_source")?.extract()?;
let image_id_res = ob.get_item("image_id");
let image_id = match image_id_res {
Ok(id) => Some(id.extract()?),
Err(_err) => None,
};
Ok(EventState { event_type, timestamp, payload, file, doc_source, image_id })
}
}