This commit is contained in:
Rendo 2025-11-08 13:27:02 +05:00
commit 6f0c9cf943

53
src/node/handler.rs Normal file
View file

@ -0,0 +1,53 @@
pub trait NodeHandler {
fn run(&self, inputs: Vec<f64>) -> f64;
}
pub struct NodeFunction {
function: fn(Vec<f64>) -> f64,
}
impl NodeFunction {
pub fn new(function: fn(Vec<f64>) -> f64) -> NodeFunction {
NodeFunction { function }
}
}
impl NodeHandler for NodeFunction {
fn run(&self, inputs: Vec<f64>) -> f64 {
(self.function)(inputs)
}
}
pub struct NodeNumber {
number: f64,
}
impl NodeNumber {
pub fn new(number: Option<f64>) -> NodeNumber {
NodeNumber {
number: number.unwrap_or(0f64),
}
}
}
impl NodeHandler for NodeNumber {
fn run(&self, inputs: Vec<f64>) -> f64 {
self.number
}
}
pub struct NodeVariable {
getter: fn() -> f64,
}
impl NodeVariable {
pub fn new(getter: fn() -> f64) -> NodeVariable {
NodeVariable { getter }
}
}
impl NodeHandler for NodeVariable {
fn run(&self, inputs: Vec<f64>) -> f64 {
(self.getter)()
}
}