From 1b6880fcbecdd6b7f95b11116be0b4fb089dac30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= Date: Thu, 7 Dec 2023 23:23:00 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E7=A7=BB=E9=99=A4Response=E5=86=85?= =?UTF-8?q?expect=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++++----- src/bin/main.rs | 6 +++++- src/models/client.rs | 21 +++++++++++++++++---- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index dc4f338..932f29f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@
- 未知访客 + Oblivion
@@ -26,8 +26,8 @@ use oblivion::api::post; use oblivion::api::put; use oblivion::api::forward; // 弃用 -let req = get("127.0.0.1:813/path", true).await.unwrap(); // 返回一个Response结构体 -println!("{}", req.text()); // 输出GET内容 +let req = get("127.0.0.1:813/test1", true).await?; // 返回一个Response结构体 +println!("{}", req.text()?); // 输出GET内容 ``` ### 结构体 @@ -74,8 +74,12 @@ use oblivion::models::server; use oblivion::models::render; use oblivion::models::router; -fn test(_: &mut OblivionRequest) -> BaseResponse { - BaseResponse::TextResponse("每一个自然人都应该拥有守护信息与获得真实信息的神圣权利".to_string(), 200) +fn test2(_: &mut OblivionRequest) -> BaseResponse { + BaseResponse::TextResponse( + "每一个人都应该拥有守护信息与获得真实信息的神圣权利, 任何与之对抗的都是我们的敌人" + .to_string(), + 200, + ) } // 创建空的初始化路由 diff --git a/src/bin/main.rs b/src/bin/main.rs index da91c0f..ec59818 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -9,7 +9,11 @@ use std::env::args; use std::time::Instant; fn test2(_: &mut OblivionRequest) -> BaseResponse { - BaseResponse::TextResponse("毁灭人类!!!!".to_string(), 200) + BaseResponse::TextResponse( + "每一个人都应该拥有守护信息与获得真实信息的神圣权利, 任何与之对抗的都是我们的敌人" + .to_string(), + 200, + ) } #[tokio::main] diff --git a/src/models/client.rs b/src/models/client.rs index a96d9bb..dd15f48 100644 --- a/src/models/client.rs +++ b/src/models/client.rs @@ -39,12 +39,25 @@ impl Response { self.status_code < 400 } - pub fn text(&mut self) -> String { - String::from_utf8(self.content.to_vec()).expect("Unable to decode.") + pub fn text(&mut self) -> Result { + match String::from_utf8(self.content.to_vec()) { + Ok(text) => Ok(text), + Err(_) => Err(OblivionException::InvalidOblivion(Some( + "Decode error occured when decode text from uft8.".to_string(), + ))), + } } - pub fn json(&mut self) -> Result { - from_str::(&to_string(&self.content)?) + pub fn json(&mut self) -> Result { + Ok(from_str::(match &to_string(&self.content) { + Ok(string) => string, + Err(_) => { + return Err(OblivionException::InvalidOblivion(Some( + "Decode error occured when serialize bytes as json.".to_string(), + ))) + } + }) + .unwrap()) } }