chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt

This commit is contained in:
Selfhosting Dev
2025-09-17 07:43:07 +09:00
parent fcf8ce1f3c
commit adbb0201a9
385 changed files with 35622 additions and 15004 deletions

View File

@ -1,17 +1,17 @@
/*!
* Nyash Box Trait System - Everything is Box in Rust
*
*
* This module implements the core "Everything is Box" philosophy using Rust's
* ownership system and trait system. Every value in Nyash is a Box that
* implements the NyashBox trait.
*/
use std::fmt::{Debug, Display};
use std::any::Any;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::fmt::{Debug, Display};
use std::fs;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
// 🔥 新しい型エイリアス - 将来的にBox<dyn NyashBox>を全て置き換える
pub type SharedNyashBox = Arc<dyn NyashBox>;
@ -26,14 +26,38 @@ pub fn next_box_id() -> u64 {
/// 🔥 Phase 8.8: pack透明化システム - ビルトインBox判定リスト
/// ユーザーは`pack`を一切意識せず、`from BuiltinBox()`で自動的に内部のpack機能が呼ばれる
pub const BUILTIN_BOXES: &[&str] = &[
"StringBox", "IntegerBox", "BoolBox", "NullBox",
"ArrayBox", "MapBox", "FileBox", "ResultBox",
"FutureBox", "ChannelBox", "MathBox", "FloatBox",
"TimeBox", "DateTimeBox", "TimerBox", "RandomBox",
"SoundBox", "DebugBox", "MethodBox", "ConsoleBox",
"BufferBox", "RegexBox", "JSONBox", "StreamBox",
"HTTPClientBox", "IntentBox", "P2PBox", "SocketBox",
"HTTPServerBox", "HTTPRequestBox", "HTTPResponseBox", "JitConfigBox"
"StringBox",
"IntegerBox",
"BoolBox",
"NullBox",
"ArrayBox",
"MapBox",
"FileBox",
"ResultBox",
"FutureBox",
"ChannelBox",
"MathBox",
"FloatBox",
"TimeBox",
"DateTimeBox",
"TimerBox",
"RandomBox",
"SoundBox",
"DebugBox",
"MethodBox",
"ConsoleBox",
"BufferBox",
"RegexBox",
"JSONBox",
"StreamBox",
"HTTPClientBox",
"IntentBox",
"P2PBox",
"SocketBox",
"HTTPServerBox",
"HTTPRequestBox",
"HTTPResponseBox",
"JitConfigBox",
];
/// 🔥 ビルトインBox判定関数 - pack透明化システムの核心
@ -59,7 +83,7 @@ impl BoxBase {
parent_type_id: None, // ビルトインBox: 継承なし
}
}
/// ビルトインBox継承用コンストラクタ
pub fn with_parent_type(parent_type_id: std::any::TypeId) -> Self {
Self {
@ -75,16 +99,16 @@ impl BoxBase {
pub trait BoxCore: Send + Sync {
/// ボックスの一意ID取得
fn box_id(&self) -> u64;
/// 継承元の型ID取得ビルトインBox継承用
fn parent_type_id(&self) -> Option<std::any::TypeId>;
/// Display実装のための統一フォーマット
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result;
/// Any変換ダウンキャスト用
fn as_any(&self) -> &dyn Any;
/// Anyミュータブル変換ダウンキャスト用
fn as_any_mut(&mut self) -> &mut dyn Any;
}
@ -94,34 +118,40 @@ pub trait BoxCore: Send + Sync {
pub trait NyashBox: BoxCore + Debug {
/// Convert this box to a string representation (equivalent to Python's toString())
fn to_string_box(&self) -> StringBox;
/// Check equality with another box (equivalent to Python's equals())
fn equals(&self, other: &dyn NyashBox) -> BoolBox;
/// Get the type name of this box for debugging
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
/// Clone this box (equivalent to Python's copy())
fn clone_box(&self) -> Box<dyn NyashBox>;
/// Share this box (state-preserving reference sharing)
fn share_box(&self) -> Box<dyn NyashBox>;
/// Identity hint: boxes that wrap external/stateful handles should override to return true.
fn is_identity(&self) -> bool { false }
fn is_identity(&self) -> bool {
false
}
/// Helper: pick share or clone based on identity semantics.
fn clone_or_share(&self) -> Box<dyn NyashBox> {
if self.is_identity() { self.share_box() } else { self.clone_box() }
if self.is_identity() {
self.share_box()
} else {
self.clone_box()
}
}
/// Arc参照を返す新しいcloneメソッド参照共有
fn clone_arc(&self) -> SharedNyashBox {
Arc::from(self.clone_box())
}
// 🌟 TypeBox革命: Get type information as a Box
// Everything is Box極限実現 - 型情報もBoxとして取得
// TODO: 次のステップで完全実装
@ -144,22 +174,23 @@ impl StringBox {
base: BoxBase::new(),
}
}
pub fn empty() -> Self {
Self::new("")
}
// ===== String Methods for Nyash =====
/// Split string by delimiter and return ArrayBox
pub fn split(&self, delimiter: &str) -> Box<dyn NyashBox> {
let parts: Vec<String> = self.value.split(delimiter).map(|s| s.to_string()).collect();
let array_elements: Vec<Box<dyn NyashBox>> = parts.into_iter()
let array_elements: Vec<Box<dyn NyashBox>> = parts
.into_iter()
.map(|s| Box::new(StringBox::new(s)) as Box<dyn NyashBox>)
.collect();
Box::new(ArrayBox::new_with_elements(array_elements))
}
/// Find substring and return position (or -1 if not found)
pub fn find(&self, search: &str) -> Box<dyn NyashBox> {
match self.value.find(search) {
@ -167,46 +198,49 @@ impl StringBox {
None => Box::new(IntegerBox::new(-1)),
}
}
/// Replace all occurrences of old with new
pub fn replace(&self, old: &str, new: &str) -> Box<dyn NyashBox> {
Box::new(StringBox::new(self.value.replace(old, new)))
}
/// Trim whitespace from both ends
pub fn trim(&self) -> Box<dyn NyashBox> {
Box::new(StringBox::new(self.value.trim()))
}
/// Convert to uppercase
pub fn to_upper(&self) -> Box<dyn NyashBox> {
Box::new(StringBox::new(self.value.to_uppercase()))
}
/// Convert to lowercase
pub fn to_lower(&self) -> Box<dyn NyashBox> {
Box::new(StringBox::new(self.value.to_lowercase()))
}
/// Check if string contains substring
pub fn contains(&self, search: &str) -> Box<dyn NyashBox> {
Box::new(BoolBox::new(self.value.contains(search)))
}
/// Check if string starts with prefix
pub fn starts_with(&self, prefix: &str) -> Box<dyn NyashBox> {
Box::new(BoolBox::new(self.value.starts_with(prefix)))
}
/// Check if string ends with suffix
pub fn ends_with(&self, suffix: &str) -> Box<dyn NyashBox> {
Box::new(BoolBox::new(self.value.ends_with(suffix)))
}
/// Join array elements using this string as delimiter
pub fn join(&self, array_box: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
if let Some(array) = array_box.as_any().downcast_ref::<ArrayBox>() {
let strings: Vec<String> = array.items.read().unwrap()
let strings: Vec<String> = array
.items
.read()
.unwrap()
.iter()
.map(|element| element.to_string_box().value)
.collect();
@ -216,12 +250,12 @@ impl StringBox {
Box::new(StringBox::new(array_box.to_string_box().value))
}
}
/// Get string length
pub fn length(&self) -> Box<dyn NyashBox> {
Box::new(IntegerBox::new(self.value.len() as i64))
}
/// Convert string to integer (parse as i64)
pub fn to_integer(&self) -> Box<dyn NyashBox> {
match self.value.trim().parse::<i64>() {
@ -232,7 +266,7 @@ impl StringBox {
}
}
}
/// Get character at index
pub fn get(&self, index: usize) -> Option<Box<dyn NyashBox>> {
if let Some(ch) = self.value.chars().nth(index) {
@ -241,7 +275,7 @@ impl StringBox {
None
}
}
/// Get substring from start to end (exclusive)
pub fn substring(&self, start: usize, end: usize) -> Box<dyn NyashBox> {
let chars: Vec<char> = self.value.chars().collect();
@ -256,19 +290,19 @@ impl BoxCore for StringBox {
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, "{}", self.value)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -278,7 +312,7 @@ impl NyashBox for StringBox {
fn to_string_box(&self) -> StringBox {
self.clone()
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_string) = other.as_any().downcast_ref::<StringBox>() {
BoolBox::new(self.value == other_string.value)
@ -286,15 +320,15 @@ impl NyashBox for StringBox {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"StringBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -316,12 +350,12 @@ pub struct IntegerBox {
impl IntegerBox {
pub fn new(value: i64) -> Self {
Self {
value,
base: BoxBase::new()
Self {
value,
base: BoxBase::new(),
}
}
pub fn zero() -> Self {
Self::new(0)
}
@ -331,19 +365,19 @@ impl BoxCore for IntegerBox {
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, "{}", self.value)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -353,7 +387,7 @@ impl NyashBox for IntegerBox {
fn to_string_box(&self) -> StringBox {
StringBox::new(self.value.to_string())
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_int) = other.as_any().downcast_ref::<IntegerBox>() {
BoolBox::new(self.value == other_int.value)
@ -361,15 +395,15 @@ impl NyashBox for IntegerBox {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"IntegerBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -391,16 +425,16 @@ pub struct BoolBox {
impl BoolBox {
pub fn new(value: bool) -> Self {
Self {
value,
base: BoxBase::new()
Self {
value,
base: BoxBase::new(),
}
}
pub fn true_box() -> Self {
Self::new(true)
}
pub fn false_box() -> Self {
Self::new(false)
}
@ -410,19 +444,19 @@ impl BoxCore for BoolBox {
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, "{}", if self.value { "true" } else { "false" })
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -432,7 +466,7 @@ impl NyashBox for BoolBox {
fn to_string_box(&self) -> StringBox {
StringBox::new(if self.value { "true" } else { "false" })
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_bool) = other.as_any().downcast_ref::<BoolBox>() {
BoolBox::new(self.value == other_bool.value)
@ -440,15 +474,15 @@ impl NyashBox for BoolBox {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"BoolBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -469,8 +503,8 @@ pub struct VoidBox {
impl VoidBox {
pub fn new() -> Self {
Self {
base: BoxBase::new()
Self {
base: BoxBase::new(),
}
}
}
@ -485,19 +519,19 @@ impl BoxCore for VoidBox {
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, "void")
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -507,19 +541,19 @@ impl NyashBox for VoidBox {
fn to_string_box(&self) -> StringBox {
StringBox::new("void")
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
BoolBox::new(other.as_any().is::<VoidBox>())
}
fn type_name(&self) -> &'static str {
"VoidBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -549,9 +583,9 @@ impl FileBox {
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) {
@ -559,7 +593,7 @@ impl FileBox {
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;
@ -568,12 +602,12 @@ impl FileBox {
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) {
@ -581,7 +615,7 @@ impl FileBox {
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) {
@ -595,19 +629,19 @@ 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
}
@ -617,7 +651,7 @@ 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)
@ -625,15 +659,15 @@ impl NyashBox for FileBox {
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()
@ -668,19 +702,19 @@ impl BoxCore for ErrorBox {
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, "{}: {}", self.error_type, self.message)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -690,23 +724,25 @@ impl NyashBox for ErrorBox {
fn to_string_box(&self) -> StringBox {
StringBox::new(format!("{}: {}", self.error_type, self.message))
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_error) = other.as_any().downcast_ref::<ErrorBox>() {
BoolBox::new(self.error_type == other_error.error_type && self.message == other_error.message)
BoolBox::new(
self.error_type == other_error.error_type && self.message == other_error.message,
)
} else {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"ErrorBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -719,17 +755,18 @@ impl Display for ErrorBox {
}
}
// FutureBox is now implemented in src/boxes/future/mod.rs using RwLock pattern
// and re-exported from src/boxes/mod.rs as both NyashFutureBox and FutureBox
// Re-export operation boxes from the dedicated operations module
pub use crate::box_arithmetic::{AddBox, SubtractBox, MultiplyBox, DivideBox, ModuloBox, CompareBox};
pub use crate::box_arithmetic::{
AddBox, CompareBox, DivideBox, ModuloBox, MultiplyBox, SubtractBox,
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_string_box_creation() {
let s = StringBox::new("Hello, Rust!");
@ -737,7 +774,7 @@ mod tests {
assert_eq!(s.type_name(), "StringBox");
assert_eq!(s.to_string_box().value, "Hello, Rust!");
}
#[test]
fn test_integer_box_creation() {
let i = IntegerBox::new(42);
@ -745,7 +782,7 @@ mod tests {
assert_eq!(i.type_name(), "IntegerBox");
assert_eq!(i.to_string_box().value, "42");
}
#[test]
fn test_bool_box_creation() {
let b = BoolBox::new(true);
@ -753,48 +790,48 @@ mod tests {
assert_eq!(b.type_name(), "BoolBox");
assert_eq!(b.to_string_box().value, "true");
}
#[test]
fn test_box_equality() {
let s1 = StringBox::new("test");
let s2 = StringBox::new("test");
let s3 = StringBox::new("different");
assert!(s1.equals(&s2).value);
assert!(!s1.equals(&s3).value);
}
#[test]
fn test_add_box_integers() {
let left = Box::new(IntegerBox::new(5)) as Box<dyn NyashBox>;
let right = Box::new(IntegerBox::new(3)) as Box<dyn NyashBox>;
let add = AddBox::new(left, right);
let result = add.execute();
let result_int = result.as_any().downcast_ref::<IntegerBox>().unwrap();
assert_eq!(result_int.value, 8);
}
#[test]
fn test_add_box_strings() {
let left = Box::new(StringBox::new("Hello, ")) as Box<dyn NyashBox>;
let right = Box::new(StringBox::new("Rust!")) as Box<dyn NyashBox>;
let add = AddBox::new(left, right);
let result = add.execute();
let result_str = result.as_any().downcast_ref::<StringBox>().unwrap();
assert_eq!(result_str.value, "Hello, Rust!");
}
#[test]
fn test_box_ids_unique() {
let s1 = StringBox::new("test");
let s2 = StringBox::new("test");
// Same content but different IDs
assert_ne!(s1.box_id(), s2.box_id());
}
#[test]
fn test_void_box() {
let v = VoidBox::new();