2023-04-27 10:53:28 -04:00
|
|
|
//! This module provides public models for handling, storing and serializing of search results
|
|
|
|
//! data scraped from the upstream search engines.
|
|
|
|
|
2023-05-02 04:58:21 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-04-22 07:35:07 -04:00
|
|
|
|
2023-07-15 12:50:31 -04:00
|
|
|
use crate::{config::parser_models::Style, engines::engine_models::EngineError};
|
2023-04-30 11:16:08 -04:00
|
|
|
|
2023-04-27 10:53:28 -04:00
|
|
|
/// A named struct to store the raw scraped search results scraped search results from the
|
|
|
|
/// upstream search engines before aggregating it.It derives the Clone trait which is needed
|
|
|
|
/// to write idiomatic rust using `Iterators`.
|
|
|
|
/// (href url in html in simple words).
|
2023-08-18 04:43:53 -04:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2023-04-22 07:35:07 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResult {
|
2023-09-03 12:23:34 -04:00
|
|
|
/// The title of the search result.
|
2023-04-22 07:35:07 -04:00
|
|
|
pub title: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// The url which is accessed when clicked on it
|
2023-04-22 07:35:07 -04:00
|
|
|
pub url: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// The description of the search result.
|
2023-04-22 07:35:07 -04:00
|
|
|
pub description: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// The names of the upstream engines from which this results were provided.
|
2023-04-22 07:35:07 -04:00
|
|
|
pub engine: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:30:04 -04:00
|
|
|
impl SearchResult {
|
2023-04-27 10:53:28 -04:00
|
|
|
/// Constructs a new `RawSearchResult` with the given arguments needed for the struct.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `title` - The title of the search result.
|
2023-08-17 16:48:20 -04:00
|
|
|
/// * `url` - The url which is accessed when clicked on it
|
2023-04-27 10:53:28 -04:00
|
|
|
/// (href url in html in simple words).
|
|
|
|
/// * `description` - The description of the search result.
|
|
|
|
/// * `engine` - The names of the upstream engines from which this results were provided.
|
2023-08-17 16:48:20 -04:00
|
|
|
pub fn new(title: String, url: String, description: String, engine: Vec<String>) -> Self {
|
2023-04-25 09:30:04 -04:00
|
|
|
SearchResult {
|
|
|
|
title,
|
|
|
|
url,
|
|
|
|
description,
|
|
|
|
engine,
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 10:53:28 -04:00
|
|
|
|
|
|
|
/// A function which adds the engine name provided as a string into a vector of strings.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `engine` - Takes an engine name provided as a String.
|
2023-04-25 09:30:04 -04:00
|
|
|
pub fn add_engines(&mut self, engine: String) {
|
|
|
|
self.engine.push(engine)
|
|
|
|
}
|
2023-04-26 10:46:49 -04:00
|
|
|
|
2023-04-30 12:24:16 -04:00
|
|
|
/// A function which returns the engine name stored from the struct as a string.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// An engine name stored as a string from the struct.
|
2023-04-26 10:46:49 -04:00
|
|
|
pub fn engine(self) -> String {
|
|
|
|
self.engine.get(0).unwrap().to_string()
|
|
|
|
}
|
2023-04-25 09:30:04 -04:00
|
|
|
}
|
|
|
|
|
2023-09-03 12:23:34 -04:00
|
|
|
/// A named struct that stores the error info related to the upstream search engines.
|
2023-07-14 14:26:29 -04:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EngineErrorInfo {
|
2023-09-03 12:23:34 -04:00
|
|
|
/// It stores the error type which occured while fetching the result from a particular search
|
|
|
|
/// engine.
|
2023-07-14 14:26:29 -04:00
|
|
|
pub error: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// It stores the name of the engine that failed to provide the requested search results.
|
2023-07-14 14:26:29 -04:00
|
|
|
pub engine: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// It stores the name of the color to indicate whether how severe the particular error is (In
|
|
|
|
/// other words it indicates the severity of the error/issue).
|
2023-08-09 21:32:47 -04:00
|
|
|
pub severity_color: String,
|
2023-07-14 14:26:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EngineErrorInfo {
|
2023-09-03 12:23:34 -04:00
|
|
|
/// Constructs a new `SearchResult` with the given arguments needed for the struct.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `error` - It takes the error type which occured while fetching the result from a particular
|
|
|
|
/// search engine.
|
|
|
|
/// * `engine` - It takes the name of the engine that failed to provide the requested search results.
|
2023-07-14 14:26:29 -04:00
|
|
|
pub fn new(error: &EngineError, engine: String) -> Self {
|
|
|
|
Self {
|
|
|
|
error: match error {
|
|
|
|
EngineError::RequestError => String::from("RequestError"),
|
|
|
|
EngineError::EmptyResultSet => String::from("EmptyResultSet"),
|
|
|
|
EngineError::UnexpectedError => String::from("UnexpectedError"),
|
|
|
|
},
|
|
|
|
engine,
|
2023-08-09 21:32:47 -04:00
|
|
|
severity_color: match error {
|
|
|
|
EngineError::RequestError => String::from("green"),
|
|
|
|
EngineError::EmptyResultSet => String::from("blue"),
|
|
|
|
EngineError::UnexpectedError => String::from("red"),
|
|
|
|
},
|
2023-07-14 14:26:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 05:34:46 -04:00
|
|
|
/// A named struct to store, serialize, deserialize the all the search results scraped and
|
2023-05-02 04:58:21 -04:00
|
|
|
/// aggregated from the upstream search engines.
|
2023-04-27 10:53:28 -04:00
|
|
|
/// `SearchResult` structs.
|
2023-05-02 04:58:21 -04:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-04-22 07:35:07 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResults {
|
2023-09-03 12:23:34 -04:00
|
|
|
/// Stores the individual serializable `SearchResult` struct into a vector of
|
2023-04-22 07:35:07 -04:00
|
|
|
pub results: Vec<SearchResult>,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// Stores the current pages search query `q` provided in the search url.
|
2023-04-22 07:35:07 -04:00
|
|
|
pub page_query: String,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// Stores the theming options for the website.
|
2023-04-30 11:16:08 -04:00
|
|
|
pub style: Style,
|
2023-09-03 12:23:34 -04:00
|
|
|
/// Stores the information on which engines failed with their engine name
|
|
|
|
/// and the type of error that caused it.
|
2023-07-14 14:26:29 -04:00
|
|
|
pub engine_errors_info: Vec<EngineErrorInfo>,
|
2023-04-22 07:35:07 -04:00
|
|
|
}
|
2023-04-25 09:30:04 -04:00
|
|
|
|
|
|
|
impl SearchResults {
|
2023-04-27 10:53:28 -04:00
|
|
|
/// Constructs a new `SearchResult` with the given arguments needed for the struct.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `results` - Takes an argument of individual serializable `SearchResult` struct
|
|
|
|
/// and stores it into a vector of `SearchResult` structs.
|
|
|
|
/// * `page_query` - Takes an argument of current page`s search query `q` provided in
|
|
|
|
/// the search url.
|
2023-07-15 06:36:46 -04:00
|
|
|
/// * `empty_result_set` - Takes a boolean which indicates that no engines gave a result for the
|
|
|
|
/// given search query.
|
2023-07-14 14:26:29 -04:00
|
|
|
pub fn new(
|
|
|
|
results: Vec<SearchResult>,
|
|
|
|
page_query: String,
|
|
|
|
engine_errors_info: Vec<EngineErrorInfo>,
|
|
|
|
) -> Self {
|
2023-04-25 09:30:04 -04:00
|
|
|
SearchResults {
|
|
|
|
results,
|
|
|
|
page_query,
|
2023-04-30 11:16:08 -04:00
|
|
|
style: Style::new("".to_string(), "".to_string()),
|
2023-07-14 14:26:29 -04:00
|
|
|
engine_errors_info,
|
2023-04-25 09:30:04 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-30 11:16:08 -04:00
|
|
|
|
2023-07-15 06:36:46 -04:00
|
|
|
/// A setter function to add website style to the return search results.
|
2023-04-30 11:16:08 -04:00
|
|
|
pub fn add_style(&mut self, style: Style) {
|
|
|
|
self.style = style;
|
|
|
|
}
|
2023-04-25 09:30:04 -04:00
|
|
|
}
|