use crate::context::{FixContext, GenericFixContext};
use std::collections::HashMap;
pub struct FixContextRegistry {
contexts: HashMap<String, Box<dyn FixContext>>,
fallback: GenericFixContext,
}
impl FixContextRegistry {
pub fn new() -> Self {
Self {
contexts: HashMap::new(),
fallback: GenericFixContext,
}
}
pub fn register(&mut self, ctx: Box<dyn FixContext>) {
let name = ctx.ruleset_name().to_string();
self.contexts.insert(name, ctx);
}
pub fn get(&self, ruleset_name: &str) -> &dyn FixContext {
self.contexts
.get(ruleset_name)
.map(|b| b.as_ref())
.unwrap_or(&self.fallback)
}
pub fn has(&self, ruleset_name: &str) -> bool {
self.contexts.contains_key(ruleset_name)
}
}
impl Default for FixContextRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestContext {
name: String,
}
impl FixContext for TestContext {
fn ruleset_name(&self) -> &str {
&self.name
}
fn migration_description(&self) -> &str {
"test migration"
}
fn llm_constraints(&self) -> &[String] {
&[]
}
}
#[test]
fn test_registry_fallback() {
let registry = FixContextRegistry::new();
let ctx = registry.get("nonexistent");
assert_eq!(ctx.migration_description(), "code migration");
}
#[test]
fn test_registry_register_and_lookup() {
let mut registry = FixContextRegistry::new();
registry.register(Box::new(TestContext {
name: "test-rules".to_string(),
}));
let ctx = registry.get("test-rules");
assert_eq!(ctx.migration_description(), "test migration");
assert!(registry.has("test-rules"));
assert!(!registry.has("other-rules"));
}
#[test]
fn test_registry_unknown_returns_fallback() {
let mut registry = FixContextRegistry::new();
registry.register(Box::new(TestContext {
name: "test-rules".to_string(),
}));
let ctx = registry.get("unknown-rules");
assert_eq!(ctx.migration_description(), "code migration");
}
}