[feat](trx-backend): add RegistrationContext for explicit initialization
Create explicit RegistrationContext type for backend factory registration instead of relying solely on global mutable state. New RegistrationContext: - register_backend(name, factory) - register a backend - is_backend_registered(name) - check if registered - registered_backends() -> Vec<String> - list all backends - build_rig(name, access) -> DynResult - instantiate a rig Maintains global API for plugin compatibility, delegates to context. Paves way for threading context through bootstrap in Phase 3B. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> Signed-off-by: Stanislaw Grams <stanislawgrams@gmail.com>
This commit is contained in:
@@ -22,8 +22,58 @@ pub enum RigAccess {
|
||||
Tcp { addr: String },
|
||||
}
|
||||
|
||||
type BackendFactory = fn(RigAccess) -> DynResult<Box<dyn RigCat>>;
|
||||
pub type BackendFactory = fn(RigAccess) -> DynResult<Box<dyn RigCat>>;
|
||||
|
||||
/// Context for registering and instantiating rig backends.
|
||||
pub struct RegistrationContext {
|
||||
factories: HashMap<String, BackendFactory>,
|
||||
}
|
||||
|
||||
impl RegistrationContext {
|
||||
/// Create a new empty registration context.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
factories: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a backend factory under a stable name (e.g. "ft817").
|
||||
pub fn register_backend(&mut self, name: &str, factory: BackendFactory) {
|
||||
let key = normalize_name(name);
|
||||
self.factories.insert(key, factory);
|
||||
}
|
||||
|
||||
/// Check whether a backend name is registered.
|
||||
pub fn is_backend_registered(&self, name: &str) -> bool {
|
||||
let key = normalize_name(name);
|
||||
self.factories.contains_key(&key)
|
||||
}
|
||||
|
||||
/// List registered backend names.
|
||||
pub fn registered_backends(&self) -> Vec<String> {
|
||||
let mut names: Vec<String> = self.factories.keys().cloned().collect();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
|
||||
/// Instantiate a rig backend based on the selected name and access method.
|
||||
pub fn build_rig(&self, name: &str, access: RigAccess) -> DynResult<Box<dyn RigCat>> {
|
||||
let key = normalize_name(name);
|
||||
let factory = self
|
||||
.factories
|
||||
.get(&key)
|
||||
.ok_or_else(|| format!("Unknown rig backend: {}", name))?;
|
||||
factory(access)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RegistrationContext {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy global registry for plugin compatibility
|
||||
struct BackendRegistry {
|
||||
factories: HashMap<String, BackendFactory>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user