diff --git a/src/boxes/array/mod.rs b/src/boxes/array/mod.rs new file mode 100644 index 00000000..00760d3d --- /dev/null +++ b/src/boxes/array/mod.rs @@ -0,0 +1,34 @@ +//! ArrayBox 📦 - 配列・リスト操作(両者一致!) +// Nyashの箱システムによる配列・リスト操作を提供します。 +// 参考: 既存Boxの設計思想 + +pub struct ArrayBox { + pub items: Vec>, +} + +impl ArrayBox { + /// 新しいArrayBoxを作成 + pub fn new() -> Self { + ArrayBox { items: Vec::new() } + } + /// 要素を追加 + pub fn push(&mut self, item: Box) { + self.items.push(item); + } + /// 要素数を取得 + pub fn len(&self) -> usize { + self.items.len() + } + /// 要素を取得 + pub fn get(&self, index: usize) -> Option<&Box> { + self.items.get(index) + } + /// 要素を削除 + pub fn remove(&mut self, index: usize) -> Option> { + if index < self.items.len() { + Some(self.items.remove(index)) + } else { + None + } + } +} diff --git a/src/boxes/buffer/mod.rs b/src/boxes/buffer/mod.rs new file mode 100644 index 00000000..8d665439 --- /dev/null +++ b/src/boxes/buffer/mod.rs @@ -0,0 +1,22 @@ +//! BufferBox 📊 - バイナリデータ処理 +// Nyashの箱システムによるバイナリデータ処理を提供します。 +// 参考: 既存Boxの設計思想 + +pub struct BufferBox { + pub data: Vec, +} + +impl BufferBox { + pub fn new() -> Self { + BufferBox { data: Vec::new() } + } + pub fn from_vec(data: Vec) -> Self { + BufferBox { data } + } + pub fn len(&self) -> usize { + self.data.len() + } + pub fn as_slice(&self) -> &[u8] { + &self.data + } +} diff --git a/src/boxes/file/mod.rs b/src/boxes/file/mod.rs new file mode 100644 index 00000000..a54c8c71 --- /dev/null +++ b/src/boxes/file/mod.rs @@ -0,0 +1,25 @@ +//! FileBox 📁 - ファイルI/O(PathBox/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 { + let file = OpenOptions::new().read(true).write(true).create(true).open(path)?; + Ok(FileBox { file }) + } + pub fn read_to_string(&mut self) -> Result { + 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) + } +} diff --git a/src/boxes/future/mod.rs b/src/boxes/future/mod.rs new file mode 100644 index 00000000..c95b4d9d --- /dev/null +++ b/src/boxes/future/mod.rs @@ -0,0 +1,21 @@ +//! FutureBox 🔄 - 非同期処理基盤 +// Nyashの箱システムによる非同期処理の基盤を提供します。 +// 参考: 既存Boxの設計思想 + +use std::future::Future; +use std::pin::Pin; + +pub struct FutureBox { + pub future: Pin + Send>>, +} + +impl FutureBox { + pub fn new(fut: F) -> Self + where + F: Future + Send + 'static, + { + FutureBox { + future: Box::pin(fut), + } + } +} diff --git a/src/boxes/http/mod.rs b/src/boxes/http/mod.rs new file mode 100644 index 00000000..520a1690 --- /dev/null +++ b/src/boxes/http/mod.rs @@ -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 { + let res = self.client.get(url).send()?.text()?; + Ok(res) + } +} diff --git a/src/boxes/json/mod.rs b/src/boxes/json/mod.rs new file mode 100644 index 00000000..dba4271e --- /dev/null +++ b/src/boxes/json/mod.rs @@ -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 { + let value = serde_json::from_str(s)?; + Ok(JSONBox { value }) + } + pub fn to_string(&self) -> String { + self.value.to_string() + } +} diff --git a/src/boxes/regex/mod.rs b/src/boxes/regex/mod.rs new file mode 100644 index 00000000..3d52e803 --- /dev/null +++ b/src/boxes/regex/mod.rs @@ -0,0 +1,19 @@ +//! RegexBox 🔍 - 正規表現 +// Nyashの箱システムによる正規表現処理を提供します。 +// 参考: 既存Boxの設計思想 + +use regex::Regex; + +pub struct RegexBox { + pub regex: Regex, +} + +impl RegexBox { + pub fn new(pattern: &str) -> Result { + let regex = Regex::new(pattern)?; + Ok(RegexBox { regex }) + } + pub fn is_match(&self, text: &str) -> bool { + self.regex.is_match(text) + } +} diff --git a/src/boxes/result/mod.rs b/src/boxes/result/mod.rs new file mode 100644 index 00000000..b62e5d80 --- /dev/null +++ b/src/boxes/result/mod.rs @@ -0,0 +1,23 @@ +//! ResultBox ⚠️ - エラー処理(ResultBox推奨) +// Nyashの箱システムによるエラー処理を提供します。 +// 参考: 既存Boxの設計思想 + +pub enum ResultBox { + Ok(T), + Err(E), +} + +impl ResultBox { + 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"), + } + } +} diff --git a/src/boxes/stream/mod.rs b/src/boxes/stream/mod.rs new file mode 100644 index 00000000..1d9399df --- /dev/null +++ b/src/boxes/stream/mod.rs @@ -0,0 +1,22 @@ +//! StreamBox 🌊 - ストリーミング処理 +// Nyashの箱システムによるストリーミング処理を提供します。 +// 参考: 既存Boxの設計思想 + +use std::io::{Read, Write, Result}; + +pub struct StreamBox { + pub reader: R, + pub writer: W, +} + +impl StreamBox { + pub fn new(reader: R, writer: W) -> Self { + StreamBox { reader, writer } + } + pub fn read(&mut self, buf: &mut [u8]) -> Result { + self.reader.read(buf) + } + pub fn write(&mut self, buf: &[u8]) -> Result<()> { + self.writer.write_all(buf) + } +}