chore: Move ADR helper files into folders with their corresponding ADR number
This commit is contained in:
155
adr/003-abstractions/topology/src/main_v1.rs
Normal file
155
adr/003-abstractions/topology/src/main_v1.rs
Normal file
@@ -0,0 +1,155 @@
|
||||
mod main_right;
|
||||
mod main_claude;
|
||||
// Capability Traits
|
||||
|
||||
trait Capability {}
|
||||
|
||||
trait LinuxOperations: Capability {
|
||||
fn execute_command(&self, command: &str) -> Result<String, String>;
|
||||
}
|
||||
|
||||
trait KubernetesOperations: Capability {
|
||||
fn create_resource(&self, resource: &str) -> Result<String, String>;
|
||||
fn delete_resource(&self, resource: &str) -> Result<String, String>;
|
||||
}
|
||||
|
||||
// Topology Implementations
|
||||
|
||||
struct LinuxHostTopology;
|
||||
|
||||
impl LinuxOperations for LinuxHostTopology {
|
||||
fn execute_command(&self, command: &str) -> Result<String, String> {
|
||||
// Implementation for executing commands on a Linux host
|
||||
Ok(format!("Executed command: {}", command))
|
||||
}
|
||||
}
|
||||
|
||||
impl Capability for LinuxHostTopology {}
|
||||
|
||||
struct K3DTopology;
|
||||
|
||||
impl KubernetesOperations for K3DTopology {
|
||||
fn create_resource(&self, resource: &str) -> Result<String, String> {
|
||||
// Implementation for creating Kubernetes resources in K3D
|
||||
Ok(format!("Created resource: {}", resource))
|
||||
}
|
||||
|
||||
fn delete_resource(&self, resource: &str) -> Result<String, String> {
|
||||
// Implementation for deleting Kubernetes resources in K3D
|
||||
Ok(format!("Deleted resource: {}", resource))
|
||||
}
|
||||
}
|
||||
|
||||
impl Capability for K3DTopology {}
|
||||
|
||||
// Score Implementations
|
||||
|
||||
struct K8sResourceScore {
|
||||
resource: String,
|
||||
}
|
||||
|
||||
impl<T> Score<T> for K8sResourceScore
|
||||
where
|
||||
T: KubernetesOperations,
|
||||
{
|
||||
fn execute(&self, topology: &T) -> Result<String, String> {
|
||||
topology.create_resource(&self.resource)
|
||||
}
|
||||
}
|
||||
|
||||
struct CommandScore {
|
||||
command: String,
|
||||
}
|
||||
|
||||
impl<T> Score<T> for CommandScore
|
||||
where
|
||||
T: LinuxOperations + 'static,
|
||||
{
|
||||
fn execute(&self, topology: &T) -> Result<String, String> {
|
||||
topology.execute_command(&self.command)
|
||||
}
|
||||
}
|
||||
|
||||
// Score Trait
|
||||
|
||||
trait Score<T>
|
||||
where
|
||||
T: Capability + 'static,
|
||||
{
|
||||
fn execute(&self, topology: &T) -> Result<String, String>;
|
||||
}
|
||||
|
||||
// Maestro Implementation
|
||||
|
||||
struct Maestro {
|
||||
scores: Vec<Box<dyn Score<Box<dyn Capability>>>>,
|
||||
}
|
||||
|
||||
impl Maestro {
|
||||
fn new() -> Self {
|
||||
Maestro { scores: Vec::new() }
|
||||
}
|
||||
|
||||
fn register_score<T>(&mut self, score: Box<T>)
|
||||
where
|
||||
T: Score<Box<dyn Capability>> + 'static,
|
||||
{
|
||||
self.scores.push(Box::new(score));
|
||||
}
|
||||
|
||||
fn execute_scores<T>(&self, topology: &T) -> Result<Vec<String>, String>
|
||||
where
|
||||
T: Capability + 'static,
|
||||
{
|
||||
let mut results = Vec::new();
|
||||
for score in &self.scores {
|
||||
if let Some(score) = score.as_any().downcast_ref::<Box<dyn Score<T>>>() {
|
||||
results.push(score.execute(topology)?);
|
||||
}
|
||||
}
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper trait for downcasting
|
||||
|
||||
trait AsAny {
|
||||
fn as_any(&self) -> &dyn std::any::Any;
|
||||
}
|
||||
|
||||
impl<T: 'static> AsAny for T {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// Main Function
|
||||
|
||||
fn main() {
|
||||
let mut maestro = Maestro::new();
|
||||
|
||||
let k8s_score = K8sResourceScore {
|
||||
resource: "deployment.yaml".to_string(),
|
||||
};
|
||||
maestro.register_score(k8s_score);
|
||||
|
||||
let command_score = CommandScore {
|
||||
command: "ls -l".to_string(),
|
||||
};
|
||||
maestro.register_score(command_score);
|
||||
|
||||
let linux_topology = LinuxHostTopology;
|
||||
let k3d_topology = K3DTopology;
|
||||
|
||||
let linux_results = maestro.execute_scores(&linux_topology).unwrap();
|
||||
println!("Linux Topology Results:");
|
||||
for result in linux_results {
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
let k3d_results = maestro.execute_scores(&k3d_topology).unwrap();
|
||||
println!("K3D Topology Results:");
|
||||
for result in k3d_results {
|
||||
println!("{}", result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user