🔧 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:
Moe Charm
2025-08-10 13:03:42 +09:00
parent b745f5ffa2
commit 750543be7c
12 changed files with 989 additions and 110 deletions

View File

@ -4,14 +4,16 @@
use regex::Regex;
use crate::box_trait::{NyashBox, StringBox, BoolBox};
use crate::boxes::array::ArrayBox;
use std::any::Any;
use std::sync::{Arc, Mutex};
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct RegexBox {
pub regex: Regex,
regex: Arc<Regex>,
pattern: Arc<String>,
id: u64,
pattern: String,
}
impl RegexBox {
@ -23,9 +25,9 @@ impl RegexBox {
COUNTER
};
Ok(RegexBox {
regex,
regex: Arc::new(regex),
pattern: Arc::new(pattern.to_string()),
id,
pattern: pattern.to_string(),
})
}
pub fn is_match(&self, text: &str) -> bool {
@ -34,6 +36,54 @@ impl RegexBox {
pub fn pattern(&self) -> &str {
&self.pattern
}
/// パターンマッチテスト
pub fn test(&self, text: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let text_str = text.to_string_box().value;
Box::new(BoolBox::new(self.is_match(&text_str)))
}
/// マッチ箇所を検索
pub fn find(&self, text: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let text_str = text.to_string_box().value;
if let Some(mat) = self.regex.find(&text_str) {
Box::new(StringBox::new(mat.as_str()))
} else {
Box::new(crate::boxes::null_box::NullBox::new())
}
}
/// すべてのマッチを検索
pub fn find_all(&self, text: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let text_str = text.to_string_box().value;
let array = ArrayBox::new();
for mat in self.regex.find_iter(&text_str) {
array.push(Box::new(StringBox::new(mat.as_str())));
}
Box::new(array)
}
/// 文字列置換
pub fn replace(&self, text: Box<dyn NyashBox>, replacement: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let text_str = text.to_string_box().value;
let replacement_str = replacement.to_string_box().value;
let result = self.regex.replace_all(&text_str, replacement_str.as_str());
Box::new(StringBox::new(&result))
}
/// 文字列分割
pub fn split(&self, text: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let text_str = text.to_string_box().value;
let array = ArrayBox::new();
for part in self.regex.split(&text_str) {
array.push(Box::new(StringBox::new(part)));
}
Box::new(array)
}
}
impl NyashBox for RegexBox {
@ -42,7 +92,7 @@ impl NyashBox for RegexBox {
}
fn to_string_box(&self) -> StringBox {
StringBox::new(format!("RegexBox({})", self.pattern))
StringBox::new(format!("RegexBox({})", **self.pattern))
}
fn as_any(&self) -> &dyn Any {
@ -59,7 +109,7 @@ impl NyashBox for RegexBox {
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_regex) = other.as_any().downcast_ref::<RegexBox>() {
BoolBox::new(self.pattern == other_regex.pattern)
BoolBox::new(**self.pattern == **other_regex.pattern)
} else {
BoolBox::new(false)
}