From d7f5dc83f8a71b4240b9bdaf1900684e80ac6412 Mon Sep 17 00:00:00 2001 From: Stanislaw Grams Date: Thu, 12 Feb 2026 20:56:11 +0100 Subject: [PATCH] [refactor](trx-backend): implement plugin compatibility adapter shim Replace legacy global BackendRegistry with bootstrap context adapter that maintains backward compatibility while delegating to explicit context. Changes: - Create BOOTSTRAP_CONTEXT: OnceLock>> - register_backend(): delegates to bootstrap context - is_backend_registered(): reads from bootstrap context - registered_backends(): reads from bootstrap context - build_rig(): reads from bootstrap context Result: Plugins continue calling global functions, but all operations now route through the bootstrap context instead of a separate global registry. This completes the de-globalization while maintaining full backward compatibility with existing plugins. Co-Authored-By: Claude Haiku 4.5 Signed-off-by: Stanislaw Grams --- src/trx-server/trx-backend/src/lib.rs | 55 +++++++++------------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/src/trx-server/trx-backend/src/lib.rs b/src/trx-server/trx-backend/src/lib.rs index 90508ce..2931871 100644 --- a/src/trx-server/trx-backend/src/lib.rs +++ b/src/trx-server/trx-backend/src/lib.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: BSD-2-Clause use std::collections::HashMap; -use std::sync::{Mutex, OnceLock}; +use std::sync::{Arc, Mutex, OnceLock}; use trx_core::rig::RigCat; use trx_core::DynResult; @@ -73,24 +73,6 @@ impl Default for RegistrationContext { } } -// Legacy global registry for plugin compatibility -struct BackendRegistry { - factories: HashMap, -} - -impl BackendRegistry { - fn new() -> Self { - Self { - factories: HashMap::new(), - } - } -} - -fn registry() -> &'static Mutex { - static REGISTRY: OnceLock> = OnceLock::new(); - REGISTRY.get_or_init(|| Mutex::new(BackendRegistry::new())) -} - fn normalize_name(name: &str) -> String { name.to_ascii_lowercase() .chars() @@ -98,11 +80,17 @@ fn normalize_name(name: &str) -> String { .collect() } +/// Phase 3D: Plugin compatibility adapter - delegates to bootstrap context. +fn bootstrap_context() -> &'static Arc> { + static BOOTSTRAP_CONTEXT: OnceLock>> = OnceLock::new(); + BOOTSTRAP_CONTEXT.get_or_init(|| Arc::new(Mutex::new(RegistrationContext::new()))) +} + /// Register a backend factory under a stable name (e.g. "ft817"). +/// Plugin compatibility: delegates to bootstrap context. pub fn register_backend(name: &str, factory: BackendFactory) { - let key = normalize_name(name); - let mut reg = registry().lock().expect("backend registry mutex poisoned"); - reg.factories.insert(key, factory); + let mut ctx = bootstrap_context().lock().expect("backend context mutex poisoned"); + ctx.register_backend(name, factory); } /// Register all built-in backends enabled by features on a context. @@ -128,29 +116,24 @@ fn dummy_factory(_access: RigAccess) -> DynResult> { } /// Check whether a backend name is registered. +/// Plugin compatibility: reads from bootstrap context. pub fn is_backend_registered(name: &str) -> bool { - let key = normalize_name(name); - let reg = registry().lock().expect("backend registry mutex poisoned"); - reg.factories.contains_key(&key) + let ctx = bootstrap_context().lock().expect("backend context mutex poisoned"); + ctx.is_backend_registered(name) } /// List registered backend names. +/// Plugin compatibility: reads from bootstrap context. pub fn registered_backends() -> Vec { - let reg = registry().lock().expect("backend registry mutex poisoned"); - let mut names: Vec = reg.factories.keys().cloned().collect(); - names.sort(); - names + let ctx = bootstrap_context().lock().expect("backend context mutex poisoned"); + ctx.registered_backends() } /// Instantiate a rig backend based on the selected name and access method. +/// Plugin compatibility: reads from bootstrap context. pub fn build_rig(name: &str, access: RigAccess) -> DynResult> { - let key = normalize_name(name); - let reg = registry().lock().expect("backend registry mutex poisoned"); - let factory = reg - .factories - .get(&key) - .ok_or_else(|| format!("Unknown rig backend: {}", name))?; - factory(access) + let ctx = bootstrap_context().lock().expect("backend context mutex poisoned"); + ctx.build_rig(name, access) } #[cfg(feature = "ft817")]