🐛 fix: コンパイルエラーを修正
主な修正内容: - ArrayBox/RegexBox/JSONBoxで`push`メソッドの戻り値を適切に処理 - BufferBoxのインポートパスを修正(buffer::BufferBox) - StreamBoxでArrayBoxを正しくインポート - HTTPClientBoxをスタブ実装に変更(reqwest依存を一時的に無効化) テストプログラムも追加: - test_array_box_simple.nyash: ArrayBoxの基本テスト - test_new_boxes.nyash: 全Box実装の統合テスト NOTE: HTTPサポートは現在OpenSSL/pkg-config依存のため一時的に無効化。 将来的にはrustls等の純Rust実装への移行を検討。 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -1,17 +1,17 @@
|
||||
//! HttpClientBox 🌐 - HTTP通信
|
||||
// Nyashの箱システムによるHTTP通信を提供します。
|
||||
// 参考: 既存Boxの設計思想
|
||||
//
|
||||
// NOTE: HTTPサポートは現在開発中です。
|
||||
// reqwestクレートの依存関係のため、一時的に無効化されています。
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use crate::boxes::map_box::MapBox;
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::Result;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HttpClientBox {
|
||||
client: Arc<Mutex<Client>>,
|
||||
id: u64,
|
||||
}
|
||||
|
||||
@ -22,118 +22,32 @@ impl HttpClientBox {
|
||||
COUNTER += 1;
|
||||
COUNTER
|
||||
};
|
||||
HttpClientBox {
|
||||
client: Arc::new(Mutex::new(Client::new())),
|
||||
id,
|
||||
}
|
||||
HttpClientBox { id }
|
||||
}
|
||||
|
||||
pub fn get(&self, url: &str) -> Result<String> {
|
||||
let client = self.client.lock().unwrap();
|
||||
let res = client.get(url).send()?.text()?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// HTTP GETリクエスト
|
||||
/// HTTP GETリクエスト(スタブ)
|
||||
pub fn http_get(&self, url: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let url_str = url.to_string_box().value;
|
||||
match self.get(&url_str) {
|
||||
Ok(response) => Box::new(StringBox::new(&response)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP GET: {}", e))),
|
||||
}
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
|
||||
/// HTTP POSTリクエスト
|
||||
/// HTTP POSTリクエスト(スタブ)
|
||||
pub fn post(&self, url: Box<dyn NyashBox>, body: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let url_str = url.to_string_box().value;
|
||||
let body_str = body.to_string_box().value;
|
||||
|
||||
let client = self.client.lock().unwrap();
|
||||
match client.post(&url_str).body(body_str).send() {
|
||||
Ok(response) => {
|
||||
match response.text() {
|
||||
Ok(text) => Box::new(StringBox::new(&text)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
||||
}
|
||||
},
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP POST: {}", e))),
|
||||
}
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
|
||||
/// HTTP PUT リクエスト
|
||||
/// HTTP PUT リクエスト(スタブ)
|
||||
pub fn put(&self, url: Box<dyn NyashBox>, body: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let url_str = url.to_string_box().value;
|
||||
let body_str = body.to_string_box().value;
|
||||
|
||||
let client = self.client.lock().unwrap();
|
||||
match client.put(&url_str).body(body_str).send() {
|
||||
Ok(response) => {
|
||||
match response.text() {
|
||||
Ok(text) => Box::new(StringBox::new(&text)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
||||
}
|
||||
},
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP PUT: {}", e))),
|
||||
}
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
|
||||
/// HTTP DELETE リクエスト
|
||||
/// HTTP DELETE リクエスト(スタブ)
|
||||
pub fn delete(&self, url: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let url_str = url.to_string_box().value;
|
||||
|
||||
let client = self.client.lock().unwrap();
|
||||
match client.delete(&url_str).send() {
|
||||
Ok(response) => {
|
||||
match response.text() {
|
||||
Ok(text) => Box::new(StringBox::new(&text)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
||||
}
|
||||
},
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP DELETE: {}", e))),
|
||||
}
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
|
||||
/// ヘッダー付きHTTPリクエスト
|
||||
/// ヘッダー付きHTTPリクエスト(スタブ)
|
||||
pub fn request(&self, method: Box<dyn NyashBox>, url: Box<dyn NyashBox>, options: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let method_str = method.to_string_box().value.to_uppercase();
|
||||
let url_str = url.to_string_box().value;
|
||||
|
||||
// optionsはMapBoxと仮定
|
||||
if let Some(map_box) = options.as_any().downcast_ref::<MapBox>() {
|
||||
let client = self.client.lock().unwrap();
|
||||
let mut request = match method_str.as_str() {
|
||||
"GET" => client.get(&url_str),
|
||||
"POST" => client.post(&url_str),
|
||||
"PUT" => client.put(&url_str),
|
||||
"DELETE" => client.delete(&url_str),
|
||||
_ => return Box::new(StringBox::new(&format!("Unsupported HTTP method: {}", method_str))),
|
||||
};
|
||||
|
||||
// ヘッダー設定
|
||||
if let Some(headers_box) = map_box.get(Box::new(StringBox::new("headers"))).as_any().downcast_ref::<MapBox>() {
|
||||
let headers_map = headers_box.map.lock().unwrap();
|
||||
for (key, value) in headers_map.iter() {
|
||||
request = request.header(key, value.to_string_box().value);
|
||||
}
|
||||
}
|
||||
|
||||
// ボディ設定
|
||||
if let Some(body_box) = map_box.get(Box::new(StringBox::new("body"))).as_any().downcast_ref::<StringBox>() {
|
||||
request = request.body(body_box.value.clone());
|
||||
}
|
||||
|
||||
match request.send() {
|
||||
Ok(response) => {
|
||||
match response.text() {
|
||||
Ok(text) => Box::new(StringBox::new(&text)),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error reading response: {}", e))),
|
||||
}
|
||||
},
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error in HTTP request: {}", e))),
|
||||
}
|
||||
} else {
|
||||
Box::new(StringBox::new("Error: options must be a MapBox"))
|
||||
}
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,4 +79,4 @@ impl NyashBox for HttpClientBox {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,7 +120,8 @@ impl JSONBox {
|
||||
|
||||
if let Some(obj) = value.as_object() {
|
||||
for key in obj.keys() {
|
||||
array.push(Box::new(StringBox::new(key)));
|
||||
// ArrayBoxのpushメソッドは&selfなので、直接呼び出し可能
|
||||
let _ = array.push(Box::new(StringBox::new(key)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -59,7 +59,7 @@ impl RegexBox {
|
||||
let array = ArrayBox::new();
|
||||
|
||||
for mat in self.regex.find_iter(&text_str) {
|
||||
array.push(Box::new(StringBox::new(mat.as_str())));
|
||||
let _ = array.push(Box::new(StringBox::new(mat.as_str())));
|
||||
}
|
||||
|
||||
Box::new(array)
|
||||
@ -79,7 +79,7 @@ impl RegexBox {
|
||||
let array = ArrayBox::new();
|
||||
|
||||
for part in self.regex.split(&text_str) {
|
||||
array.push(Box::new(StringBox::new(part)));
|
||||
let _ = array.push(Box::new(StringBox::new(part)));
|
||||
}
|
||||
|
||||
Box::new(array)
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox};
|
||||
use crate::boxes::buffer::BufferBox;
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::io::{Read, Write, Result};
|
||||
@ -80,10 +81,25 @@ impl NyashStreamBox {
|
||||
pub fn stream_write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
// BufferBoxから変換
|
||||
if let Some(buffer_box) = data.as_any().downcast_ref::<BufferBox>() {
|
||||
let buffer_data = buffer_box.data.lock().unwrap();
|
||||
match self.write(&buffer_data) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error writing to stream: {}", e))),
|
||||
// BufferBoxのreadAllを使用してデータ取得
|
||||
let array_data = buffer_box.readAll();
|
||||
// ArrayBoxをバイト配列に変換
|
||||
if let Some(array_box) = array_data.as_any().downcast_ref::<ArrayBox>() {
|
||||
let items = array_box.items.lock().unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
for item in items.iter() {
|
||||
if let Some(int_box) = item.as_any().downcast_ref::<IntegerBox>() {
|
||||
if int_box.value >= 0 && int_box.value <= 255 {
|
||||
bytes.push(int_box.value as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
match self.write(&bytes) {
|
||||
Ok(()) => Box::new(StringBox::new("ok")),
|
||||
Err(e) => Box::new(StringBox::new(&format!("Error writing to stream: {}", e))),
|
||||
}
|
||||
} else {
|
||||
Box::new(StringBox::new("Error: BufferBox data is not an ArrayBox"))
|
||||
}
|
||||
} else if let Some(string_box) = data.as_any().downcast_ref::<StringBox>() {
|
||||
match self.write(string_box.value.as_bytes()) {
|
||||
|
||||
Reference in New Issue
Block a user