parse_book_source/analyzer/
mod.rs1use crate::Result;
2use regex::Regex;
3use std::fmt::Debug;
4pub mod analyzer_manager;
5pub mod default;
6pub mod html;
7pub mod json;
8pub use analyzer_manager::AnalyzerManager;
9pub use default::DefaultAnalyzer;
10pub use html::HtmlAnalyzer;
11pub use json::JsonPathAnalyzer;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum AnalyzerType {
15 JsonPath,
16 Html,
17 Default,
18}
19
20impl AnalyzerType {
21 pub fn parse_to_analyzer(&self, date: &str) -> Result<Box<dyn Analyzer>> {
22 match self {
23 AnalyzerType::JsonPath => Ok(Box::new(JsonPathAnalyzer::parse(date)?)),
24 AnalyzerType::Html => Ok(Box::new(HtmlAnalyzer::parse(date)?)),
25 AnalyzerType::Default => Ok(Box::new(DefaultAnalyzer::parse(date)?)),
26 }
27 }
28}
29
30#[derive(Debug, Clone)]
31pub struct Analyzers {
32 pub pattern: Regex,
33 pub replace: Option<Regex>,
34 pub analyzer: AnalyzerType,
35}
36
37impl Analyzers {
38 pub fn new(pattern: &str, replace: Option<&str>, analyzer: AnalyzerType) -> Result<Self> {
39 Ok(Self {
40 pattern: Regex::new(pattern)?,
41 replace: replace.map(Regex::new).transpose()?,
42 analyzer,
43 })
44 }
45}
46
47#[derive(Debug, Clone)]
48pub struct SingleRule {
49 pub rule: String,
50 pub replace: String,
52 pub analyzer: AnalyzerType,
53}
54
55impl SingleRule {
56 pub fn new(rule: &str, replace: Option<&str>, analyzer: AnalyzerType) -> Result<Self> {
57 Ok(Self {
58 rule: rule.to_string(),
59 replace: replace.unwrap_or("").to_string(),
60 analyzer,
61 })
62 }
63
64 pub fn replace_content(&self, content: &str) -> Result<String> {
65 if self.replace.is_empty() {
66 return Ok(content.to_string());
67 }
68
69 if let Some((regex, replace_content)) = self.replace.split_once("##") {
70 let regex = Regex::new(regex)?;
71 Ok(regex.replace_all(content, replace_content).to_string())
72 } else {
73 let regex = Regex::new(&self.replace)?;
74 Ok(regex.replace_all(content, "").to_string())
75 }
76 }
77}
78
79pub trait Analyzer {
80 fn parse(content: &str) -> Result<Self>
81 where
82 Self: Sized;
83
84 fn get_string(&self, rule: &str) -> Result<String> {
85 let _ = rule;
86 unimplemented!()
87 }
88
89 fn get_string_list(&self, rule: &str) -> Result<Vec<String>> {
90 let _ = rule;
91 unimplemented!()
92 }
93
94 fn get_elements(&self, rule: &str) -> Result<Vec<String>> {
95 let _ = rule;
96 unimplemented!()
97 }
98}