🔧 refactor: すべてのBoxをArc<Mutex>パターンで統一
CopilotのBox実装を私たちのArc<Mutex>パターンで統一: - BufferBox: Arc<Mutex<Vec<u8>>>で内部可変性を実現 - FileBox: Arc<Mutex<File>>でファイルハンドル管理 - JSONBox: Arc<Mutex<Value>>でJSON値を保持 - HttpClientBox: Arc<Mutex<Client>>でHTTPクライアント管理 - StreamBox: Arc<Mutex>でストリームバッファと位置を管理 - RegexBox: Arc<Regex>で軽量ラッパー実装 各Boxに実用的なメソッドも追加: - BufferBox: write, read, readAll, clear, length, append, slice - FileBox: read, write, exists, delete, copy - JSONBox: parse, stringify, get, set, has, keys - HttpClientBox: get, post, put, delete, request - StreamBox: write, read, position, length, reset - RegexBox: test, find, findAll, replace, split interpreterに新Box用のメソッド実行を追加: - data_methods.rs: BufferBox, JSONBox, RegexBox - network_methods.rs: HttpClientBox, StreamBox これでコードベース全体が一貫性のあるArc<Mutex>パターンで統一されました! 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -6,11 +6,12 @@ use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use std::any::Any;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write, Result};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FileBox {
|
||||
pub file: File,
|
||||
pub path: String,
|
||||
file: Arc<Mutex<File>>,
|
||||
path: Arc<String>,
|
||||
id: u64,
|
||||
}
|
||||
|
||||
@ -23,20 +24,61 @@ impl FileBox {
|
||||
};
|
||||
let file = OpenOptions::new().read(true).write(true).create(true).open(path)?;
|
||||
Ok(FileBox {
|
||||
file,
|
||||
path: path.to_string(),
|
||||
file: Arc::new(Mutex::new(file)),
|
||||
path: Arc::new(path.to_string()),
|
||||
id,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_to_string(&mut self) -> Result<String> {
|
||||
pub fn read_to_string(&self) -> Result<String> {
|
||||
let mut file = self.file.lock().unwrap();
|
||||
let mut s = String::new();
|
||||
self.file.read_to_string(&mut s)?;
|
||||
file.read_to_string(&mut s)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
|
||||
self.file.write_all(buf)
|
||||
pub fn write_all(&self, buf: &[u8]) -> Result<()> {
|
||||
let mut file = self.file.lock().unwrap();
|
||||
file.write_all(buf)
|
||||
}
|
||||
|
||||
/// ファイルの内容を読み取る
|
||||
pub fn read(&self) -> Box<dyn NyashBox> {
|
||||
match self.read_to_string() {
|
||||
Ok(content) => Box::new(StringBox::new(&content)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading file: {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
/// ファイルに内容を書き込む
|
||||
pub fn write(&self, content: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let content_str = content.to_string_box().value;
|
||||
match self.write_all(content_str.as_bytes()) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error writing file: {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
/// ファイルが存在するかチェック
|
||||
pub fn exists(&self) -> Box<dyn NyashBox> {
|
||||
use std::path::Path;
|
||||
Box::new(BoolBox::new(Path::new(&**self.path).exists()))
|
||||
}
|
||||
|
||||
/// ファイルを削除
|
||||
pub fn delete(&self) -> Box<dyn NyashBox> {
|
||||
match std::fs::remove_file(&**self.path) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error deleting file: {}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
/// ファイルをコピー
|
||||
pub fn copy(&self, dest: &str) -> Box<dyn NyashBox> {
|
||||
match std::fs::copy(&**self.path, dest) {
|
||||
Ok(_) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error copying file: {}", e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +109,7 @@ impl NyashBox for FileBox {
|
||||
|
||||
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)
|
||||
BoolBox::new(*self.path == *other_file.path)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user