feat: Phase 1完了 - plugin_loader_v2大規模リファクタリング(1006→893行、420行分離)

 Single Responsibility Principle適用による構造改善
- extern_functions.rs: env.*外部関数処理(261行)
- ffi_bridge.rs: FFI/TLV処理(158行)
- instance_manager.rs: インスタンス管理(140行)
- loader.rs: 1006→893行(11%削減)

🎯 効果:
- 6つの責任を分離し単一責任原則遵守
- モジュール間の境界明確化
- 保守性・可読性大幅向上

📦 追加: filebox-pluginモジュール化も含む
This commit is contained in:
Selfhosting Dev
2025-09-25 02:21:52 +09:00
parent b4f6818f3b
commit b0b667a39d
11 changed files with 1435 additions and 1060 deletions

View File

@ -0,0 +1,92 @@
//! State management for FileBox plugin
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::{
atomic::{AtomicU32, Ordering},
Mutex,
};
// ============ FileBox Instance ============
pub struct FileBoxInstance {
pub file: Option<std::fs::File>,
pub path: String,
pub buffer: Option<Vec<u8>>, // プラグインが管理するバッファ
}
impl FileBoxInstance {
pub fn new() -> Self {
Self {
file: None,
path: String::new(),
buffer: None,
}
}
pub fn with_path(path: String) -> Self {
Self {
file: None,
path,
buffer: None,
}
}
}
// グローバルインスタンス管理
pub static INSTANCES: Lazy<Mutex<HashMap<u32, FileBoxInstance>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
// インスタンスIDカウンタ1開始
pub static INSTANCE_COUNTER: AtomicU32 = AtomicU32::new(1);
/// Allocate a new instance ID
pub fn allocate_instance_id() -> u32 {
INSTANCE_COUNTER.fetch_add(1, Ordering::Relaxed)
}
/// Store an instance with the given ID
pub fn store_instance(id: u32, instance: FileBoxInstance) -> Result<(), &'static str> {
match INSTANCES.lock() {
Ok(mut map) => {
map.insert(id, instance);
Ok(())
}
Err(_) => Err("Failed to lock instances map"),
}
}
/// Remove an instance by ID
pub fn remove_instance(id: u32) -> Option<FileBoxInstance> {
match INSTANCES.lock() {
Ok(mut map) => map.remove(&id),
Err(_) => None,
}
}
/// Get mutable access to an instance
pub fn with_instance_mut<F, R>(id: u32, f: F) -> Result<R, &'static str>
where
F: FnOnce(&mut FileBoxInstance) -> R,
{
match INSTANCES.lock() {
Ok(mut map) => match map.get_mut(&id) {
Some(instance) => Ok(f(instance)),
None => Err("Instance not found"),
},
Err(_) => Err("Failed to lock instances map"),
}
}
/// Get access to an instance
pub fn with_instance<F, R>(id: u32, f: F) -> Result<R, &'static str>
where
F: FnOnce(&FileBoxInstance) -> R,
{
match INSTANCES.lock() {
Ok(map) => match map.get(&id) {
Some(instance) => Ok(f(instance)),
None => Err("Instance not found"),
},
Err(_) => Err("Failed to lock instances map"),
}
}