2025-08-10 02:45:57 +00:00
|
|
|
|
//! ResultBox ⚠️ - エラー処理(ResultBox推奨)
|
|
|
|
|
|
// Nyashの箱システムによるエラー処理を提供します。
|
|
|
|
|
|
// 参考: 既存Boxの設計思想
|
|
|
|
|
|
|
2025-08-11 12:09:14 +09:00
|
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore};
|
2025-08-10 03:21:24 +00:00
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub enum NyashResultBox {
|
|
|
|
|
|
Ok(Box<dyn NyashBox>),
|
|
|
|
|
|
Err(Box<dyn NyashBox>),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NyashResultBox {
|
|
|
|
|
|
pub fn new_ok(value: Box<dyn NyashBox>) -> Self {
|
|
|
|
|
|
NyashResultBox::Ok(value)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new_err(error: Box<dyn NyashBox>) -> Self {
|
|
|
|
|
|
NyashResultBox::Err(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-10 15:15:10 +09:00
|
|
|
|
pub fn is_ok_bool(&self) -> bool {
|
2025-08-10 03:21:24 +00:00
|
|
|
|
matches!(self, NyashResultBox::Ok(_))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn is_err(&self) -> bool {
|
|
|
|
|
|
matches!(self, NyashResultBox::Err(_))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn unwrap(self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => val,
|
|
|
|
|
|
NyashResultBox::Err(_) => panic!("called `unwrap()` on an `Err` value"),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NyashBox for NyashResultBox {
|
|
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => Box::new(NyashResultBox::Ok(val.clone_box())),
|
|
|
|
|
|
NyashResultBox::Err(err) => Box::new(NyashResultBox::Err(err.clone_box())),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-15 14:29:47 +09:00
|
|
|
|
|
|
|
|
|
|
/// 仮実装: clone_boxと同じ(後で修正)
|
|
|
|
|
|
fn share_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
self.clone_box()
|
|
|
|
|
|
}
|
2025-08-10 03:21:24 +00:00
|
|
|
|
|
|
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => StringBox::new(format!("Ok({})", val.to_string_box().value)),
|
|
|
|
|
|
NyashResultBox::Err(err) => StringBox::new(format!("Err({})", err.to_string_box().value)),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
|
"NyashResultBox"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
|
if let Some(other_result) = other.as_any().downcast_ref::<NyashResultBox>() {
|
|
|
|
|
|
match (self, other_result) {
|
|
|
|
|
|
(NyashResultBox::Ok(a), NyashResultBox::Ok(b)) => a.equals(b.as_ref()),
|
|
|
|
|
|
(NyashResultBox::Err(a), NyashResultBox::Err(b)) => a.equals(b.as_ref()),
|
|
|
|
|
|
_ => BoolBox::new(false),
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-11 12:09:14 +09:00
|
|
|
|
impl BoxCore for NyashResultBox {
|
|
|
|
|
|
fn box_id(&self) -> u64 {
|
|
|
|
|
|
// For enum variants, we use the contained value's ID
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => val.box_id(),
|
|
|
|
|
|
NyashResultBox::Err(err) => err.box_id(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-11 15:01:11 +09:00
|
|
|
|
|
|
|
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
|
|
|
|
|
// For enum variants, we use the contained value's parent type ID
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => val.parent_type_id(),
|
|
|
|
|
|
NyashResultBox::Err(err) => err.parent_type_id(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-11 12:09:14 +09:00
|
|
|
|
|
|
|
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => write!(f, "Ok({})", val.to_string_box().value),
|
|
|
|
|
|
NyashResultBox::Err(err) => write!(f, "Err({})", err.to_string_box().value),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-11 15:01:11 +09:00
|
|
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2025-08-11 12:09:14 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for NyashResultBox {
|
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
|
self.fmt_box(f)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
🔧 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>
2025-08-10 13:03:42 +09:00
|
|
|
|
// Export NyashResultBox as ResultBox for compatibility
|
|
|
|
|
|
pub type ResultBox = NyashResultBox;
|
2025-08-10 02:45:57 +00:00
|
|
|
|
|
🔧 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>
2025-08-10 13:03:42 +09:00
|
|
|
|
impl ResultBox {
|
|
|
|
|
|
/// is_ok()の実装
|
|
|
|
|
|
pub fn is_ok(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
Box::new(BoolBox::new(matches!(self, NyashResultBox::Ok(_))))
|
2025-08-10 02:45:57 +00:00
|
|
|
|
}
|
🔧 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>
2025-08-10 13:03:42 +09:00
|
|
|
|
|
|
|
|
|
|
/// getValue()の実装 - Ok値を取得
|
|
|
|
|
|
pub fn get_value(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
match self {
|
|
|
|
|
|
NyashResultBox::Ok(val) => val.clone_box(),
|
|
|
|
|
|
NyashResultBox::Err(_) => Box::new(StringBox::new("Error: Result is Err")),
|
|
|
|
|
|
}
|
2025-08-10 02:45:57 +00:00
|
|
|
|
}
|
🔧 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>
2025-08-10 13:03:42 +09:00
|
|
|
|
|
|
|
|
|
|
/// getError()の実装 - Err値を取得
|
|
|
|
|
|
pub fn get_error(&self) -> Box<dyn NyashBox> {
|
2025-08-10 02:45:57 +00:00
|
|
|
|
match self {
|
🔧 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>
2025-08-10 13:03:42 +09:00
|
|
|
|
NyashResultBox::Ok(_) => Box::new(StringBox::new("Error: Result is Ok")),
|
|
|
|
|
|
NyashResultBox::Err(err) => err.clone_box(),
|
2025-08-10 02:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|