parse_book_source/book_source/
rule.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::{AnalyzerManager, BookInfo, BookListItem, Chapter, ExploreItem, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuleSearch {
    pub book_list: String,
    pub book_url: String,
    #[serde(flatten)]
    pub book_info: RuleBookInfo,
}

impl RuleSearch {
    pub fn parse_to_book_list_item(
        &self,
        analyzer: &mut AnalyzerManager,
        content: &str,
    ) -> Result<BookListItem> {
        Ok(BookListItem {
            book_url: analyzer.get_string(&self.book_url, content, None)?,
            book_info: self.book_info.parse_to_book_info(analyzer, content)?,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuleExploreItem {
    pub title: String,
    pub url: String,
}

impl RuleExploreItem {
    pub fn parse_to_explore_item(
        &self,
        analyzer: &mut AnalyzerManager,
        content: &str,
    ) -> Result<ExploreItem> {
        Ok(ExploreItem {
            title: analyzer.get_string(&self.title, content, None)?,
            url: analyzer.get_string(&self.url, content, None)?,
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RuleBookInfo {
    pub name: String,
    pub author: String,
    #[serde(default)]
    pub cover_url: String,
    #[serde(default)]
    pub intro: String,
    #[serde(default)]
    pub kind: String,
    #[serde(default)]
    pub last_chapter: String,
    #[serde(default)]
    pub toc_url: String,
    #[serde(default)]
    pub word_count: String,
}

impl RuleBookInfo {
    pub fn parse_to_book_info(
        &self,
        analyzer: &mut AnalyzerManager,
        content: &str,
    ) -> Result<BookInfo> {
        Ok(BookInfo {
            name: analyzer.get_string(&self.name, content, None)?,
            author: analyzer.get_string(&self.author, content, None)?,
            cover_url: analyzer.get_string(&self.cover_url, content, None)?,
            intro: analyzer.get_string(&self.intro, content, None)?,
            kind: analyzer.get_string(&self.kind, content, None)?,
            last_chapter: analyzer.get_string(&self.last_chapter, content, None)?,
            toc_url: analyzer.get_string(&self.toc_url, content, None)?,
            word_count: analyzer.get_string(&self.word_count, content, None)?,
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RuleToc {
    pub chapter_list: String,
    pub chapter_name: String,
    pub chapter_url: String,
}

impl RuleToc {
    pub fn parse_to_chapter(
        &self,
        analyzer: &mut AnalyzerManager,
        content: &str,
    ) -> Result<Chapter> {
        Ok(Chapter {
            chapter_name: analyzer.get_string(&self.chapter_name, content, None)?,
            chapter_url: analyzer.get_string(&self.chapter_url, content, None)?,
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum RuleContent {
    #[serde(rename_all = "camelCase")]
    More {
        content: String,
        next_content_url: String,
        start: usize,
        end: String,
    },
    One {
        content: String,
    },
}