diff --git a/binaries/cli/src/template/rust/Cargo-template.toml b/binaries/cli/src/template/rust/Cargo-template.toml index d28ac4f8..bf138465 100644 --- a/binaries/cli/src/template/rust/Cargo-template.toml +++ b/binaries/cli/src/template/rust/Cargo-template.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["op_1", "op_2", "node_1"] +members = ["talker_1", "talker_2", "listener_1"] diff --git a/binaries/cli/src/template/rust/dataflow-template.yml b/binaries/cli/src/template/rust/dataflow-template.yml index 0f019fe4..2dd90356 100644 --- a/binaries/cli/src/template/rust/dataflow-template.yml +++ b/binaries/cli/src/template/rust/dataflow-template.yml @@ -1,26 +1,26 @@ nodes: - - id: op_1 - operator: - build: cargo build -p op_1 - shared-library: target/debug/op_1 + - id: talker_1 + custom: + build: cargo build -p talker_1 + source: target/debug/talker_1 inputs: tick: dora/timer/millis/100 outputs: - - some-output - - id: op_2 - operator: - build: cargo build -p op_2 - shared-library: target/debug/op_2 + - speech + - id: talker_2 + custom: + build: cargo build -p talker_2 + source: target/debug/talker_2 inputs: tick: dora/timer/secs/2 outputs: - - some-output + - speech - - id: custom-node_1 + - id: listener_1 custom: - build: cargo build -p node_1 - source: target/debug/node_1 + build: cargo build -p listener_1 + source: target/debug/listener_1 inputs: tick: dora/timer/secs/1 - input-1: op_1/some-output - input-2: op_2/some-output + input-1: talker_1/speech + input-2: talker_2/speech diff --git a/binaries/cli/src/template/rust/listener/Cargo-template.toml b/binaries/cli/src/template/rust/listener/Cargo-template.toml new file mode 100644 index 00000000..fa46f49a --- /dev/null +++ b/binaries/cli/src/template/rust/listener/Cargo-template.toml @@ -0,0 +1,9 @@ +[package] +name = "___name___" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +dora-node-api = {} diff --git a/binaries/cli/src/template/rust/listener/main-template.rs b/binaries/cli/src/template/rust/listener/main-template.rs new file mode 100644 index 00000000..cc378b88 --- /dev/null +++ b/binaries/cli/src/template/rust/listener/main-template.rs @@ -0,0 +1,25 @@ +use dora_node_api::{DoraNode, Event}; +use std::error::Error; + +fn main() -> Result<(), Box> { + let (mut node, mut events) = DoraNode::init_from_env()?; + + while let Some(event) = events.recv() { + match event { + Event::Input { + id, + metadata, + data, + } => match id.as_str() { + "speech" => { + let message: &str = (&data).try_into()?; + println!("I heard: {message} from {id}"); + } + other => eprintln!("Received input `{other}`"), + }, + _ => {} + } + } + + Ok(()) +} \ No newline at end of file diff --git a/binaries/cli/src/template/rust/mod.rs b/binaries/cli/src/template/rust/mod.rs index 6f72e5a2..ad12afe4 100644 --- a/binaries/cli/src/template/rust/mod.rs +++ b/binaries/cli/src/template/rust/mod.rs @@ -4,6 +4,10 @@ use std::{ path::{Path, PathBuf}, }; +const MAIN_RS: &str = include_str!("node/main-template.rs"); +const TALKER_RS: &str = include_str!("talker/main-template.rs"); +const LISTENER_RS: &str = include_str!("listener/main-template.rs"); + const VERSION: &str = env!("CARGO_PKG_VERSION"); pub fn create(args: crate::CommandNew, use_path_deps: bool) -> eyre::Result<()> { let crate::CommandNew { @@ -15,7 +19,7 @@ pub fn create(args: crate::CommandNew, use_path_deps: bool) -> eyre::Result<()> match kind { crate::Kind::Operator => { bail!("Operators are going to be depreciated, please don't use it") }, - crate::Kind::CustomNode => create_custom_node(name, path, use_path_deps), + crate::Kind::CustomNode => create_custom_node(name, path, use_path_deps, MAIN_RS), crate::Kind::Dataflow => create_dataflow(name, path, use_path_deps), } } @@ -49,9 +53,9 @@ fn create_dataflow( fs::write(&cargo_toml_path, cargo_toml) .with_context(|| format!("failed to write `{}`", cargo_toml_path.display()))?; - create_operator("op_1".into(), Some(root.join("op_1")), use_path_deps)?; - create_operator("op_2".into(), Some(root.join("op_2")), use_path_deps)?; - create_custom_node("node_1".into(), Some(root.join("node_1")), use_path_deps)?; + create_custom_node("talker_1".into(), Some(root.join("talker_1")), use_path_deps, TALKER_RS)?; + create_custom_node("talker_2".into(), Some(root.join("talker_2")), use_path_deps, TALKER_RS)?; + create_custom_node("listener_1".into(), Some(root.join("listener_1")), use_path_deps, LISTENER_RS)?; println!( "Created new Rust dataflow at `{name}` at {}", @@ -61,6 +65,8 @@ fn create_dataflow( Ok(()) } +#[deprecated(since = "0.3.4")] +#[allow(unused)] fn create_operator( name: String, path: Option, @@ -119,9 +125,9 @@ fn create_custom_node( name: String, path: Option, use_path_deps: bool, + template_scripts: &str, ) -> Result<(), eyre::ErrReport> { const CARGO_TOML: &str = include_str!("node/Cargo-template.toml"); - const MAIN_RS: &str = include_str!("node/main-template.rs"); if name.contains('/') { bail!("node name must not contain `/` separators"); @@ -151,7 +157,7 @@ fn create_custom_node( .with_context(|| format!("failed to write `{}`", cargo_toml_path.display()))?; let main_rs_path = src.join("main.rs"); - fs::write(&main_rs_path, MAIN_RS) + fs::write(&main_rs_path, template_scripts) .with_context(|| format!("failed to write `{}`", main_rs_path.display()))?; println!( diff --git a/binaries/cli/src/template/rust/talker/Cargo-template.toml b/binaries/cli/src/template/rust/talker/Cargo-template.toml new file mode 100644 index 00000000..fa46f49a --- /dev/null +++ b/binaries/cli/src/template/rust/talker/Cargo-template.toml @@ -0,0 +1,9 @@ +[package] +name = "___name___" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +dora-node-api = {} diff --git a/binaries/cli/src/template/rust/talker/main-template.rs b/binaries/cli/src/template/rust/talker/main-template.rs new file mode 100644 index 00000000..8af842ab --- /dev/null +++ b/binaries/cli/src/template/rust/talker/main-template.rs @@ -0,0 +1,24 @@ +use dora_node_api::{dora_core::config::DataId, DoraNode, Event, IntoArrow}; +use std::error::Error; + +fn main() -> Result<(), Box> { + let (mut node, mut events) = DoraNode::init_from_env()?; + + while let Some(event) = events.recv() { + match event { + Event::Input { + id, + metadata, + data: _, + } => match id.as_str() { + _other => { + node.send_output(DataId::from("speech".to_owned()), metadata.parameters, String::from("Hello World!").into_arrow())?; + eprintln!("Node received `{id}`"); + }, + }, + _ => {} + } + } + + Ok(()) +}