parse_book_source/source/rule/
rule_book_info.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
use crate::{utils::JsonData, BookInfo, ParseError, Result, Variables};
use serde::{Deserialize, Serialize};
use serde_json::Value;

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

#[derive(Debug, Clone)]
pub struct JsonRuleBookInfo {
    pub author: JsonData,
    pub cover_url: JsonData,
    pub intro: JsonData,
    pub kind: JsonData,
    pub last_chapter: JsonData,
    pub name: JsonData,
    pub toc_url: JsonData,
    pub word_count: JsonData,
}

impl TryFrom<&RuleBookInfo> for JsonRuleBookInfo {
    type Error = ParseError;
    fn try_from(value: &RuleBookInfo) -> std::result::Result<Self, Self::Error> {
        Ok(Self {
            author: value.author.as_str().try_into()?,
            intro: value.intro.as_str().try_into()?,
            kind: value.kind.as_str().try_into()?,
            name: value.name.as_str().try_into()?,
            word_count: value.word_count.as_str().try_into()?,
            cover_url: value.cover_url.as_str().try_into()?,
            last_chapter: value.last_chapter.as_str().try_into()?,
            toc_url: value.toc_url.as_str().try_into()?,
        })
    }
}

impl TryFrom<RuleBookInfo> for JsonRuleBookInfo {
    type Error = ParseError;
    fn try_from(value: RuleBookInfo) -> std::result::Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl JsonRuleBookInfo {
    pub fn parse_book_info(&self, data: &Value, variables: &mut Variables) -> Result<BookInfo> {
        Ok(BookInfo {
            author: self.author.parse_data(data, variables)?,
            cover_url: self.cover_url.parse_data(data, variables).ok(),
            intro: self.intro.parse_data(data, variables)?,
            kind: self.kind.parse_data(data, variables)?,
            last_chapter: self.last_chapter.parse_data(data, variables)?,
            name: self.name.parse_data(data, variables)?,
            toc_url: self.toc_url.parse_data(data, variables)?,
            word_count: self.word_count.parse_data(data, variables)?,
        })
    }
}