From cef45e43a6885b05a2873b7ac7bffdf5e12b40ee Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 25 Jan 2023 07:49:21 +0100 Subject: [PATCH] refactor --- Cargo.lock | 86 ++++++++++++++++++++++---------------------- src/config.rs | 26 +++++++------- src/main.rs | 4 +-- src/msg.rs | 3 +- src/pages/html_fn.rs | 6 ++-- src/pages/index.rs | 40 +++++++++------------ 6 files changed, 79 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 832281b..d74c851 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,9 +44,9 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.2.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c83abf9903e1f0ad9973cc4f7b9767fd5a03a583f51a5b7a339e07987cd2724" +checksum = "0070905b2c4a98d184c4e81025253cb192aa8a73827553f38e9410801ceb35bb" dependencies = [ "actix-codec", "actix-rt", @@ -75,6 +75,8 @@ dependencies = [ "rand", "sha1", "smallvec", + "tokio", + "tokio-util", "tracing", "zstd", ] @@ -104,9 +106,9 @@ dependencies = [ [[package]] name = "actix-rt" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ea16c295198e958ef31930a6ef37d0fb64e9ca3b6116e6b93a8bdae96ee1000" +checksum = "15265b6b8e2347670eb363c47fc8c75208b4a4994b27192f345fcbe707804f3e" dependencies = [ "futures-core", "tokio", @@ -114,9 +116,9 @@ dependencies = [ [[package]] name = "actix-server" -version = "2.1.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0da34f8e659ea1b077bb4637948b815cd3768ad5a188fdcd74ff4d84240cd824" +checksum = "3e8613a75dd50cc45f473cee3c34d59ed677c0f7b44480ce3b8247d7dc519327" dependencies = [ "actix-rt", "actix-service", @@ -153,9 +155,9 @@ dependencies = [ [[package]] name = "actix-web" -version = "4.2.1" +version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d48f7b6534e06c7bfc72ee91db7917d4af6afe23e7d223b51e68fffbb21e96b9" +checksum = "464e0fddc668ede5f26ec1f9557a8d44eda948732f40c6b0ad79126930eb775f" dependencies = [ "actix-codec", "actix-http", @@ -279,9 +281,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "base64" -version = "0.13.1" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" [[package]] name = "bitflags" @@ -311,9 +313,9 @@ dependencies = [ [[package]] name = "brotli-decompressor" -version = "2.3.2" +version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ad2d4653bf5ca36ae797b1f4bb4dbddb60ce49ca4aed8a2ce4829f60425b80" +checksum = "4b6561fd3f895a11e8f72af2cb7d22e08366bebc2b6b57f7744c4bda27034744" dependencies = [ "alloc-no-stdlib", "alloc-stdlib", @@ -321,9 +323,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.11.1" +version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" [[package]] name = "bytes" @@ -443,9 +445,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579" +checksum = "b61a7545f753a88bcbe0a70de1fcc0221e10bfc752f576754fa91e663db1622e" dependencies = [ "cc", "cxxbridge-flags", @@ -455,9 +457,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70" +checksum = "f464457d494b5ed6905c63b0c4704842aba319084a0a3561cdc1359536b53200" dependencies = [ "cc", "codespan-reporting", @@ -470,15 +472,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c" +checksum = "43c7119ce3a3701ed81aca8410b9acf6fc399d2629d057b87e2efa4e63a3aaea" [[package]] name = "cxxbridge-macro" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5" +checksum = "65e07508b90551e610910fa648a1878991d367064997a596135b86df30daf07e" dependencies = [ "proc-macro2", "quote", @@ -642,7 +644,7 @@ dependencies = [ [[package]] name = "gnupg" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/gnupg-rs#6d7729cd72aa95aa221882633100b6017754339f" +source = "git+https://git.hydrar.de/jmarya/gnupg-rs#3b57d7879b8bb676ffc860feac433611230118f6" [[package]] name = "h2" @@ -1188,9 +1190,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" dependencies = [ "unicode-ident", ] @@ -1271,9 +1273,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.13" +version = "0.11.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" +checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9" dependencies = [ "base64", "bytes", @@ -1344,9 +1346,9 @@ checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" [[package]] name = "security-framework" -version = "2.7.0" +version = "2.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c" +checksum = "7c4437699b6d34972de58652c68b98cb5b53a4199ab126db8e20ec8ded29a721" dependencies = [ "bitflags", "core-foundation", @@ -1357,9 +1359,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.6.1" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" +checksum = "31c9bb296072e961fcbd8853511dd39c2d8be2deb1e17c6860b1d30732b323b4" dependencies = [ "core-foundation-sys", "libc", @@ -1486,9 +1488,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.1.3" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" dependencies = [ "winapi-util", ] @@ -1548,9 +1550,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.24.1" +version = "1.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d9f76183f91ecfb55e1d7d5602bd1d979e38a3a522fe900241cf195624d67ae" +checksum = "597a12a59981d9e3c38d216785b0c37399f6e415e8d0712047620f189371b0bb" dependencies = [ "autocfg", "bytes", @@ -1638,9 +1640,9 @@ dependencies = [ [[package]] name = "unicode-bidi" -version = "0.3.8" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58" [[package]] name = "unicode-ident" @@ -1799,7 +1801,7 @@ checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" [[package]] name = "web-base" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/web-base#bbc19aab0ed51e4bda52a0c7935e9e328ed00323" +source = "git+https://git.hydrar.de/jmarya/web-base#4ac976ed197f9feefa577e02c0f9c810f0623ee2" dependencies = [ "actix-files", "actix-web", @@ -1918,18 +1920,18 @@ dependencies = [ [[package]] name = "zstd" -version = "0.11.2+zstd.1.5.2" +version = "0.12.2+zstd.1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4" +checksum = "e9262a83dc741c0b0ffec209881b45dbc232c21b02a2b9cb1adb93266e41303d" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "5.0.2+zstd.1.5.2" +version = "6.0.2+zstd.1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db" +checksum = "a6cf39f730b440bab43da8fb5faf5f254574462f73f260f85f7987f32154ff17" dependencies = [ "libc", "zstd-sys", diff --git a/src/config.rs b/src/config.rs index 3786ec8..c37a711 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,7 @@ pub struct Config { } fn read_json_file(f: &str) -> Option { - return serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()?; + serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()? } impl Config { @@ -24,23 +24,23 @@ impl Config { } pub fn name(&self) -> Option { - return Option::from(self.root.get("name")?.as_str()?.to_string()); + Option::from(self.root.get("name")?.as_str()?.to_string()) } pub fn email(&self) -> Option { - return Option::from(self.root.get("email")?.as_str()?.to_string()); + Option::from(self.root.get("email")?.as_str()?.to_string()) } pub fn xmr_address(&self) -> Option { - return Option::from(self.root.get("xmr_address")?.as_str()?.to_string()); + Option::from(self.root.get("xmr_address")?.as_str()?.to_string()) } fn color_n_fg(&self) -> Option { - return Some(self.root.get("colors")?.get("fg")?.as_i64()?); + self.root.get("colors")?.get("fg")?.as_i64() } fn color_n_bg(&self) -> Option { - return Some(self.root.get("colors")?.get("bg")?.as_i64()?); + self.root.get("colors")?.get("bg")?.as_i64() } pub fn fg_color(&self) -> Option { @@ -48,12 +48,12 @@ impl Config { let fg = col.get("special")?.get("foreground")?.as_str()?; if let Some(fg_n) = self.color_n_fg() { let n = fg_n - 1; - let fg = col.get("colors")?.get(format!("color{}", n))?.as_str()?; + let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?; return Some(fg.to_string()); } - return Some(fg.to_string()); + Some(fg.to_string()) } else { - return None; + None } } @@ -62,12 +62,12 @@ impl Config { let fg = col.get("special")?.get("background")?.as_str()?; if let Some(bg_n) = self.color_n_bg() { let n = bg_n - 1; - let fg = col.get("colors")?.get(format!("color{}", n))?.as_str()?; + let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?; return Some(fg.to_string()); } - return Some(fg.to_string()); + Some(fg.to_string()) } else { - return None; + None } } @@ -75,6 +75,6 @@ impl Config { let settings = self.root.get("notify")?.get("gotify")?; let host = settings.get("host")?.as_str()?.to_string(); let token = settings.get("token")?.as_str()?.to_string(); - return Some(GotifySettings { host, token }); + Some(GotifySettings { host, token }) } } diff --git a/src/main.rs b/src/main.rs index 3f47de6..296bd22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,7 @@ async fn main() -> std::io::Result<()> { .service(pages::index::message_page) .service(pages::index::message_post); - let app = web_base::assign_pages(csrf.clone(), app); - - app + web_base::assign_pages(csrf.clone(), app) }) .bind(("0.0.0.0", 8080))? .run() diff --git a/src/msg.rs b/src/msg.rs index e49adf0..e57b1b6 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -5,8 +5,7 @@ pub fn encrypt(msg: String) -> String { let pub_key = pgp .import_key(&std::fs::read_to_string("./config/pub.key").unwrap()) .unwrap(); - let c = pgp.encrypt(&pub_key, &msg).unwrap(); - return c; + pgp.encrypt(&pub_key, &msg).unwrap() } pub fn save_msg(msg: String, name: &str) { diff --git a/src/pages/html_fn.rs b/src/pages/html_fn.rs index f6827fa..c9d51dc 100644 --- a/src/pages/html_fn.rs +++ b/src/pages/html_fn.rs @@ -10,7 +10,7 @@ pub(crate) async fn build_site( shadow: bool, config: &Data, ) -> HttpResponse { - let BOOTSTRAP = html! { + let bootstrap = html! { link href="/bootstrap.min.css" rel="stylesheet"; link href="/bootstrap-icons.css" rel="stylesheet"; link href="/bootstrap.bundle.min.js" rel="stylesheet"; @@ -40,7 +40,7 @@ pub(crate) async fn build_site( title { (title) }; - (BOOTSTRAP) + (bootstrap) }; body style=(c_style) class=(c_class) { style { (g_style) }; @@ -49,5 +49,5 @@ pub(crate) async fn build_site( }; }; - return HttpResponse::Ok().message_body(r.into_string()).unwrap(); + HttpResponse::Ok().message_body(r.into_string()).unwrap() } diff --git a/src/pages/index.rs b/src/pages/index.rs index f6bc093..fdee71f 100644 --- a/src/pages/index.rs +++ b/src/pages/index.rs @@ -16,18 +16,17 @@ pub async fn message_post(r: HttpRequest, f: Form) -> impl Responde let config: &web::Data = r.app_data().unwrap(); crate::msg::save_msg(f.message.clone(), &f.msg_name.to_string()); crate::notification::notify( - &format!("New Message from {}", f.msg_name.to_string()), + &format!("New Message from {}", f.msg_name), "New Message", config.clone(), ) .await; - return web_base::func::redirect("/message"); + web_base::func::redirect("/message") } #[get("/message")] pub async fn message_page(r: HttpRequest) -> impl Responder { let config: &web::Data = r.app_data().unwrap(); - let host = web_base::func::get_host(&r); let resp = html! { div class="container" style="margin-top: 25px" { @@ -42,7 +41,7 @@ pub async fn message_page(r: HttpRequest) -> impl Responder { } }; - return pages::html_fn::build_site(resp.into_string(), "Message", false, true, config).await; + pages::html_fn::build_site(resp.into_string(), "Message", false, true, config).await } #[get("/mirrors.txt")] @@ -61,13 +60,11 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder { return pages::html_fn::build_site(resp.into_string(), "Mirrors", false, true, config) .await; } - let res: HttpResponse = HttpResponse::Ok().message_body(content).unwrap(); - return res; + return HttpResponse::Ok().message_body(content).unwrap(); } - let res: HttpResponse = HttpResponse::NotFound() + HttpResponse::NotFound() .message_body("".to_string()) - .unwrap(); - return res; + .unwrap() } #[get("/public_key")] @@ -80,7 +77,7 @@ pub async fn public_key(r: HttpRequest) -> impl Responder { let pgp = gnupg::GnuPG::new().unwrap(); let key_name = pgp.import_key(&key).unwrap().name; - let key = key.replace("\n", "
"); + let key = key.replace('\n', "
"); let resp = html! { div class="container" style="margin-top: 25px" { @@ -100,23 +97,21 @@ pub async fn public_key(r: HttpRequest) -> impl Responder { } if let Ok(key_f) = std::fs::File::open("./config/pub.key") { if let Ok(key_data) = std::io::read_to_string(key_f) { - let res: HttpResponse = HttpResponse::Ok() + return HttpResponse::Ok() .insert_header(header::ContentType::plaintext()) .message_body(key_data) .unwrap(); - return res; } } - let res: HttpResponse = HttpResponse::NotFound() + HttpResponse::NotFound() .message_body("".to_string()) - .unwrap(); - return res; + .unwrap() } fn build_information_block(conf: &web::Data) -> String { let name = conf.name().unwrap(); - return html! { + html! { div class="container border-dark" style="margin-top: 20px" { img src="/assets/me" height="200" width="200" alt="Me" class="rounded"; br;br; @@ -124,7 +119,7 @@ fn build_information_block(conf: &web::Data) -> String { hr; } } - .into_string(); + .into_string() } fn build_contact_block(conf: &web::Data) -> String { @@ -140,7 +135,7 @@ fn build_contact_block(conf: &web::Data) -> String { false => "".to_string(), }; - return html! { + html! { div class="container border-dark" { h1 { span class="bi bi-person-lines-fill" style="vertical-align: middle;"; @@ -152,9 +147,9 @@ fn build_contact_block(conf: &web::Data) -> String { hr; } } - .into_string(); + .into_string() } else { - return "".to_string(); + "".to_string() } } @@ -175,7 +170,7 @@ fn build_donation_block(conf: &web::Data) -> String { } .into_string() } else { - return "".to_string(); + "".to_string() } } @@ -191,6 +186,5 @@ pub(crate) async fn index(conf: web::Data) -> impl Responder { {donation_block} " ); - let r = crate::pages::html_fn::build_site(content, "About Me", false, true, &conf).await; - return r; + crate::pages::html_fn::build_site(content, "About Me", false, true, &conf).await }