diff options
author | Martin Fischer <martin@push-f.com> | 2021-12-23 07:51:48 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-12-23 23:05:10 +0100 |
commit | 460150079f7bbf868c42d0c3c10119a15e9d6b84 (patch) | |
tree | a7bea00b74670dad4ee663da7cbbb2a77d658496 /examples/form | |
parent | 62fa053b4be22cdedfb4ffeb3045e9fc2d854292 (diff) |
format with cargo fmt
Diffstat (limited to 'examples/form')
-rw-r--r-- | examples/form/src/main.rs | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/examples/form/src/main.rs b/examples/form/src/main.rs index a63560c..af6b2af 100644 --- a/examples/form/src/main.rs +++ b/examples/form/src/main.rs @@ -1,11 +1,11 @@ -use std::convert::Infallible; -use hyper::service::{service_fn, make_service_fn}; -use hyper::{Method, Server, StatusCode, Body}; use hyper::http::request::Parts; use hyper::http::response::Builder; +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Method, Server, StatusCode}; use serde::Deserialize; +use sputnik::hyper_body::{FormError, SputnikBody}; use sputnik::{html_escape, mime, request::SputnikParts, response::SputnikBuilder}; -use sputnik::hyper_body::{SputnikBody, FormError}; +use std::convert::Infallible; type Response = hyper::Response<Body>; @@ -14,7 +14,7 @@ enum Error { #[error("page not found")] NotFound(String), #[error("{0}")] - FormError(#[from] FormError) + FormError(#[from] FormError), } fn render_error(err: Error) -> (StatusCode, String) { @@ -28,33 +28,37 @@ async fn route(req: &mut Parts, body: Body) -> Result<Response, Error> { match (&req.method, req.uri.path()) { (&Method::GET, "/form") => Ok(get_form(req)), (&Method::POST, "/form") => post_form(req, body).await, - _ => return Err(Error::NotFound("page not found".to_owned())) + _ => return Err(Error::NotFound("page not found".to_owned())), } } fn get_form(_req: &mut Parts) -> Response { Builder::new() - .content_type(mime::TEXT_HTML) - .body( - "<form method=post><input name=text> <button>Submit</button></form>".into() - ).unwrap() + .content_type(mime::TEXT_HTML) + .body("<form method=post><input name=text> <button>Submit</button></form>".into()) + .unwrap() } #[derive(Deserialize)] -struct FormData {text: String} +struct FormData { + text: String, +} async fn post_form(_req: &mut Parts, body: Body) -> Result<Response, Error> { let msg: FormData = body.into_form().await?; - Ok(Builder::new().content_type(mime::TEXT_HTML).body( - format!("hello <em>{}</em>", html_escape(msg.text)).into() - ).unwrap()) + Ok(Builder::new() + .content_type(mime::TEXT_HTML) + .body(format!("hello <em>{}</em>", html_escape(msg.text)).into()) + .unwrap()) } -async fn service(req: hyper::Request<hyper::Body>) -> Result<hyper::Response<hyper::Body>, Infallible> { +async fn service( + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, Infallible> { let (mut parts, body) = req.into_parts(); match route(&mut parts, body).await { Ok(mut res) => { - for (k,v) in parts.response_headers().iter() { + for (k, v) in parts.response_headers().iter() { res.headers_mut().append(k, v.clone()); } Ok(res) @@ -62,23 +66,22 @@ async fn service(req: hyper::Request<hyper::Body>) -> Result<hyper::Response<hyp Err(err) => { let (code, message) = render_error(err); // you can easily wrap or log errors here - Ok(hyper::Response::builder().status(code).body(message.into()).unwrap()) + Ok(hyper::Response::builder() + .status(code) + .body(message.into()) + .unwrap()) } } } #[tokio::main] async fn main() { - let service = make_service_fn(move |_| { - async move { - Ok::<_, hyper::Error>(service_fn(move |req| { - service(req) - })) - } + let service = make_service_fn(move |_| async move { + Ok::<_, hyper::Error>(service_fn(move |req| service(req))) }); let addr = ([127, 0, 0, 1], 8000).into(); let server = Server::bind(&addr).serve(service); println!("Listening on http://{}", addr); server.await; -}
\ No newline at end of file +} |