use fidius_core::Value;
use fidius_python::PythonPluginHandle;
use crate::error::CallError;
use crate::executor::{PluginExecutor, ValueExecutor};
use crate::types::PluginInfo;
pub struct Pyo3Executor {
py: PythonPluginHandle,
info: PluginInfo,
}
impl Pyo3Executor {
pub fn new(py: PythonPluginHandle, info: PluginInfo) -> Self {
Self { py, info }
}
}
impl PluginExecutor for Pyo3Executor {
fn info(&self) -> &PluginInfo {
&self.info
}
fn method_count(&self) -> u32 {
self.py.method_count() as u32
}
fn call_raw(&self, method: usize, input: &[u8]) -> Result<Vec<u8>, CallError> {
self.py.call_raw(method, input).map_err(CallError::from)
}
}
impl ValueExecutor for Pyo3Executor {
fn call(&self, method: usize, args: Value) -> Result<Value, CallError> {
let json =
serde_json::to_vec(&args).map_err(|e| CallError::Serialization(e.to_string()))?;
let out = self
.py
.call_typed_json(method, &json)
.map_err(CallError::from)?;
serde_json::from_slice(&out).map_err(|e| CallError::Deserialization(e.to_string()))
}
}