Refactor schema migrations, optimize queries, and improve data fetching logic
- Simplified `run_dev_queries` by delegating schema updates to `run_migrations` for consistency. - Added universal `fetch_all_*_by_book` methods for streamlined data retrieval across repositories. - Replaced nested query logic with batch fetching to improve performance and maintainability. - Optimized HTML-to-text conversion by consolidating regex patterns with `LazyLock`.
This commit is contained in:
@@ -1,44 +1,45 @@
|
||||
use regex::Regex;
|
||||
|
||||
|
||||
/// Returns the current UNIX timestamp in seconds.
|
||||
/// Equivalent to TS `System.timeStampInSeconds()`.
|
||||
pub fn timestamp_in_seconds() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Creates a new UUID v4 string, or reuses an existing ID if provided.
|
||||
/// Equivalent to TS `System.createUniqueId()`.
|
||||
pub fn create_unique_id(existing_id: Option<&str>) -> String {
|
||||
match existing_id {
|
||||
Some(id) => id.to_string(),
|
||||
None => uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Converts HTML content to plain text by stripping tags and decoding entities.
|
||||
/// Equivalent to TS `System.htmlToText()`.
|
||||
pub fn html_to_text(html_node: &str) -> String {
|
||||
let mut text: String = html_node.to_string();
|
||||
let p_regex: Regex = Regex::new(r"(?i)</?p[^>]*>").unwrap();
|
||||
let br_regex: Regex = Regex::new(r"(?i)<br\s*/?>").unwrap();
|
||||
let span_heading_regex: Regex = Regex::new(r"(?i)</?(span|h[1-6])[^>]*>").unwrap();
|
||||
text = p_regex.replace_all(&text, "\n").to_string();
|
||||
text = br_regex.replace_all(&text, "\n").to_string();
|
||||
text = span_heading_regex.replace_all(&text, "").to_string();
|
||||
text = text.replace("'", "'");
|
||||
text = text.replace(""", "\"");
|
||||
text = text.replace("&", "&");
|
||||
text = text.replace("<", "<");
|
||||
text = text.replace(">", ">");
|
||||
text = text.replace("'", "'");
|
||||
let double_newline_regex: Regex = Regex::new(r"\r?\n\s*\n").unwrap();
|
||||
text = double_newline_regex.replace_all(&text, "\n").to_string();
|
||||
let spaces_regex: Regex = Regex::new(r"[ \t]+").unwrap();
|
||||
text = spaces_regex.replace_all(&text, " ").to_string();
|
||||
text.trim().to_string()
|
||||
}
|
||||
use std::sync::LazyLock;
|
||||
use regex::Regex;
|
||||
|
||||
static P_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)</?p[^>]*>").unwrap());
|
||||
static BR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)<br\s*/?>").unwrap());
|
||||
static SPAN_HEADING_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)</?(span|h[1-6])[^>]*>").unwrap());
|
||||
static DOUBLE_NEWLINE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\r?\n\s*\n").unwrap());
|
||||
static SPACES_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[ \t]+").unwrap());
|
||||
|
||||
|
||||
/// Returns the current UNIX timestamp in seconds.
|
||||
/// Equivalent to TS `System.timeStampInSeconds()`.
|
||||
pub fn timestamp_in_seconds() -> i64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Creates a new UUID v4 string, or reuses an existing ID if provided.
|
||||
/// Equivalent to TS `System.createUniqueId()`.
|
||||
pub fn create_unique_id(existing_id: Option<&str>) -> String {
|
||||
match existing_id {
|
||||
Some(id) => id.to_string(),
|
||||
None => uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Converts HTML content to plain text by stripping tags and decoding entities.
|
||||
/// Equivalent to TS `System.htmlToText()`.
|
||||
pub fn html_to_text(html_node: &str) -> String {
|
||||
let mut text: String = P_REGEX.replace_all(html_node, "\n").to_string();
|
||||
text = BR_REGEX.replace_all(&text, "\n").to_string();
|
||||
text = SPAN_HEADING_REGEX.replace_all(&text, "").to_string();
|
||||
text = text.replace("'", "'");
|
||||
text = text.replace(""", "\"");
|
||||
text = text.replace("&", "&");
|
||||
text = text.replace("<", "<");
|
||||
text = text.replace(">", ">");
|
||||
text = text.replace("'", "'");
|
||||
text = DOUBLE_NEWLINE_REGEX.replace_all(&text, "\n").to_string();
|
||||
text = SPACES_REGEX.replace_all(&text, " ").to_string();
|
||||
text.trim().to_string()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user