parse_book_source/book_source/
rule.rs1use crate::{AnalyzerManager, BookInfo, BookListItem, Chapter, ExploreItem, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Default, Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
5#[serde(rename_all = "camelCase")]
6pub struct RuleSearch {
7 pub book_list: String,
8 pub book_url: String,
9 #[serde(flatten)]
10 pub book_info: RuleBookInfo,
11}
12
13impl RuleSearch {
14 pub fn parse_to_book_list_item(
15 &self,
16 analyzer: &mut AnalyzerManager,
17 content: &str,
18 ) -> Result<BookListItem> {
19 Ok(BookListItem {
20 book_url: analyzer.get_string(&self.book_url, content, None)?,
21 book_info: self.book_info.parse_to_book_info(analyzer, content)?,
22 })
23 }
24}
25
26#[derive(Default, Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
27#[serde(rename_all = "camelCase")]
28pub struct RuleExploreItem {
29 pub title: String,
30 pub url: String,
31}
32
33impl RuleExploreItem {
34 pub fn parse_to_explore_item(
35 &self,
36 analyzer: &mut AnalyzerManager,
37 content: &str,
38 ) -> Result<ExploreItem> {
39 Ok(ExploreItem {
40 title: analyzer.get_string(&self.title, content, None)?,
41 url: analyzer.get_string(&self.url, content, None)?,
42 })
43 }
44}
45
46#[derive(Default, Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
47#[serde(rename_all = "camelCase")]
48pub struct RuleBookInfo {
49 pub name: String,
50 pub author: String,
51 #[serde(default)]
52 pub cover_url: String,
53 #[serde(default)]
54 pub intro: String,
55 #[serde(default)]
56 pub kind: String,
57 #[serde(default)]
58 pub last_chapter: String,
59 #[serde(default)]
60 pub toc_url: String,
61 #[serde(default)]
62 pub word_count: String,
63}
64
65impl RuleBookInfo {
66 pub fn parse_to_book_info(
67 &self,
68 analyzer: &mut AnalyzerManager,
69 content: &str,
70 ) -> Result<BookInfo> {
71 Ok(BookInfo {
72 name: analyzer.get_string(&self.name, content, None)?,
73 author: analyzer.get_string(&self.author, content, None)?,
74 cover_url: analyzer.get_string(&self.cover_url, content, None)?,
75 intro: analyzer.get_string(&self.intro, content, None)?,
76 kind: analyzer.get_string(&self.kind, content, None)?,
77 last_chapter: analyzer.get_string(&self.last_chapter, content, None)?,
78 toc_url: analyzer.get_string(&self.toc_url, content, None)?,
79 word_count: analyzer.get_string(&self.word_count, content, None)?,
80 })
81 }
82}
83
84#[derive(Default, Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
85#[serde(rename_all = "camelCase")]
86pub struct RuleToc {
87 pub chapter_list: String,
88 pub chapter_name: String,
89 pub chapter_url: String,
90}
91
92impl RuleToc {
93 pub fn parse_to_chapter(
94 &self,
95 analyzer: &mut AnalyzerManager,
96 content: &str,
97 ) -> Result<Chapter> {
98 Ok(Chapter {
99 chapter_name: analyzer.get_string(&self.chapter_name, content, None)?,
100 chapter_url: analyzer.get_string(&self.chapter_url, content, None)?,
101 })
102 }
103}
104
105#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
106#[serde(untagged)]
107pub enum RuleContent {
108 #[serde(rename_all = "camelCase")]
109 More {
110 content: String,
111 next_content_url: String,
112 start: usize,
113 end: String,
114 },
115 One {
116 content: String,
117 },
118}