FileBox SSOT設計移行完了: Provider Pattern実装

## 🎯 目的
FileBoxをSSOT(Single Source of Truth)設計に移行し、
static/dynamic/builtin providerを統一的に扱える基盤を構築。

##  実装完了(7タスク)

### 1. Provider Lock Global
**File**: `src/runtime/provider_lock.rs`
- `FILEBOX_PROVIDER: OnceLock<Arc<dyn FileIo>>` 追加
- `set_filebox_provider()` / `get_filebox_provider()` 実装

### 2. VM初期化時のProvider選択
**File**: `src/runner/modes/vm.rs`
- `execute_vm_mode()` 冒頭でprovider選択・登録
- ENV(`NYASH_FILEBOX_MODE`, `NYASH_DISABLE_PLUGINS`)対応

### 3. CoreRoFileIo完全実装
**File**: `src/boxes/file/core_ro.rs` (NEW)
- Read-onlyファイルI/O実装
- Thread-safe: `RwLock<Option<File>>`
- Newline正規化(CRLF→LF)

### 4. FileBox委譲化
**File**: `src/boxes/file/mod.rs`
- 直接`std::fs::File`使用 → `Arc<dyn FileIo>` provider委譲
- 全メソッドをprovider経由に変更
- Fail-Fast: write/delete等の非対応操作は明確エラー

### 5. basic/file_box.rs Deprecate
**File**: `src/boxes/file/basic/file_box.rs`
- 120行 → 12行に削減
- `#[deprecated]` マーク + 再エクスポート
- 後方互換性維持

### 6. Feature Flag追加
**File**: `Cargo.toml`
- `builtin-filebox = []` feature追加

### 7. Provider抽象・選択ロジック
**Files**:
- `src/boxes/file/provider.rs` (NEW) - FileIo trait定義
- `src/boxes/file/box_shim.rs` (NEW) - 薄いラッパー
- `src/runner/modes/common_util/provider_registry.rs` (NEW) - 選択ロジック

## 📊 アーキテクチャ進化

**Before**:
```
FileBox (mod.rs) ──直接使用──> std::fs::File
FileBox (basic/)  ──直接使用──> std::fs
```

**After**:
```
FileBox ──委譲──> Arc<dyn FileIo> ──実装──> CoreRoFileIo
                                        ├──> PluginFileIo (future)
                                        └──> BuiltinFileIo (future)
```

## 🔧 技術的成果

1. **Thread Safety**: `RwLock<Option<File>>` で並行アクセス安全
2. **Fail-Fast**: 非対応操作は明確エラー(silent failure無し)
3. **後方互換性**: deprecated re-exportで既存コード維持
4. **環境制御**: `NYASH_FILEBOX_MODE` でランタイム切替

## 📝 環境変数

- `NYASH_FILEBOX_MODE=auto|core-ro|plugin-only`
  - `auto`: プラグインあれば使用、なければCoreRoにフォールバック
  - `core-ro`: 強制的にCoreRo(read-only)
  - `plugin-only`: プラグイン必須(なければFail-Fast)
- `NYASH_DISABLE_PLUGINS=1`: 強制的にcore-roモード

## 🎯 次のステップ(Future)

- [ ] Dynamic統合(plugin_loaderとの連携)
- [ ] BuiltinFileIo実装(feature builtin-filebox)
- [ ] Write/Delete等の操作対応(provider拡張)

## 📚 ドキュメント

- 詳細仕様: `docs/development/runtime/FILEBOX_PROVIDER.md`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-08 15:13:22 +09:00
parent f7737d409d
commit cef820596f
11 changed files with 300 additions and 187 deletions

View File

@ -1,120 +1,12 @@
//! FileBox - File system operations in Nyash
//! DEPRECATED: Use `crate::boxes::file::FileBox` instead
//!
//! Implements the FileBox type with file I/O operations.
//! This module is kept for backward compatibility only.
//! All new code should use the SSOT provider-based FileBox implementation.
use crate::box_trait::{NyashBox, BoxCore, BoxBase, StringBox, BoolBox, VoidBox};
use std::any::Any;
use std::fmt::{Debug, Display};
use std::fs;
use std::path::Path;
#![deprecated(
since = "0.1.0",
note = "Use crate::boxes::file::FileBox instead. This module will be removed in a future version."
)]
/// File values in Nyash - file system operations
#[derive(Debug, Clone)]
pub struct FileBox {
pub path: String,
base: BoxBase,
}
impl FileBox {
pub fn new(path: impl Into<String>) -> Self {
Self {
path: path.into(),
base: BoxBase::new(),
}
}
// ===== File Methods for Nyash =====
/// Read file contents as string
pub fn read(&self) -> Box<dyn NyashBox> {
match fs::read_to_string(&self.path) {
Ok(content) => Box::new(StringBox::new(content)),
Err(_) => Box::new(VoidBox::new()), // Return void on error for now
}
}
/// Write content to file
pub fn write(&self, content: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let content_str = content.to_string_box().value;
match fs::write(&self.path, content_str) {
Ok(_) => Box::new(BoolBox::new(true)),
Err(_) => Box::new(BoolBox::new(false)),
}
}
/// Check if file exists
pub fn exists(&self) -> Box<dyn NyashBox> {
Box::new(BoolBox::new(Path::new(&self.path).exists()))
}
/// Delete file
pub fn delete(&self) -> Box<dyn NyashBox> {
match fs::remove_file(&self.path) {
Ok(_) => Box::new(BoolBox::new(true)),
Err(_) => Box::new(BoolBox::new(false)),
}
}
/// Copy file to destination
pub fn copy(&self, dest_path: &str) -> Box<dyn NyashBox> {
match fs::copy(&self.path, dest_path) {
Ok(_) => Box::new(BoolBox::new(true)),
Err(_) => Box::new(BoolBox::new(false)),
}
}
}
impl BoxCore for FileBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "<FileBox: {}>", self.path)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl NyashBox for FileBox {
fn to_string_box(&self) -> StringBox {
StringBox::new(format!("<FileBox: {}>", self.path))
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_file) = other.as_any().downcast_ref::<FileBox>() {
BoolBox::new(self.path == other_file.path)
} else {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"FileBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
}
}
impl Display for FileBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_box(f)
}
}
// Re-export the new FileBox implementation for backward compatibility
pub use crate::boxes::file::FileBox;