parse_book_source/source/rule/
rule_toc.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
use crate::Variables;
use crate::{chapter_list::Chapter, Result};
use crate::{
    chapter_list::ChapterList,
    utils::{json_path, JsonData},
    ParseError,
};
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use serde_json::Value;

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

#[derive(Debug, Clone)]
pub struct JsonRuleToc {
    pub chapter_list: String,
    pub chapter_name: JsonData,
    pub chapter_url: JsonData,
}

impl TryFrom<&RuleToc> for JsonRuleToc {
    type Error = ParseError;
    fn try_from(value: &RuleToc) -> std::result::Result<Self, Self::Error> {
        Ok(Self {
            chapter_list: value.chapter_list.clone(),
            chapter_name: value.chapter_name.as_str().try_into()?,
            chapter_url: value.chapter_url.as_str().try_into()?,
        })
    }
}

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

impl JsonRuleToc {
    pub fn parse_chapter_list(
        &self,
        data: &Value,
        variables: &mut Variables,
    ) -> Result<ChapterList> {
        let chapter_list = if self.chapter_list.as_str().ends_with("[*]") {
            json_path(data, self.chapter_list.as_str())?
        } else {
            json_path(data, &format!("{}[*]", self.chapter_list))?
        };

        if chapter_list.is_null() {
            return Ok(ChapterList::new(vec![]));
        }

        let mut res = vec![];
        for item in chapter_list
            .as_array()
            .ok_or(anyhow!("book_list is not array"))?
        {
            res.push(Chapter {
                chapter_name: self.chapter_name.parse_data(item, variables)?,
                chapter_url: self.chapter_url.parse_data(item, variables)?,
            });
        }

        Ok(res.into())
    }
}