feat: Introduce Topology Trait for Compile-Time Safe Score Binding

Introduce the `Topology` trait to ensure that `Maestro` can compile-time safely bind compatible `Scores` and `Topologies`. This refactoring includes updating `HarmonyTuiEvent`, `ScoreListWidget`, and related structures to work with generic `Topology` types, enhancing type safety and modularity.
This commit is contained in:
2025-04-02 15:51:28 -04:00
parent f7dc15cbf0
commit fc718f11cf
9 changed files with 87 additions and 64 deletions

View File

@@ -56,10 +56,7 @@ impl<T: Topology> Maestro<T> {
score_mut.append(&mut scores);
}
pub async fn interpret<S>(&self, score: S) -> Result<Outcome, InterpretError>
where
S: Score<T>,
{
pub async fn interpret(&self, score: Box<dyn Score<T>>) -> Result<Outcome, InterpretError> {
info!("Running score {score:?}");
let interpret = score.create_interpret();
info!("Launching interpret {interpret:?}");

View File

@@ -1,6 +1,21 @@
use super::{interpret::Interpret, topology::Topology};
pub trait Score<T: Topology>: std::fmt::Debug + Send + Sync {
pub trait Score<T: Topology>: std::fmt::Debug + Send + Sync + CloneBoxScore<T> {
fn create_interpret(&self) -> Box<dyn Interpret<T>>;
fn name(&self) -> String;
}
pub trait CloneBoxScore<T: Topology> {
fn clone_box(&self) -> Box<dyn Score<T>>;
}
impl<S, T> CloneBoxScore<T> for S
where
T: Topology,
S: Score<T> + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn Score<T>> {
Box::new(self.clone())
}
}

View File

@@ -99,7 +99,10 @@ impl<T: Topology + DnsServer> Interpret<T> for DnsInterpret {
inventory: &Inventory,
topology: &T,
) -> Result<Outcome, InterpretError> {
info!("Executing {} on inventory {inventory:?}", <DnsInterpret as Interpret<T>>::get_name(self));
info!(
"Executing {} on inventory {inventory:?}",
<DnsInterpret as Interpret<T>>::get_name(self)
);
self.serve_dhcp_entries(inventory, topology).await?;
self.ensure_hosts_registered(topology).await?;