Skip to content

Commit 2734f08

Browse files
committed
refactorred roa-body
1 parent c6479a0 commit 2734f08

File tree

10 files changed

+117
-394
lines changed

10 files changed

+117
-394
lines changed

roa-body/Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "roa-body"
3-
version = "0.5.0-beta.1"
3+
version = "0.5.0-beta.2"
44
authors = ["Hexilee <i@hexilee.me>"]
55
edition = "2018"
66
license = "MIT"
@@ -25,15 +25,13 @@ codecov = { repository = "Hexilee/roa" }
2525
log = "0.4"
2626
futures = "0.3"
2727
async-std = "1.5.0"
28-
mime = "0.3"
2928
bytes = "0.5.4"
30-
29+
lazy_static = "1.4"
3130
roa-core = { path = "../roa-core", version = "0.5.0-beta" }
3231
serde = { version = "1", optional = true }
3332
askama = { version = "0.9", optional = true }
3433
serde_json = { version = "1.0", optional = true }
3534
serde_urlencoded = { version = "0.6", optional = true }
36-
encoding = { version = "0.2", optional = true }
3735
mime_guess = { version = "2.0", optional = true }
3836
percent-encoding = { version = "2.1.0", optional = true }
3937

@@ -43,11 +41,12 @@ tokio = { version = "0.2", features = ["full"] }
4341
reqwest = { version = "0.10", features = ["json", "cookies", "gzip"] }
4442
roa-tcp = { path = "../roa-tcp", version = "0.5.0-beta" }
4543
roa-core = { path = "../roa-core", version = "0.5.0-beta", features = ["runtime"] }
44+
mime = "0.3"
4645

4746
[features]
4847
default = []
4948
full = ["json", "urlencoded", "file", "template"]
50-
json = ["serde", "serde_json", "encoding"]
49+
json = ["serde", "serde_json"]
5150
urlencoded = ["serde", "serde_urlencoded"]
5251
file = ["mime_guess", "percent-encoding"]
5352
template = ["askama"]

roa-body/src/content_type.rs

Lines changed: 0 additions & 88 deletions
This file was deleted.

roa-body/src/decode.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

roa-body/src/file.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod content_disposition;
2-
use crate::content_type::ContentType;
3-
use async_std::fs::File;
2+
mod help;
3+
44
pub use async_std::path::Path;
5-
use content_disposition::ContentDisposition;
65
pub use content_disposition::DispositionType;
6+
7+
use async_std::fs::File;
8+
use content_disposition::ContentDisposition;
79
use roa_core::{http, Context, Result, State};
810

911
#[inline]
@@ -18,8 +20,11 @@ pub async fn write_file<S: State>(
1820
if let Some(filename) = path.file_name() {
1921
ctx.resp_mut().headers.insert(
2022
http::header::CONTENT_TYPE,
21-
ContentType(mime_guess::from_path(&filename).first_or_octet_stream())
22-
.to_value()?,
23+
mime_guess::from_path(&filename)
24+
.first_or_octet_stream()
25+
.as_ref()
26+
.parse()
27+
.map_err(help::bug_report)?,
2328
);
2429

2530
let name = filename.to_string_lossy();

roa-body/src/file/content_disposition.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::help::bug_report;
1+
use super::help::bug_report;
22
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
33
use roa_core::http::header::HeaderValue;
44
use roa_core::Result;
@@ -44,6 +44,7 @@ pub struct ContentDisposition {
4444
}
4545

4646
impl ContentDisposition {
47+
#[inline]
4748
pub(crate) fn new(typ: DispositionType, filename: Option<&str>) -> Self {
4849
Self {
4950
typ,
@@ -52,6 +53,7 @@ impl ContentDisposition {
5253
}
5354
}
5455

56+
#[inline]
5557
pub fn value(&self) -> Result<HeaderValue> {
5658
let value_str = self.to_string();
5759
value_str.parse().map_err(|err| {
@@ -64,6 +66,7 @@ impl ContentDisposition {
6466
}
6567

6668
impl Display for ContentDisposition {
69+
#[inline]
6770
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
6871
match &self.encoded_filename {
6972
None => f.write_fmt(format_args!("{}", self.typ)),
@@ -76,6 +79,7 @@ impl Display for ContentDisposition {
7679
}
7780

7881
impl Display for DispositionType {
82+
#[inline]
7983
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
8084
match self {
8185
DispositionType::Inline => f.write_str("inline"),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use roa_core::Error;
44
const BUG_HELP: &str =
55
r"This is a bug, please report it to https://github.com/Hexilee/roa.";
66

7+
#[inline]
78
pub fn bug_report(message: impl ToString) -> Error {
89
Error::new(
910
StatusCode::INTERNAL_SERVER_ERROR,

roa-body/src/json.rs

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)