From 36673bd5bf6b7b639c2bbf9fc9383921923e4276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 20 May 2023 19:34:08 +0200 Subject: [PATCH] rustfmt: hard_tabs = true MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- rustfmt.toml | 1 + src/error.rs | 50 +++++++++++++++++++++++++------------------------- src/main.rs | 20 ++++++++++---------- src/web.rs | 40 ++++++++++++++++++++-------------------- 4 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..218e203 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +hard_tabs = true diff --git a/src/error.rs b/src/error.rs index eeba121..1ec5813 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,43 +18,43 @@ use std::str::Utf8Error; #[derive(Debug)] pub(crate) enum Error { - MethodMismatch, - InvalidToken, - InvalidContentType, - Hyper(hyper::Error), - SerdeJson(serde_json::Error), - Utf8(Utf8Error), + MethodMismatch, + InvalidToken, + InvalidContentType, + Hyper(hyper::Error), + SerdeJson(serde_json::Error), + Utf8(Utf8Error), } impl StdError for Error {} impl std::fmt::Display for Error { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - Error::MethodMismatch => write!(fmt, "the method is invalid"), - Error::InvalidToken => write!(fmt, "the token is invalid"), - Error::InvalidContentType => write!(fmt, "the content-type is invalid"), - Error::Hyper(e) => write!(fmt, "hyper error: {}", e), - Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e), - Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e), - } - } + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Error::MethodMismatch => write!(fmt, "the method is invalid"), + Error::InvalidToken => write!(fmt, "the token is invalid"), + Error::InvalidContentType => write!(fmt, "the content-type is invalid"), + Error::Hyper(e) => write!(fmt, "hyper error: {}", e), + Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e), + Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e), + } + } } impl From for Error { - fn from(err: hyper::Error) -> Error { - Error::Hyper(err) - } + fn from(err: hyper::Error) -> Error { + Error::Hyper(err) + } } impl From for Error { - fn from(err: serde_json::Error) -> Error { - Error::SerdeJson(err) - } + fn from(err: serde_json::Error) -> Error { + Error::SerdeJson(err) + } } impl From for Error { - fn from(err: Utf8Error) -> Error { - Error::Utf8(err) - } + fn from(err: Utf8Error) -> Error { + Error::Utf8(err) + } } diff --git a/src/main.rs b/src/main.rs index ad950a2..e9ec8c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,20 +24,20 @@ use std::convert::Infallible; use std::net::SocketAddr; use hyper::{ - service::{make_service_fn, service_fn}, - Server, + service::{make_service_fn, service_fn}, + Server, }; #[tokio::main] async fn main() { - pretty_env_logger::init(); + pretty_env_logger::init(); - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(webhooks)) }); - let server = Server::bind(&addr).serve(make_svc); - println!("Listening on http://{}", addr); + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(webhooks)) }); + let server = Server::bind(&addr).serve(make_svc); + println!("Listening on http://{}", addr); - if let Err(e) = server.await { - eprintln!("Server error: {e}"); - } + if let Err(e) = server.await { + eprintln!("Server error: {e}"); + } } diff --git a/src/web.rs b/src/web.rs index f508fe9..dd34f85 100644 --- a/src/web.rs +++ b/src/web.rs @@ -23,26 +23,26 @@ use hyper::{body, header, Body, Method, Request, Response}; use log::{debug, error}; fn error_res(e: E) -> Result, Infallible> { - error!("error response: {:?}", e); + error!("error response: {:?}", e); - let text = format!("{:?}", e); - let res = Response::builder() - .status(400) - .body(Body::from(Vec::from(text.as_bytes()))) - .unwrap(); - Ok(res) + let text = format!("{:?}", e); + let res = Response::builder() + .status(400) + .body(Body::from(Vec::from(text.as_bytes()))) + .unwrap(); + Ok(res) } async fn webhooks_inner(req: Request) -> Result, Error> { - match req.method() { - &Method::POST => (), - _ => return Err(Error::MethodMismatch), - } + match req.method() { + &Method::POST => (), + _ => return Err(Error::MethodMismatch), + } - debug!("Headers: {:?}", req.headers()); + debug!("Headers: {:?}", req.headers()); - let headers = req.headers(); - if let Some(content_type) = headers.get(header::CONTENT_TYPE) && + let headers = req.headers(); + if let Some(content_type) = headers.get(header::CONTENT_TYPE) && let Some(token) = headers.get("X-Gitlab-Token") { if content_type != "application/json" { return Err(Error::InvalidContentType); @@ -53,14 +53,14 @@ async fn webhooks_inner(req: Request) -> Result, Error> { } } - let tmp = body::to_bytes(req.into_body()).await?; - let text: &str = from_utf8(&tmp)?; - let json: WebHook = serde_json::from_str(text)?; - debug!("Passed: {:?}", json); + let tmp = body::to_bytes(req.into_body()).await?; + let text: &str = from_utf8(&tmp)?; + let json: WebHook = serde_json::from_str(text)?; + debug!("Passed: {:?}", json); - Ok(Response::new("Hello world".into())) + Ok(Response::new("Hello world".into())) } pub async fn webhooks(req: Request) -> Result, Infallible> { - webhooks_inner(req).await.or_else(error_res) + webhooks_inner(req).await.or_else(error_res) }