Skip to content

Commit

Permalink
Merge pull request #52 from hubertshelley/refactor/response_fn
Browse files Browse the repository at this point in the history
refactor: add fn text,html,json for response
  • Loading branch information
hubertshelley authored Jul 9, 2024
2 parents c8fec85 + 54e8451 commit 82b5f44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
55 changes: 33 additions & 22 deletions silent/src/core/response.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use std::fmt;
use std::fmt::{Display, Formatter};

use bytes::Bytes;
use crate::core::res_body::{full, ResBody};
use crate::headers::{ContentType, Header, HeaderMap, HeaderMapExt};
use crate::{header, Configs, Result, SilentError, StatusCode};
#[cfg(feature = "cookie")]
use cookie::{Cookie, CookieJar};
use http::{Extensions, Version};
use http_body::{Body, SizeHint};
use serde::Serialize;
use serde_json::Value;

use crate::core::res_body::{full, ResBody};
use crate::headers::{ContentType, Header, HeaderMap, HeaderMapExt};
use crate::{header, Configs, Result, SilentError, StatusCode};

/// 响应体
/// ```
/// use silent::Response;
Expand Down Expand Up @@ -76,6 +74,30 @@ impl Response {
);
Ok(res)
}
#[inline]
/// 生成文本响应
pub fn text(text: &str) -> Self {
let mut res = Self::empty();
res.set_typed_header(ContentType::text_utf8());
res.set_body(full(text.as_bytes().to_vec()));
res
}
#[inline]
/// 生成html响应
pub fn html(html: &str) -> Self {
let mut res = Self::empty();
res.set_typed_header(ContentType::html());
res.set_body(full(html.as_bytes().to_vec()));
res
}
#[inline]
/// 生成json响应
pub fn json<T: Serialize>(json: &T) -> Self {
let mut res = Self::empty();
res.set_typed_header(ContentType::json());
res.set_body(full(serde_json::to_vec(json).unwrap()));
res
}
}

impl<B: Body> Response<B> {
Expand Down Expand Up @@ -230,22 +252,11 @@ impl<B: Body> Response<B> {

impl<S: Serialize> From<S> for Response {
fn from(value: S) -> Self {
let mut res = Response::empty();
let result: Bytes = match serde_json::to_value(&value).unwrap() {
Value::String(value) => {
if value.contains("html") {
res.set_typed_header(ContentType::html());
} else {
res.set_typed_header(ContentType::text_utf8());
}
value.into_bytes().into()
}
_ => {
res.set_typed_header(ContentType::json());
serde_json::to_vec(&value).unwrap().into()
}
};
res.set_body(full(result));
res
match serde_json::to_value(&value).unwrap() {
Value::String(value) => Response::empty()
.with_typed_header(ContentType::json())
.with_body(full(value.as_bytes().to_vec())),
_ => Self::json(&value),
}
}
}
2 changes: 0 additions & 2 deletions silent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ mod handler;
mod log;
pub mod middleware;
mod route;
#[cfg(feature = "server")]
mod rt;
#[cfg(feature = "scheduler")]
mod scheduler;
#[cfg(feature = "security")]
Expand Down
13 changes: 0 additions & 13 deletions silent/src/rt/mod.rs

This file was deleted.

0 comments on commit 82b5f44

Please sign in to comment.