Skip to content

Commit

Permalink
♻️ 移除Response内expect方法
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Dec 7, 2023
1 parent b9580b1 commit 1b6880f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="./static/favicon.png" alt="未知访客" width="200" height="200"></img>
<img src="./static/favicon.png" alt="Oblivion" width="200" height="200"></img>
</div>

<div align="center">
Expand All @@ -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内容
```

### 结构体
Expand Down Expand Up @@ -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,
)
}

// 创建空的初始化路由
Expand Down
6 changes: 5 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 17 additions & 4 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, OblivionException> {
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<Value, serde_json::Error> {
from_str::<Value>(&to_string(&self.content)?)
pub fn json(&mut self) -> Result<Value, OblivionException> {
Ok(from_str::<Value>(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())
}
}

Expand Down

0 comments on commit 1b6880f

Please sign in to comment.