Browse Source

add rust template

tags/v0.3.5-rc0
XxChang 1 year ago
parent
commit
5ea31fc69b
7 changed files with 95 additions and 22 deletions
  1. +1
    -1
      binaries/cli/src/template/rust/Cargo-template.toml
  2. +15
    -15
      binaries/cli/src/template/rust/dataflow-template.yml
  3. +9
    -0
      binaries/cli/src/template/rust/listener/Cargo-template.toml
  4. +25
    -0
      binaries/cli/src/template/rust/listener/main-template.rs
  5. +12
    -6
      binaries/cli/src/template/rust/mod.rs
  6. +9
    -0
      binaries/cli/src/template/rust/talker/Cargo-template.toml
  7. +24
    -0
      binaries/cli/src/template/rust/talker/main-template.rs

+ 1
- 1
binaries/cli/src/template/rust/Cargo-template.toml View File

@@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["op_1", "op_2", "node_1"]
members = ["talker_1", "talker_2", "listener_1"]

+ 15
- 15
binaries/cli/src/template/rust/dataflow-template.yml View File

@@ -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

+ 9
- 0
binaries/cli/src/template/rust/listener/Cargo-template.toml View File

@@ -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 = {}

+ 25
- 0
binaries/cli/src/template/rust/listener/main-template.rs View File

@@ -0,0 +1,25 @@
use dora_node_api::{DoraNode, Event};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
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(())
}

+ 12
- 6
binaries/cli/src/template/rust/mod.rs View File

@@ -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<PathBuf>,
@@ -119,9 +125,9 @@ fn create_custom_node(
name: String,
path: Option<PathBuf>,
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!(


+ 9
- 0
binaries/cli/src/template/rust/talker/Cargo-template.toml View File

@@ -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 = {}

+ 24
- 0
binaries/cli/src/template/rust/talker/main-template.rs View File

@@ -0,0 +1,24 @@
use dora_node_api::{dora_core::config::DataId, DoraNode, Event, IntoArrow};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
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(())
}

Loading…
Cancel
Save