|
| 1 | +use pyo3::{prelude::*, types::PyBytes}; |
| 2 | +use streamson_lib::{error, handler, path::Path}; |
| 3 | + |
| 4 | +/// Streamson handler which performs data conversion |
| 5 | +#[pyclass] |
| 6 | +#[derive(Clone)] |
| 7 | +pub struct PythonConverter { |
| 8 | + callable: PyObject, |
| 9 | + use_path: bool, |
| 10 | +} |
| 11 | + |
| 12 | +#[pymethods] |
| 13 | +impl PythonConverter { |
| 14 | + /// Create instance of PythonConverter |
| 15 | + /// |
| 16 | + /// # Arguments |
| 17 | + /// * `callable` - python callable (3 arguments) |
| 18 | + #[new] |
| 19 | + pub fn new(callable: PyObject, use_path: bool) -> Self { |
| 20 | + Self { callable, use_path } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl handler::Handler for PythonConverter { |
| 25 | + fn handle( |
| 26 | + &mut self, |
| 27 | + path: &Path, |
| 28 | + matcher_idx: usize, |
| 29 | + data: Option<&[u8]>, |
| 30 | + ) -> Result<Option<Vec<u8>>, error::Handler> { |
| 31 | + let gil = Python::acquire_gil(); |
| 32 | + let res = self |
| 33 | + .callable |
| 34 | + .call1( |
| 35 | + gil.python(), |
| 36 | + ( |
| 37 | + if self.use_path { |
| 38 | + Some(path.to_string()) |
| 39 | + } else { |
| 40 | + None |
| 41 | + }, |
| 42 | + matcher_idx, |
| 43 | + data.unwrap().to_vec(), |
| 44 | + ), |
| 45 | + ) |
| 46 | + .map_err(|_| error::Handler::new("Failed to call handler function"))?; |
| 47 | + let bytes = res.cast_as::<PyBytes>(gil.python()).unwrap(); |
| 48 | + Ok(FromPyObject::extract(bytes) |
| 49 | + .map_err(|_| error::Handler::new("Function does not return bytes."))?) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Streamson handler which calls python callable |
| 54 | +#[pyclass] |
| 55 | +#[derive(Clone)] |
| 56 | +pub struct PythonHandler { |
| 57 | + callable: PyObject, |
| 58 | + require_path: bool, |
| 59 | +} |
| 60 | + |
| 61 | +#[pymethods] |
| 62 | +impl PythonHandler { |
| 63 | + /// Create instance of PythonHandler |
| 64 | + /// |
| 65 | + /// # Arguments |
| 66 | + /// * `callable` - python callable (3 arguments) |
| 67 | + /// * `require_path` - should path be passed to handler |
| 68 | + #[new] |
| 69 | + pub fn new(callable: PyObject, require_path: bool) -> Self { |
| 70 | + Self { |
| 71 | + callable, |
| 72 | + require_path, |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl handler::Handler for PythonHandler { |
| 78 | + /// Call python function as a part of rust handler |
| 79 | + /// |
| 80 | + /// # Arguments |
| 81 | + /// * `path` - matched path |
| 82 | + /// * `matcher_idx` - index of triggered matcher |
| 83 | + /// * `data` - matched data |
| 84 | + fn handle( |
| 85 | + &mut self, |
| 86 | + path: &Path, |
| 87 | + matcher_idx: usize, |
| 88 | + data: Option<&[u8]>, |
| 89 | + ) -> Result<Option<Vec<u8>>, error::Handler> { |
| 90 | + let gil = Python::acquire_gil(); |
| 91 | + self.callable |
| 92 | + .call1( |
| 93 | + gil.python(), |
| 94 | + ( |
| 95 | + if self.require_path { |
| 96 | + Some(path.to_string()) |
| 97 | + } else { |
| 98 | + None |
| 99 | + }, |
| 100 | + matcher_idx, |
| 101 | + data, |
| 102 | + ), |
| 103 | + ) |
| 104 | + .map_err(|_| error::Handler::new("Calling python failed"))?; |
| 105 | + Ok(None) |
| 106 | + } |
| 107 | +} |
0 commit comments