Basic example to try the harmony! macro
This commit is contained in:
9
harmony/Cargo.toml
Normal file
9
harmony/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "harmony"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
harmony_macros = { version = "0.1.0", path = "../harmony_macros" }
|
||||
async-trait = "0.1.89"
|
||||
tokio = { version = "1.48.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
43
harmony/src/lib.rs
Normal file
43
harmony/src/lib.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Topology: Send + Sync {
|
||||
fn name(&self) -> &str;
|
||||
async fn ensure_ready(&self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Inventory {
|
||||
pub location: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Score<T: Topology>: Send + Sync {
|
||||
async fn interpret(&self, inventory: &Inventory, topology: &T) -> Result<(), String> {
|
||||
let interpret = self.create_interpret();
|
||||
interpret.execute(inventory, topology).await
|
||||
}
|
||||
|
||||
fn name(&self) -> String;
|
||||
|
||||
#[doc(hidden)]
|
||||
fn create_interpret(&self) -> Box<dyn Interpret<T>>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Interpret<T>: std::fmt::Debug + Send {
|
||||
async fn execute(&self, inventory: &Inventory, topology: &T) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub async fn run_cli<T: Topology>(
|
||||
inventory: Inventory,
|
||||
topology: T,
|
||||
scores: Vec<Box<dyn Score<T>>>,
|
||||
) -> Result<(), String> {
|
||||
for score in scores {
|
||||
let interpret = score.create_interpret();
|
||||
interpret.execute(&inventory, &topology).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user