Basic example to try the harmony! macro

This commit is contained in:
Ian Letourneau
2025-11-10 11:50:22 -05:00
commit cc6da434c7
9 changed files with 377 additions and 0 deletions

9
harmony/Cargo.toml Normal file
View 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
View 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(())
}