comfy_builder_core/
node.rs

1use pyo3::types::{PyCFunction, PyDict, PyList, PyTuple};
2use pyo3::{Bound, PyAny, PyResult, Python};
3use std::error::Error;
4use std::ops::Deref;
5
6pub trait Node<'a>: Default {
7    type In: In<'a>;
8    type Out: Out<'a>;
9
10    type Error: Into<Box<dyn Error + Send + Sync>> + 'static;
11
12    fn new() -> Self {
13        Default::default()
14    }
15
16    fn initialize_inputs(
17        &self,
18        kwargs: Kwargs<'a>,
19    ) -> Result<Self::In, <Self::In as TryFrom<Kwargs<'a>>>::Error> {
20        Self::In::try_from(kwargs)
21    }
22
23    fn execute(&self, input: Self::In) -> Result<Self::Out, Self::Error>;
24}
25
26pub trait NodeFunctionProvider {
27    fn define_fn(python: Python) -> PyResult<Bound<PyCFunction>>;
28    fn execute_fn(python: Python) -> PyResult<Bound<PyCFunction>>;
29}
30
31pub trait In<'py>: TryFrom<Kwargs<'py>> {
32    fn blueprints(python: Python<'py>, io: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyList>>;
33    fn is_list() -> bool;
34}
35
36pub trait Out<'py> {
37    fn blueprints(python: Python<'py>, io: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyList>>;
38    fn to_schema(self, python: Python) -> PyResult<Bound<PyTuple>>;
39}
40
41pub struct Kwargs<'py>(pub Option<Bound<'py, PyDict>>);
42
43impl<'py> From<Option<Bound<'py, PyDict>>> for Kwargs<'py> {
44    fn from(value: Option<Bound<'py, PyDict>>) -> Self {
45        Kwargs(value)
46    }
47}
48
49impl<'a> Deref for Kwargs<'a> {
50    type Target = Option<Bound<'a, PyDict>>;
51
52    fn deref(&self) -> &Self::Target {
53        &self.0
54    }
55}