From edc9344b74319cd5d35ef5fb14fdb971dca6dfa1 Mon Sep 17 00:00:00 2001 From: Pass Automated Testing Suite Date: Mon, 18 Oct 2021 13:07:45 +0200 Subject: [PATCH] refactor: GcdParameters impl fmt::Display and gcd. Index html now static axum and actix-web now returns GcdParameters' fmt::Display. They also serve the same static html. gcd function is now part of GcdParameters as a method. --- src/main.rs | 107 +++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/src/main.rs b/src/main.rs index dd41841..a20f7e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use actix_web::{web, App, HttpResponse, HttpServer}; use serde::Deserialize; +use std::fmt; #[derive(Deserialize, Debug)] struct GcdParameters { n: u64, @@ -17,9 +18,37 @@ struct GcdParameters { } #[cfg(axum)] +impl GcdParameters { + fn gcd(&self) -> u64 { + assert!(self.n != 0 && self.m != 0); + let mut m = self.m; + let mut n = self.n; + while m != 0 { + if m < n { + std::mem::swap(&mut m, &mut n); + } + m %= n; + } + n + } +} + +impl fmt::Display for GcdParameters { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "The greatest common divisor of the numbers {} and {} is {}", + self.n, + self.m, + self.gcd(), + ) + } +} #[tokio::main] async fn main() { - let app = Router::new().route("/", get(get_index).post(post_gcd)); + let app = Router::new() + .route("/", get(get_index)) + .route("/gcd", post(post_gcd)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum::Server::bind(&addr) @@ -45,37 +74,14 @@ fn main() { #[cfg(axum)] async fn get_index() -> Html<&'static str> { - Html( - r#" - - - - GCD Calculator - - -
- - - -
- - - "#, - ) + Html(INDEX_HTML) } #[cfg(actix)] fn get_index() -> HttpResponse { - HttpResponse::Ok().content_type("text/html").body( - r#" - GCD Calculator -
- - - -
- "#, - ) + HttpResponse::Ok() + .content_type("text/html") + .body(INDEX_HTML) } #[cfg(axum)] @@ -88,38 +94,35 @@ async fn post_gcd( "Computing the GCD with zero is boring", )); } - let response = format!( - "The greatest common divisor of the numbers {} and {} is {}.", - input.n, - input.m, - gcd(input.n, input.m) - ); - Ok(Html(response)) + Ok(Html(input.to_string())) } #[cfg(actix)] fn post_gcd(form: web::Form) -> HttpResponse { if form.n == 0 || form.m == 0 { +fn post_gcd(input: web::Form) -> HttpResponse { + if input.n == 0 || input.m == 0 { return HttpResponse::BadRequest() .content_type("text/html") .body("Computing the GCD with zero is boring"); } - let response = format!( - "The greatest common divisor of the numbers {} and {} is {}", - form.n, - form.m, - gcd(form.n, form.m) - ); - HttpResponse::Ok().content_type("text/html").body(response) + HttpResponse::Ok() + .content_type("text/html") + .body(input.to_string()) } -fn gcd(mut n: u64, mut m: u64) -> u64 { - assert!(n != 0 && m != 0); - while m != 0 { - if m < n { - std::mem::swap(&mut m, &mut n); - } - m %= n; - } - n -} +static INDEX_HTML: &str = r#" + + + + GCD Calculator + + +
+ + + +
+ + +"#; -- 2.20.1