comfy_builder_core/
node.rs1use 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(&self, kwargs: Kwargs<'a>) -> Result<Self::In, <Self::In as TryFrom<Kwargs<'a>>>::Error> {
17 Self::In::try_from(kwargs)
18 }
19
20 fn execute(&self, input: Self::In) -> Result<Self::Out, Self::Error>;
21}
22
23pub trait NodeFunctionProvider {
24 fn define_fn(python: Python) -> PyResult<Bound<PyCFunction>>;
25 fn execute_fn(python: Python) -> PyResult<Bound<PyCFunction>>;
26}
27
28pub trait In<'py>: TryFrom<Kwargs<'py>> {
29 fn blueprints(python: Python<'py>, io: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyList>>;
30 fn is_list() -> bool;
31}
32
33pub trait Out<'py> {
34 fn blueprints(python: Python<'py>, io: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyList>>;
35 fn to_schema(self, python: Python) -> PyResult<Bound<PyTuple>>;
36}
37
38pub struct Kwargs<'py>(pub Option<Bound<'py, PyDict>>);
39
40impl<'py> From<Option<Bound<'py, PyDict>>> for Kwargs<'py> {
41 fn from(value: Option<Bound<'py, PyDict>>) -> Self {
42 Kwargs(value)
43 }
44}
45
46impl<'a> Deref for Kwargs<'a> {
47 type Target = Option<Bound<'a, PyDict>>;
48
49 fn deref(&self) -> &Self::Target {
50 &self.0
51 }
52}