Add core Box implementations: ArrayBox, ResultBox, FutureBox, BufferBox, FileBox, JSONBox, HttpClientBox, StreamBox, RegexBox

This commit is contained in:
moe-charm
2025-08-10 02:45:57 +00:00
parent ccb3204a35
commit 6f916855c8
9 changed files with 207 additions and 0 deletions

34
src/boxes/array/mod.rs Normal file
View File

@ -0,0 +1,34 @@
//! ArrayBox 📦 - 配列・リスト操作(両者一致!)
// Nyashの箱システムによる配列・リスト操作を提供します。
// 参考: 既存Boxの設計思想
pub struct ArrayBox {
pub items: Vec<Box<dyn std::any::Any>>,
}
impl ArrayBox {
/// 新しいArrayBoxを作成
pub fn new() -> Self {
ArrayBox { items: Vec::new() }
}
/// 要素を追加
pub fn push(&mut self, item: Box<dyn std::any::Any>) {
self.items.push(item);
}
/// 要素数を取得
pub fn len(&self) -> usize {
self.items.len()
}
/// 要素を取得
pub fn get(&self, index: usize) -> Option<&Box<dyn std::any::Any>> {
self.items.get(index)
}
/// 要素を削除
pub fn remove(&mut self, index: usize) -> Option<Box<dyn std::any::Any>> {
if index < self.items.len() {
Some(self.items.remove(index))
} else {
None
}
}
}

22
src/boxes/buffer/mod.rs Normal file
View File

@ -0,0 +1,22 @@
//! BufferBox 📊 - バイナリデータ処理
// Nyashの箱システムによるバイナリデータ処理を提供します。
// 参考: 既存Boxの設計思想
pub struct BufferBox {
pub data: Vec<u8>,
}
impl BufferBox {
pub fn new() -> Self {
BufferBox { data: Vec::new() }
}
pub fn from_vec(data: Vec<u8>) -> Self {
BufferBox { data }
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn as_slice(&self) -> &[u8] {
&self.data
}
}

25
src/boxes/file/mod.rs Normal file
View File

@ -0,0 +1,25 @@
//! FileBox 📁 - ファイルI/OPathBox/DirBoxとセット
// Nyashの箱システムによるファイル入出力を提供します。
// 参考: 既存Boxの設計思想
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Result};
pub struct FileBox {
pub file: File,
}
impl FileBox {
pub fn open(path: &str) -> Result<Self> {
let file = OpenOptions::new().read(true).write(true).create(true).open(path)?;
Ok(FileBox { file })
}
pub fn read_to_string(&mut self) -> Result<String> {
let mut s = String::new();
self.file.read_to_string(&mut s)?;
Ok(s)
}
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.file.write_all(buf)
}
}

21
src/boxes/future/mod.rs Normal file
View File

@ -0,0 +1,21 @@
//! FutureBox 🔄 - 非同期処理基盤
// Nyashの箱システムによる非同期処理の基盤を提供します。
// 参考: 既存Boxの設計思想
use std::future::Future;
use std::pin::Pin;
pub struct FutureBox<T> {
pub future: Pin<Box<dyn Future<Output = T> + Send>>,
}
impl<T> FutureBox<T> {
pub fn new<F>(fut: F) -> Self
where
F: Future<Output = T> + Send + 'static,
{
FutureBox {
future: Box::pin(fut),
}
}
}

22
src/boxes/http/mod.rs Normal file
View File

@ -0,0 +1,22 @@
//! HttpClientBox 🌐 - HTTP通信
// Nyashの箱システムによるHTTP通信を提供します。
// 参考: 既存Boxの設計思想
use reqwest::blocking::Client;
use reqwest::Result;
pub struct HttpClientBox {
pub client: Client,
}
impl HttpClientBox {
pub fn new() -> Self {
HttpClientBox {
client: Client::new(),
}
}
pub fn get(&self, url: &str) -> Result<String> {
let res = self.client.get(url).send()?.text()?;
Ok(res)
}
}

19
src/boxes/json/mod.rs Normal file
View File

@ -0,0 +1,19 @@
//! JSONBox 📋 - JSON解析・生成
// Nyashの箱システムによるJSON解析・生成を提供します。
// 参考: 既存Boxの設計思想
use serde_json::{Value, Error};
pub struct JSONBox {
pub value: Value,
}
impl JSONBox {
pub fn from_str(s: &str) -> Result<Self, Error> {
let value = serde_json::from_str(s)?;
Ok(JSONBox { value })
}
pub fn to_string(&self) -> String {
self.value.to_string()
}
}

19
src/boxes/regex/mod.rs Normal file
View File

@ -0,0 +1,19 @@
//! RegexBox 🔍 - 正規表現
// Nyashの箱システムによる正規表現処理を提供します。
// 参考: 既存Boxの設計思想
use regex::Regex;
pub struct RegexBox {
pub regex: Regex,
}
impl RegexBox {
pub fn new(pattern: &str) -> Result<Self, regex::Error> {
let regex = Regex::new(pattern)?;
Ok(RegexBox { regex })
}
pub fn is_match(&self, text: &str) -> bool {
self.regex.is_match(text)
}
}

23
src/boxes/result/mod.rs Normal file
View File

@ -0,0 +1,23 @@
//! ResultBox ⚠️ - エラー処理ResultBox推奨
// Nyashの箱システムによるエラー処理を提供します。
// 参考: 既存Boxの設計思想
pub enum ResultBox<T, E> {
Ok(T),
Err(E),
}
impl<T, E> ResultBox<T, E> {
pub fn is_ok(&self) -> bool {
matches!(self, ResultBox::Ok(_))
}
pub fn is_err(&self) -> bool {
matches!(self, ResultBox::Err(_))
}
pub fn unwrap(self) -> T {
match self {
ResultBox::Ok(val) => val,
ResultBox::Err(_) => panic!("called `unwrap()` on an `Err` value"),
}
}
}

22
src/boxes/stream/mod.rs Normal file
View File

@ -0,0 +1,22 @@
//! StreamBox 🌊 - ストリーミング処理
// Nyashの箱システムによるストリーミング処理を提供します。
// 参考: 既存Boxの設計思想
use std::io::{Read, Write, Result};
pub struct StreamBox<R: Read, W: Write> {
pub reader: R,
pub writer: W,
}
impl<R: Read, W: Write> StreamBox<R, W> {
pub fn new(reader: R, writer: W) -> Self {
StreamBox { reader, writer }
}
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.reader.read(buf)
}
pub fn write(&mut self, buf: &[u8]) -> Result<()> {
self.writer.write_all(buf)
}
}