From ec29c9c20bfeeb8aa6fda30fee6006314879d7e7 Mon Sep 17 00:00:00 2001 From: Aphronyx Date: Fri, 17 May 2024 14:19:01 +0800 Subject: [PATCH] refactor: rewrite enum `Url` --- src/rust/zh.copymanga/Cargo.lock | 36 ++++++++++++++++--- src/rust/zh.copymanga/Cargo.toml | 1 + src/rust/zh.copymanga/src/lib.rs | 27 ++++++++------ src/rust/zh.copymanga/src/parser.rs | 2 +- src/rust/zh.copymanga/src/url.rs | 55 ++++++++++------------------- 5 files changed, 69 insertions(+), 52 deletions(-) diff --git a/src/rust/zh.copymanga/Cargo.lock b/src/rust/zh.copymanga/Cargo.lock index a7757aa81..8c818097e 100644 --- a/src/rust/zh.copymanga/Cargo.lock +++ b/src/rust/zh.copymanga/Cargo.lock @@ -120,6 +120,7 @@ dependencies = [ "chinese-number", "hex", "regex", + "strum_macros", "uuid", ] @@ -170,7 +171,7 @@ checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" dependencies = [ "proc-macro2", "quote", - "syn 2.0.63", + "syn 2.0.64", ] [[package]] @@ -183,6 +184,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hex" version = "0.4.3" @@ -201,9 +208,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.154" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "num-traits" @@ -257,6 +264,25 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "strum_macros" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.64", +] + [[package]] name = "syn" version = "1.0.109" @@ -270,9 +296,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.63" +version = "2.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" dependencies = [ "proc-macro2", "quote", diff --git a/src/rust/zh.copymanga/Cargo.toml b/src/rust/zh.copymanga/Cargo.toml index dda62eede..f8bc571d6 100644 --- a/src/rust/zh.copymanga/Cargo.toml +++ b/src/rust/zh.copymanga/Cargo.toml @@ -22,4 +22,5 @@ cbc = "0.1.2" chinese-number = { version = "0.7.7", default-features = false, features = ["chinese-to-number"] } hex = { version = "0.4.3", default-features = false, features = ["alloc"] } regex = { version = "1.10.3", default-features = false, features = ["unicode"] } +strum_macros = "0.26.2" uuid = { version = "1.4.1", default-features = false } diff --git a/src/rust/zh.copymanga/src/lib.rs b/src/rust/zh.copymanga/src/lib.rs index de68d1415..5cac332e2 100644 --- a/src/rust/zh.copymanga/src/lib.rs +++ b/src/rust/zh.copymanga/src/lib.rs @@ -33,7 +33,7 @@ fn get_manga_list(filters: Vec, page: i32) -> Result { #[get_manga_details] fn get_manga_details(manga_id: String) -> Result { - let manga_page = Url::Manga(&manga_id).get_html()?; + let manga_page = Url::Manga { id: &manga_id }.get_html()?; let cover = manga_page .get_attr("img.lazyload", "data-src") @@ -50,7 +50,7 @@ fn get_manga_details(manga_id: String) -> Result { let description = manga_page.get_text("p.intro"); - let manga_url = Url::Manga(&manga_id).to_string(); + let manga_url = Url::Manga { id: &manga_id }.to_string(); let categories = manga_page .select("span.comicParticulars-left-theme-all.comicParticulars-tag > a") @@ -82,7 +82,7 @@ fn get_manga_details(manga_id: String) -> Result { #[get_chapter_list] fn get_chapter_list(manga_id: String) -> Result> { - let group_values = Url::ChapterList(&manga_id) + let group_values = Url::ChapterList { id: &manga_id } .get_json()? .as_object()? .get_as_string("results")? @@ -140,7 +140,11 @@ fn get_chapter_list(manga_id: String) -> Result> { .map(|(chapter_id, title, date_updated)| { let part = title.parse::()?; - let chapter_url = Url::Chapter(&manga_id, chapter_id).to_string(); + let chapter_url = Url::Chapter { + manga_id: &manga_id, + chapter_id, + } + .to_string(); Ok(Chapter { id: chapter_id.clone(), @@ -163,12 +167,15 @@ fn get_chapter_list(manga_id: String) -> Result> { fn get_page_list(manga_id: String, chapter_id: String) -> Result> { let mut pages = Vec::::new(); - let page_arr = Url::Chapter(&manga_id, &chapter_id) - .get_html()? - .get_attr("div.imageData", "contentkey") - .decrypt() - .json()? - .as_array()?; + let page_arr = Url::Chapter { + manga_id: &manga_id, + chapter_id: &chapter_id, + } + .get_html()? + .get_attr("div.imageData", "contentkey") + .decrypt() + .json()? + .as_array()?; let image_format = defaults_get("imageFormat").and_then(|v| v.as_string().map(|v| v.read()))?; diff --git a/src/rust/zh.copymanga/src/parser.rs b/src/rust/zh.copymanga/src/parser.rs index 5ab70abba..cea0e148a 100644 --- a/src/rust/zh.copymanga/src/parser.rs +++ b/src/rust/zh.copymanga/src/parser.rs @@ -83,7 +83,7 @@ impl MangaArr for ArrayRef { .collect::>() .join("、"); - let manga_url = Url::Manga(&manga_id).to_string(); + let manga_url = Url::Manga { id: &manga_id }.to_string(); let status_code = manga_obj.get("status").as_int().unwrap_or(-1); let status = match status_code { diff --git a/src/rust/zh.copymanga/src/url.rs b/src/rust/zh.copymanga/src/url.rs index 09752e2bb..7d7075dff 100644 --- a/src/rust/zh.copymanga/src/url.rs +++ b/src/rust/zh.copymanga/src/url.rs @@ -1,17 +1,17 @@ use aidoku::{ error::Result, helpers::uri::QueryParameters, + prelude::format, std::{html::Node, net::Request, ValueRef, Vec}, Filter, FilterType, }; use alloc::string::ToString; use core::fmt::Display; +use strum_macros::Display; +#[derive(Display)] +#[strum(prefix = "https://copymanga.site")] pub enum Url<'a> { - /// https://copymanga.site/comics?theme={}&status={}®ion={}&ordering={}&offset={}&limit={} - /// - /// --- - /// /// ## `theme` /// /// - : 全部 @@ -111,12 +111,9 @@ pub enum Url<'a> { /// ## `limit` /// /// Manga per response - Filters(QueryParameters), + #[strum(to_string = "/comics?{query}")] + Filters { query: QueryParameters }, - /// https://copymanga.site/api/kb/web/searchb/comics?offset={}&platform={}&limit={}&q={}&q_type={} - /// - /// --- - /// /// ## `offset` /// /// `({page} - 1) * {limit}` @@ -139,16 +136,20 @@ pub enum Url<'a> { /// - `name`: 名稱 /// - `author`: 作者 /// - `local`: 漢化組 - Search(QueryParameters), + #[strum(to_string = "/api/kb/web/searchb/comics?{query}")] + Search { query: QueryParameters }, - /// https://copymanga.site/comic/{manga_id} - Manga(&'a str), + #[strum(to_string = "/comic/{id}")] + Manga { id: &'a str }, - /// https://copymanga.site/comicdetail/{manga_id}/chapters - ChapterList(&'a str), + #[strum(to_string = "/comicdetail/{id}/chapters")] + ChapterList { id: &'a str }, - /// https://copymanga.site/comic/{manga_id}/chapter/{chapter_id} - Chapter(&'a str, &'a str), + #[strum(to_string = "/comic/{manga_id}/chapter/{chapter_id}")] + Chapter { + manga_id: &'a str, + chapter_id: &'a str, + }, } /// # 狀態 @@ -286,24 +287,6 @@ impl<'a> Url<'a> { } } -impl<'a> Display for Url<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - Self::Filters(query) => write!(f, "{}/comics?{}", DOMAIN, query), - Self::Search(query) => write!(f, "{}/api/kb/web/searchb/comics?{}", DOMAIN, query), - Self::Manga(manga_id) => write!(f, "{}{}{}", DOMAIN, MANGA_PATH, manga_id), - Self::ChapterList(manga_id) => { - write!(f, "{}/comicdetail/{}/chapters", DOMAIN, manga_id) - } - Self::Chapter(manga_id, chapter_id) => write!( - f, - "{}{}{}{}{}", - DOMAIN, MANGA_PATH, manga_id, CHAPTER_PATH, chapter_id - ), - } - } -} - impl<'a> From<(Vec, i32)> for Url<'a> { fn from((filters, page): (Vec, i32)) -> Self { let mut genre_index = 0; @@ -357,7 +340,7 @@ impl<'a> From<(Vec, i32)> for Url<'a> { query.push("q", Some(&search_str)); query.push_encoded("q_type", None); - return Url::Search(query); + return Url::Search { query }; } _ => continue, @@ -369,7 +352,7 @@ impl<'a> From<(Vec, i32)> for Url<'a> { query.push_encoded("region", Some(region.to_string().as_str())); query.push_encoded("ordering", Some(sort_by.to_string().as_str())); - Url::Filters(query) + Url::Filters { query } } }