Skip to content

Commit

Permalink
fix: clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Jan 5, 2024
1 parent 44117c9 commit 333c10b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
7 changes: 3 additions & 4 deletions viz-core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl RequestExt for Request {
where
T: serde::de::DeserializeOwned,
{
<Form as Payload>::check_header(self.content_type(), self.content_length(), None)?;
<Form as Payload>::check_type(self.content_type())?;
let bytes = self.bytes().await?;
serde_urlencoded::from_reader(bytes::Buf::reader(bytes)).map_err(PayloadError::UrlDecode)
}
Expand All @@ -294,15 +294,14 @@ impl RequestExt for Request {
where
T: serde::de::DeserializeOwned,
{
<Json as Payload>::check_header(self.content_type(), self.content_length(), None)?;
<Json as Payload>::check_type(self.content_type())?;
let bytes = self.bytes().await?;
serde_json::from_slice(&bytes).map_err(PayloadError::Json)
}

#[cfg(feature = "multipart")]
async fn multipart(&mut self) -> Result<Multipart, PayloadError> {
let m =
<Multipart as Payload>::check_header(self.content_type(), self.content_length(), None)?;
let m = <Multipart as Payload>::check_type(self.content_type())?;

Check warning on line 304 in viz-core/src/request.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/request.rs#L304

Added line #L304 was not covered by tests

let boundary = m
.get_param(mime::BOUNDARY)
Expand Down
38 changes: 29 additions & 9 deletions viz-core/src/types/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ pub trait Payload {
limit.unwrap_or(Self::LIMIT)
}

/// Detects `Content-Type`
///
/// # Errors
///
/// Will return [`PayloadError::UnsupportedMediaType`] if the detected media type is not supported.
#[inline]
fn check_type(m: Option<mime::Mime>) -> Result<mime::Mime, PayloadError> {
m.filter(Self::detect)
.ok_or_else(|| PayloadError::UnsupportedMediaType(Self::mime()))
}

/// Checks `Content-Length`
///
/// # Errors
///
/// Will return [`PayloadError::TooLarge`] if the detected content length is too large.
#[inline]
fn check_length(len: Option<u64>, limit: Option<u64>) -> Result<(), PayloadError> {
match len {
None => Err(PayloadError::LengthRequired),

Check warning on line 130 in viz-core/src/types/payload.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/types/payload.rs#L130

Added line #L130 was not covered by tests
Some(len) => (len <= Self::limit(limit))
.then_some(())
.ok_or_else(|| PayloadError::TooLarge),
}
}

/// Checks `Content-Type` & `Content-Length`
///
/// # Errors
Expand All @@ -121,14 +147,8 @@ pub trait Payload {
len: Option<u64>,
limit: Option<u64>,
) -> Result<mime::Mime, PayloadError> {
let m = m
.filter(Self::detect)
.ok_or_else(|| PayloadError::UnsupportedMediaType(Self::mime()))?;

match len {
None => Err(PayloadError::LengthRequired),
Some(len) if len > Self::limit(limit) => Err(PayloadError::TooLarge),
Some(_) => Ok(m),
}
let m = Self::check_type(m)?;
Self::check_length(len, limit)?;
Ok(m)
}
}
9 changes: 5 additions & 4 deletions viz-test/tests/payload.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use viz::{types, Error, Request, RequestExt, Response, ResponseExt, Result};

use viz::{types, Error, Request, RequestLimitsExt, Response, ResponseExt, Result};

#[tokio::test]
async fn payload() -> Result<()> {
Expand All @@ -9,15 +10,15 @@ async fn payload() -> Result<()> {

let router = Router::new()
.post("/form", |mut req: Request| async move {
let data = req.form::<HashMap<String, String>>().await?;
let data = req.limited_form::<HashMap<String, String>>().await?;
Ok(Response::json(data))
})
.post("/json", |mut req: Request| async move {
let data = req.json::<HashMap<String, String>>().await?;
let data = req.limited_json::<HashMap<String, String>>().await?;
Ok(Response::json(data))
})
.post("/multipart", |mut req: Request| async move {
let _ = req.multipart().await?;
let _ = req.limited_multipart().await?;
Ok(())
})
.with(
Expand Down
5 changes: 5 additions & 0 deletions viz/src/server/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ pub trait Listener {
fn accept(&self) -> impl Future<Output = Result<(Self::Io, Self::Addr)>> + Send;

/// Returns the local address that this listener is bound to.
///
/// # Errors
///
/// An error will return if got the socket address of the local half of this connection is
/// failed.
fn local_addr(&self) -> Result<Self::Addr>;
}

0 comments on commit 333c10b

Please sign in to comment.