1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Import necessary items from the pyo3 crate
use pyo3::{exceptions::PyTypeError, prelude::*};
use serde::Serialize;

// Define an enumeration for different event types
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum EventType {
	Graph,
	Vector,
	QueryResult,
	Success,
	Failure,
}

// Implement conversion from Python object to EventType
impl<'a> FromPyObject<'a> for EventType {
	fn extract(ob: &'a PyAny) -> PyResult<Self> {
		// Try to extract a string from the Python object
		if let Ok(event_type) = ob.extract::<&str>() {
			// Match the string to determine the EventType
			match event_type {
				"Graph" => Ok(EventType::Graph),
				"Vector" => Ok(EventType::Vector),
				"QueryResult" => Ok(EventType::QueryResult),
				// If the string does not match any known EventType, return an error
				_ => Err(PyErr::new::<PyTypeError, _>("Invalid event type")),
			}
		} else {
			// If extraction fails, return an error
			Err(PyErr::new::<PyTypeError, _>("Invalid event type"))
		}
	}
}

// Define a structure to represent the state of an event
#[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>,
}

// Implement conversion from Python object to EventState
impl<'a> FromPyObject<'a> for EventState {
	fn extract(ob: &'a PyAny) -> PyResult<Self> {
		// Extract values for event_type, timestamp, and payload from the Python object
		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,
		};
		// Create and return an EventState instance
		Ok(EventState { event_type, timestamp, payload, file, doc_source, image_id })
	}
}