Basic example to try the harmony! macro
This commit is contained in:
14
examples/hello_world/Cargo.toml
Normal file
14
examples/hello_world/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "hello_world"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[[example]]
|
||||
name = "hello_world"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
async-trait = "0.1.89"
|
||||
harmony = { version = "0.1.0", path = "../../harmony" }
|
||||
harmony_macros = { version = "0.1.0", path = "../../harmony_macros" }
|
||||
tokio = "1.48.0"
|
||||
75
examples/hello_world/src/main.rs
Normal file
75
examples/hello_world/src/main.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use async_trait::async_trait;
|
||||
use harmony::{Interpret, Inventory, Score, Topology};
|
||||
use harmony_macros::harmony;
|
||||
|
||||
struct MyTopology {}
|
||||
|
||||
#[async_trait]
|
||||
impl Topology for MyTopology {
|
||||
fn name(&self) -> &str {
|
||||
"MyTopology"
|
||||
}
|
||||
|
||||
async fn ensure_ready(&self) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MyScore {}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology> Score<T> for MyScore {
|
||||
fn name(&self) -> String {
|
||||
"MyScore".to_string()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
|
||||
Box::new(MyInterpret {})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MyInterpret {}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology> Interpret<T> for MyInterpret {
|
||||
async fn execute(&self, _inventory: &Inventory, _topology: &T) -> Result<(), String> {
|
||||
println!("MyInterpret is executing");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct AnotherScore {}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology> Score<T> for AnotherScore {
|
||||
fn name(&self) -> String {
|
||||
"AnotherScore".to_string()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
|
||||
Box::new(AnotherInterpret {})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AnotherInterpret {}
|
||||
|
||||
#[async_trait]
|
||||
impl<T: Topology> Interpret<T> for AnotherInterpret {
|
||||
async fn execute(&self, _inventory: &Inventory, _topology: &T) -> Result<(), String> {
|
||||
println!("AnotherInterpret is executing");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
harmony! {
|
||||
inventory: Inventory { location: "hello".to_string()},
|
||||
topology: MyTopology {}
|
||||
scores: [
|
||||
MyScore {},
|
||||
AnotherScore {}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user