From ef6d45b8784cb32adfbfe2a39c1e3f894db87f0b Mon Sep 17 00:00:00 2001 From: Stanislaw Grams Date: Thu, 12 Feb 2026 20:47:19 +0100 Subject: [PATCH] [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 - 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 Signed-off-by: Stanislaw Grams --- src/trx-server/trx-backend/src/lib.rs | 52 ++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/trx-server/trx-backend/src/lib.rs b/src/trx-server/trx-backend/src/lib.rs index 1f71db8..5cb52a7 100644 --- a/src/trx-server/trx-backend/src/lib.rs +++ b/src/trx-server/trx-backend/src/lib.rs @@ -22,8 +22,58 @@ pub enum RigAccess { Tcp { addr: String }, } -type BackendFactory = fn(RigAccess) -> DynResult>; +pub type BackendFactory = fn(RigAccess) -> DynResult>; +/// Context for registering and instantiating rig backends. +pub struct RegistrationContext { + factories: HashMap, +} + +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 { + let mut names: Vec = 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> { + 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, }