Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Clippy Fixes #526

Merged
merged 12 commits into from
May 22, 2020
6 changes: 3 additions & 3 deletions src/cookies/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::{Arc, RwLock};
pub(crate) struct CookiesMiddleware;

impl CookiesMiddleware {
/// Creates a new CookiesMiddleware.
/// Creates a new `CookiesMiddleware`.
pub fn new() -> Self {
Self::default()
}
Expand Down Expand Up @@ -82,15 +82,15 @@ impl CookieData {
if let Some(cookie_headers) = req.header(&headers::COOKIE) {
for cookie_header in cookie_headers {
// spec says there should be only one, so this is permissive
for pair in cookie_header.as_str().split(";") {
for pair in cookie_header.as_str().split(';') {
if let Ok(cookie) = Cookie::parse_encoded(String::from(pair)) {
jar.add_original(cookie);
}
}
}
}

CookieData {
Self {
content: Arc::new(RwLock::new(jar)),
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ use crate::{Middleware, Request, Response};
/// Ok(String::from("hello"))
/// }
///
/// fn main() {
/// let mut app = tide::Server::new();
/// app.at("/hello").get(hello);
/// }
/// let mut app = tide::Server::new();
/// app.at("/hello").get(hello);
/// ```
///
/// An endpoint with similar functionality that does not make use of the `async` keyword would look something like this:
Expand All @@ -41,10 +39,8 @@ use crate::{Middleware, Request, Response};
/// async_std::future::ready(Ok(String::from("hello")))
/// }
///
/// fn main() {
/// let mut app = tide::Server::new();
/// app.at("/hello").get(hello);
/// }
/// let mut app = tide::Server::new();
/// app.at("/hello").get(hello);
/// ```
///
/// Tide routes will also accept endpoints with `Fn` signatures of this form, but using the `async` keyword has better ergonomics.
Expand Down
11 changes: 5 additions & 6 deletions src/fs/serve_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ impl<State> Endpoint<State> for ServeDir {
Ok(file) => file,
};

let len = match file.metadata().await {
Ok(metadata) => metadata.len() as usize,
Err(_) => {
log::warn!("Could not retrieve metadata");
return Ok(Response::new(StatusCode::InternalServerError));
}
let len = if let Ok(metadata) = file.metadata().await {
metadata.len() as usize
} else {
log::warn!("Could not retrieve metadata");
return Ok(Response::new(StatusCode::InternalServerError));
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
};

let body = Body::from_reader(BufReader::new(file), Some(len));
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub use http_types::{self as http, Body, Error, Status, StatusCode};
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn new() -> server::Server<()> {
Server::new()
}
Expand Down
3 changes: 2 additions & 1 deletion src/log/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use crate::{Middleware, Next, Request};
/// let mut app = tide::Server::new();
/// app.middleware(tide::log::LogMiddleware::new());
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub struct LogMiddleware {
_priv: (),
}

impl LogMiddleware {
/// Create a new instance of `LogMiddleware`.
#[must_use]
pub fn new() -> Self {
Self { _priv: () }
}
Expand Down
1 change: 1 addition & 0 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct Next<'a, State> {

impl<'a, State: 'static> Next<'a, State> {
/// Asynchronously execute the remaining middleware chain.
#[must_use]
pub fn run(mut self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
if let Some((current, next)) = self.next_middleware.split_first() {
self.next_middleware = next;
Expand Down
20 changes: 16 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<State> Request<State> {
state: Arc<State>,
request: http_types::Request,
route_params: Vec<Params>,
) -> Request<State> {
Request {
) -> Self {
Self {
state,
request,
route_params,
Expand All @@ -59,6 +59,7 @@ impl<State> Request<State> {
/// #
/// # Ok(()) })}
/// ```
#[must_use]
pub fn method(&self) -> Method {
self.request.method()
}
Expand All @@ -82,6 +83,7 @@ impl<State> Request<State> {
/// #
/// # Ok(()) })}
/// ```
#[must_use]
pub fn uri(&self) -> &Url {
self.request.url()
}
Expand All @@ -105,6 +107,7 @@ impl<State> Request<State> {
/// #
/// # Ok(()) })}
/// ```
#[must_use]
pub fn version(&self) -> Option<Version> {
self.request.version()
}
Expand All @@ -128,6 +131,7 @@ impl<State> Request<State> {
/// #
/// # Ok(()) })}
/// ```
#[must_use]
pub fn header(
&self,
key: &http_types::headers::HeaderName,
Expand All @@ -136,6 +140,7 @@ impl<State> Request<State> {
}

/// Get a local value.
#[must_use]
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.request.local().get()
}
Expand All @@ -146,6 +151,7 @@ impl<State> Request<State> {
self
}

#[must_use]
/// Access app-global state.
pub fn state(&self) -> &State {
&self.state
Expand All @@ -171,8 +177,7 @@ impl<State> Request<State> {
self.route_params
.iter()
.rev()
.filter_map(|params| params.find(key))
.next()
.find_map(|params| params.find(key))
.unwrap()
.parse()
}
Expand Down Expand Up @@ -293,6 +298,7 @@ impl<State> Request<State> {
}

/// returns a `Cookie` by name of the cookie.
#[must_use]
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
let cookie_data = self
.local::<CookieData>()
Expand All @@ -303,9 +309,15 @@ impl<State> Request<State> {
}

/// Get the length of the body.
#[must_use]
pub fn len(&self) -> Option<usize> {
self.request.len()
}
/// Checks if the body is empty.
#[must_use]
pub fn is_empty(&self) -> Option<bool> {
Some(self.request.len()? == 0)
}
}

impl<State> AsMut<http::Request> for Request<State> {
Expand Down
16 changes: 15 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Response {

impl Response {
/// Create a new instance.
#[must_use]
pub fn new(status: StatusCode) -> Self {
let res = http_types::Response::new(status);
Self {
Expand Down Expand Up @@ -72,22 +73,32 @@ impl Response {
}

/// Returns the statuscode.
#[must_use]
pub fn status(&self) -> crate::StatusCode {
self.res.status()
}

/// Set the statuscode.
#[must_use]
pub fn set_status(mut self, status: crate::StatusCode) -> Self {
self.res.set_status(status);
self
}

/// Get the length of the body.
#[must_use]
pub fn len(&self) -> Option<usize> {
self.res.len()
}

/// Checks if the body is empty.
#[must_use]
pub fn is_empty(&self) -> Option<bool> {
Some(self.res.len()? == 0)
}

/// Get an HTTP header.
#[must_use]
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
self.res.header(name)
}
Expand Down Expand Up @@ -126,6 +137,7 @@ impl Response {
/// Set the request MIME.
///
/// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)
#[must_use]
pub fn set_mime(self, mime: Mime) -> Self {
self.set_header(http_types::headers::CONTENT_TYPE, format!("{}", mime))
}
Expand All @@ -135,6 +147,7 @@ impl Response {
/// # Mime
///
/// The encoding is set to `text/plain; charset=utf-8`.
#[must_use]
pub fn body_string(mut self, string: String) -> Self {
self.res.set_body(string);
self.set_mime(mime::TEXT_PLAIN_UTF_8)
Expand Down Expand Up @@ -167,7 +180,7 @@ impl Response {
pub async fn body_form<T: serde::Serialize>(
mut self,
form: T,
) -> Result<Response, serde_qs::Error> {
) -> Result<Self, serde_qs::Error> {
// TODO: think about how to handle errors
self.res.set_body(serde_qs::to_string(&form)?.into_bytes());
Ok(self
Expand Down Expand Up @@ -221,6 +234,7 @@ impl Response {
}

/// Get a local value.
#[must_use]
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.res.local().get()
}
Expand Down
1 change: 1 addition & 0 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<'a, State: 'static> Route<'a, State> {
}

/// Get the current path.
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
Expand Down
4 changes: 2 additions & 2 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub(crate) struct Selection<'a, State> {
}

impl<State: 'static> Router<State> {
pub(crate) fn new() -> Router<State> {
Router {
pub(crate) fn new() -> Self {
Self {
method_map: HashMap::default(),
all_method_router: MethodRouter::new(),
}
Expand Down
Loading