From 02434d80a2d1fe23fdaf065ea448bd158a797928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Tue, 19 Sep 2023 18:07:30 +0700 Subject: [PATCH] Shorter code --- Cargo.lock | 25 ++++++++++++++++++++-- Cargo.toml | 2 +- src/api/structs.rs | 53 +++++++++++++++++++++++----------------------- src/types/ext.rs | 10 +++++++++ 4 files changed, 61 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf332c8..67ed6f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -333,6 +333,27 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-extra" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ab90e7b70bea63a153137162affb6a0bce26b584c24a4c7885509783e2cf30b" +dependencies = [ + "axum", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "pin-project-lite", + "serde", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + [[package]] name = "axum-login" version = "0.6.0" @@ -376,7 +397,7 @@ checksum = "714cad544cd87d8da821cda715bb9aaa5d4d1adbdb64c549b18138e3cbf93c44" dependencies = [ "async-session", "axum", - "axum-extra", + "axum-extra 0.7.5", "futures", "http-body", "tokio", @@ -2582,7 +2603,7 @@ dependencies = [ "async-trait", "atom_syndication", "axum", - "axum-extra", + "axum-extra 0.8.0", "axum-login", "axum-sessions", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 50c56f0..d13b9c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ async-fred-session = "0.1.5" async-trait = "0.1.71" atom_syndication = { version = "0.12.2", features = ["serde"] } axum = { version = "0.6.19", features = ["tracing", "json", "tower-log", "macros", "tokio", "headers", "query", "original-uri"] } -axum-extra = { version = "0.7.5" } +axum-extra = { version = "0.8.0" } axum-login = "0.6.0" axum-sessions = "0.5.0" chrono = { version = "0.4.26", features = ["serde", "alloc", "clock"], default-features = false } diff --git a/src/api/structs.rs b/src/api/structs.rs index ec792f7..39d6fe4 100644 --- a/src/api/structs.rs +++ b/src/api/structs.rs @@ -9,6 +9,7 @@ use validify::Validify; use super::macros::append_set_statement; use crate::models::DocFormat; +use crate::types::ext::VecExt; use crate::types::conversions::{edge_object_from_pairs, edge_object_from_simple_pairs}; use crate::utils::markdown::{make_excerpt, markdown_to_html}; @@ -69,7 +70,7 @@ impl BlogPostPatchData { append_set_statement!("slug", "optional str", lines, submitted_fields); append_set_statement!("is_published", "optional bool", lines, submitted_fields); append_set_statement!("format", "optional DocFormat", lines, submitted_fields); - if submitted_fields.iter().any(|&f| f == "body") { + if submitted_fields.contains("body") { // If user submitted "body" field, we will generate "html", "excerpt" and write, too lines.push("body := $body"); lines.push("html := $html"); @@ -78,7 +79,7 @@ impl BlogPostPatchData { append_set_statement!("locale", "optional str", lines, submitted_fields); append_set_statement!("author", "optional User", lines, submitted_fields); append_set_statement!("og_image", "optional str", lines, submitted_fields); - if submitted_fields.iter().any(|&f| f == "categories") && self.categories.is_some() { + if submitted_fields.contains("categories") && self.categories.is_some() { let line = "categories := ( SELECT BlogCategory FILTER .id IN array_unpack(>$categories) )"; @@ -91,25 +92,25 @@ impl BlogPostPatchData { let mut pairs = indexmap! { "id" => (Some(EValue::Uuid(post_id)), Cd::One), }; - if submitted_fields.iter().any(|&f| f == "title") { + if submitted_fields.contains("title") { pairs.insert( "title", (self.title.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "slug") { + if submitted_fields.contains("slug") { pairs.insert( "slug", (self.slug.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "is_published") { + if submitted_fields.contains("is_published") { pairs.insert( "is_published", (self.is_published.map(EValue::Bool), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "format") { + if submitted_fields.contains("format") { pairs.insert( "format", ( @@ -118,7 +119,7 @@ impl BlogPostPatchData { ), ); } - if submitted_fields.iter().any(|&f| f == "body") { + if submitted_fields.contains("body") { let body = self.body.clone(); let html = body.as_ref().map(|b| markdown_to_html(b)); let excerpt = body.as_ref().map(|b| make_excerpt(b)); @@ -129,19 +130,19 @@ impl BlogPostPatchData { (excerpt.map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "locale") { + if submitted_fields.contains("locale") { pairs.insert( "locale", (self.locale.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "author") { + if submitted_fields.contains("author") { pairs.insert( "author", (self.author.map(EValue::Uuid), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "og_image") { + if submitted_fields.contains("og_image") { pairs.insert( "og_image", (self.og_image.clone().map(EValue::Str), Cd::AtMostOne), @@ -178,7 +179,7 @@ impl BlogPostCreateData { pub fn gen_set_clause<'a>(&self, submitted_fields: &Vec<&String>) -> String { let mut lines = vec!["title := $title", "slug := $slug"]; append_set_statement!("is_published", "optional bool", lines, submitted_fields); - if submitted_fields.iter().any(|&f| f == "body") { + if submitted_fields.contains("body") { // If user submitted "body" field, we will generate "html", "excerpt" and write, too lines.push("body := $body"); lines.push("html := $html"); @@ -203,13 +204,13 @@ impl BlogPostCreateData { "title" => (Some(EValue::Str(self.title.clone())), Cd::One), "slug" => (Some(EValue::Str(self.slug.clone())), Cd::One), }; - if submitted_fields.iter().any(|&f| f == "is_published") { + if submitted_fields.contains("is_published") { pairs.insert( "is_published", (self.is_published.map(EValue::Bool), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "body") { + if submitted_fields.contains("body") { let body = self.body.clone(); let html = body.as_ref().map(|v| markdown_to_html(v)); let excerpt = body.as_ref().map(|v| make_excerpt(v)); @@ -220,7 +221,7 @@ impl BlogPostCreateData { (excerpt.map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "format") { + if submitted_fields.contains("format") { pairs.insert( "format", ( @@ -229,19 +230,19 @@ impl BlogPostCreateData { ), ); } - if submitted_fields.iter().any(|&f| f == "locale") { + if submitted_fields.contains("locale") { pairs.insert( "locale", (self.locale.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "author") { + if submitted_fields.contains("author") { pairs.insert( "author", (self.author.map(EValue::Uuid), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "og_image") { + if submitted_fields.contains("og_image") { pairs.insert( "og_image", (self.og_image.clone().map(EValue::Str), Cd::AtMostOne), @@ -277,13 +278,13 @@ impl BlogCategoryPatchData { let mut pairs = indexmap!( "id" => (Some(EValue::Uuid(id)), Cd::One), ); - if submitted_fields.iter().any(|&f| f == "title") { + if submitted_fields.contains("title") { pairs.insert( "title", (self.title.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "slug") { + if submitted_fields.contains("slug") { pairs.insert( "slug", (self.slug.clone().map(EValue::Str), Cd::AtMostOne), @@ -338,19 +339,19 @@ impl PresentationPatchData { let mut pairs = indexmap!( "id" => (Some(EValue::Uuid(id)), Cd::One), ); - if submitted_fields.iter().any(|&f| f == "title") { + if submitted_fields.contains("title") { pairs.insert( "title", (self.title.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "url") { + if submitted_fields.contains("url") { pairs.insert( "url", (self.url.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "event") { + if submitted_fields.contains("event") { pairs.insert( "event", (self.event.clone().map(EValue::Str), Cd::AtMostOne), @@ -411,7 +412,7 @@ impl BookPatchData { let mut lines = Vec::<&str>::new(); append_set_statement!("title", "optional str", lines, submitted_fields); append_set_statement!("download_url", "optional str", lines, submitted_fields); - if submitted_fields.iter().any(|&f| f == "author") { + if submitted_fields.contains("author") { let line = "author := ( SELECT BookAuthor FILTER .id = $author )"; @@ -425,19 +426,19 @@ impl BookPatchData { let mut pairs = indexmap!( "id" => (Some(EValue::Uuid(id)), Cd::One), ); - if submitted_fields.iter().any(|&f| f == "title") { + if submitted_fields.contains("title") { pairs.insert( "title", (self.title.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "download_url") { + if submitted_fields.contains("download_url") { pairs.insert( "download_url", (self.download_url.clone().map(EValue::Str), Cd::AtMostOne), ); } - if submitted_fields.iter().any(|&f| f == "author") { + if submitted_fields.contains("author") { pairs.insert( "author", (self.author.map(EValue::Uuid), Cd::One), diff --git a/src/types/ext.rs b/src/types/ext.rs index 38614cb..6946d3e 100644 --- a/src/types/ext.rs +++ b/src/types/ext.rs @@ -25,3 +25,13 @@ impl UriExt for Uri { builder.build().unwrap_or(self.clone()) } } + +pub trait VecExt { + fn contains(&self, elm: &T) -> bool; +} + +impl VecExt for &Vec<&String> { + fn contains(&self, elm: &str) -> bool { + self.iter().any(|&e| e == elm) + } +}