use crate::Result;
use crate::search::types::{SearchEngineType, SearchResult};
use serde_json::Value;
pub trait BaseParser: Send + Sync {
fn name(&self) -> &str;
fn engine_type(&self) -> SearchEngineType;
fn supports(&self, engine_type: &SearchEngineType) -> bool {
&self.engine_type() == engine_type
}
fn parse(&self, html: &str, limit: usize) -> Result<Vec<SearchResult>>;
}
pub struct BaseParserImpl {
name: String,
engine_type: SearchEngineType,
}
impl BaseParserImpl {
pub fn new(name: String, engine_type: SearchEngineType) -> Self {
Self { name, engine_type }
}
}
impl BaseParser for BaseParserImpl {
fn name(&self) -> &str {
&self.name
}
fn engine_type(&self) -> SearchEngineType {
self.engine_type
}
fn parse(&self, _html: &str, _limit: usize) -> Result<Vec<SearchResult>> {
Ok(Vec::new()) }
}
pub mod helpers {
use super::*;
pub fn extract_json_text(json: &Value, field: &str) -> String {
json[field].as_str().unwrap_or("").to_string()
}
pub fn extract_nested_json_text(json: &Value, path: &[&str]) -> String {
let mut current = json;
for field in path {
current = ¤t[field];
}
current.as_str().unwrap_or("").to_string()
}
pub fn extract_json_array(json: &Value, field: &str) -> Option<Vec<Value>> {
json[field].as_array().cloned()
}
pub fn extract_nested_json_array(json: &Value, path: &[&str]) -> Option<Vec<Value>> {
let mut current = json;
for field in path {
current = ¤t[field];
}
current.as_array().cloned()
}
pub fn create_search_result_from_json(
json: &Value,
title_field: &str,
url_field: &str,
snippet_field: &str,
rank: usize,
) -> SearchResult {
SearchResult {
title: extract_json_text(json, title_field),
url: extract_json_text(json, url_field),
snippet: extract_json_text(json, snippet_field),
rank,
}
}
pub fn create_search_result_from_nested_json(
json: &Value,
title_path: &[&str],
url_path: &[&str],
snippet_path: &[&str],
rank: usize,
) -> SearchResult {
SearchResult {
title: extract_nested_json_text(json, title_path),
url: extract_nested_json_text(json, url_path),
snippet: extract_nested_json_text(json, snippet_path),
rank,
}
}
}