Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add callable workers trait #1

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# So, you want to update the Rust toolchain...

# Besides making sure the code compiles (duh) and the tests/clippy pass (obviously)
# Please note that you'll have to update all the toolchain references in the codebase!
# AND build new docker images for:
# - rust_tari-build-with-deps
# - rust-ndk
# AND update the action-buildlibs dockerfile.


# Hours spent updating the Rust Toolchain = 4
[toolchain]
channel = "nightly"
7 changes: 4 additions & 3 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use serde_json::Value;
use std::collections::HashMap;
use crate::node::*;
use crate::workers::Workers;
use crate::workers::CallableWorkers;

#[allow(dead_code)]
pub struct Engine {
id: String,
workers: Workers,
workers: Box<dyn CallableWorkers>,
}

#[allow(dead_code)]
Expand All @@ -18,10 +19,10 @@ pub enum Error {

#[allow(dead_code)]
impl <'a, 'b> Engine {
pub fn new(id: &str, workers: Workers) -> Engine {
pub fn new(id: &str, workers: Box<dyn CallableWorkers>) -> Engine {
Engine {
id: id.to_string(),
workers: workers,
workers
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::rc::Rc;
use std::any::{Any, TypeId};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Deserializer};
use serde_json::Value;
use crate::target::{Inputs, Outputs};
use std::collections::HashMap;
use serde::de::DeserializeOwned;

#[derive(Debug)]
pub struct IOData {
Expand Down Expand Up @@ -59,11 +60,16 @@ impl Node {
}
}
}


// TODO: Remove 'static from above method
pub fn get_field_t<T:Default + Clone + DeserializeOwned + 'static>(&self, field: &str, inputs: &InputData) -> Result<T> {
self.get_field(field, inputs, T::default(), Box::new(|t| t.clone()), Box::new(|v| serde_json::from_value(v.clone()).unwrap()), None)
}

pub fn get_number_field_or(&self, field: &str, inputs: &InputData, default: Option<i64>) -> Result<i64> {
self.get_field(field, inputs, i64::MIN, Box::new(|r| *r), Box::new(|v| v.as_i64().unwrap()), default)
}

pub fn get_float_number_field_or(&self, field: &str, inputs: &InputData, default: Option<f64>) -> Result<f64> {
self.get_field(field, inputs, f64::MIN, Box::new(|r| *r), Box::new(|v| v.as_f64().unwrap()), default)
}
Expand Down Expand Up @@ -108,16 +114,16 @@ impl Node {
pub fn get_string_field(&self, field: &str, inputs: &InputData) -> Result<String> {
self.get_string_field_or(field, inputs, None)
}

pub fn get_number_field(&self, field: &str, inputs: &InputData) -> Result<i64> {
self.get_number_field_or(field, inputs, None)
}

pub fn get_float_number_field(&self, field: &str, inputs: &InputData) -> Result<f64> {
self.get_float_number_field_or(field, inputs, None)
}

pub fn get_json_field(&self, field: &str, inputs: &InputData) -> Result<Value> {
self.get_json_field_or(field, inputs, None)
}
}
}
13 changes: 10 additions & 3 deletions src/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct Workers {

#[allow(dead_code)]
impl Workers {

pub fn new() -> Workers {
Workers { map: HashMap::new() }
}
Expand All @@ -16,7 +15,15 @@ impl Workers {
self.map.insert(name.to_string(), worker);
}

pub fn call(&self, name: &str, node: Node, input: InputData) -> Option<OutputData> {

}

pub trait CallableWorkers {
fn call(&self, name: &str, node: Node, input: InputData) -> Option<OutputData>;
}

impl CallableWorkers for Workers{
fn call(&self, name: &str, node: Node, input: InputData) -> Option<OutputData> {
self.map.get(name).map(|f| f(node, input))
}
}
}