2023-04-27 17:53:28 +03:00
|
|
|
//! This module provides public models for handling, storing and serializing of search results
|
|
|
|
//! data scraped from the upstream search engines.
|
|
|
|
|
2023-05-02 11:58:21 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-08-27 21:02:23 +03:00
|
|
|
use smallvec::SmallVec;
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-09-03 20:50:50 +03:00
|
|
|
use super::{engine_models::EngineError, parser_models::Style};
|
2023-04-30 18:16:08 +03:00
|
|
|
|
2023-04-27 17:53:28 +03: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 10:43:53 +02:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2023-04-22 14:35:07 +03:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResult {
|
2023-09-03 19:23:34 +03:00
|
|
|
/// The title of the search result.
|
2023-04-22 14:35:07 +03:00
|
|
|
pub title: String,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// The url which is accessed when clicked on it
|
2023-04-22 14:35:07 +03:00
|
|
|
pub url: String,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// The description of the search result.
|
2023-04-22 14:35:07 +03:00
|
|
|
pub description: String,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// The names of the upstream engines from which this results were provided.
|
2023-08-27 21:02:23 +03:00
|
|
|
pub engine: SmallVec<[String; 0]>,
|
2023-04-22 14:35:07 +03:00
|
|
|
}
|
|
|
|
|
2023-04-25 16:30:04 +03:00
|
|
|
impl SearchResult {
|
2023-04-27 17:53:28 +03:00
|
|
|
/// Constructs a new `RawSearchResult` with the given arguments needed for the struct.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `title` - The title of the search result.
|
2023-08-17 22:48:20 +02:00
|
|
|
/// * `url` - The url which is accessed when clicked on it
|
2023-04-27 17:53:28 +03: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-27 21:02:23 +03:00
|
|
|
pub fn new(title: &str, url: &str, description: &str, engine: &[&str]) -> Self {
|
2023-04-25 16:30:04 +03:00
|
|
|
SearchResult {
|
2023-08-27 21:02:23 +03:00
|
|
|
title: title.to_owned(),
|
|
|
|
url: url.to_owned(),
|
|
|
|
description: description.to_owned(),
|
|
|
|
engine: engine.iter().map(|name| name.to_string()).collect(),
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:53:28 +03: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-08-27 21:02:23 +03:00
|
|
|
pub fn add_engines(&mut self, engine: &str) {
|
|
|
|
self.engine.push(engine.to_owned())
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|
2023-04-26 17:46:49 +03:00
|
|
|
|
2023-04-30 19:24:16 +03: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-08-27 21:02:23 +03:00
|
|
|
pub fn engine(&mut self) -> String {
|
|
|
|
std::mem::take(&mut self.engine[0])
|
2023-04-26 17:46:49 +03:00
|
|
|
}
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|
|
|
|
|
2023-09-03 19:23:34 +03:00
|
|
|
/// A named struct that stores the error info related to the upstream search engines.
|
2023-08-27 21:02:23 +03:00
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
2023-07-14 21:26:29 +03:00
|
|
|
pub struct EngineErrorInfo {
|
2023-09-03 19:23:34 +03:00
|
|
|
/// It stores the error type which occured while fetching the result from a particular search
|
|
|
|
/// engine.
|
2023-07-14 21:26:29 +03:00
|
|
|
pub error: String,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// It stores the name of the engine that failed to provide the requested search results.
|
2023-07-14 21:26:29 +03:00
|
|
|
pub engine: String,
|
2023-09-03 19:23:34 +03: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-10 04:32:47 +03:00
|
|
|
pub severity_color: String,
|
2023-07-14 21:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EngineErrorInfo {
|
2023-09-03 19:23:34 +03: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-08-27 21:02:23 +03:00
|
|
|
pub fn new(error: &EngineError, engine: &str) -> Self {
|
2023-07-14 21:26:29 +03:00
|
|
|
Self {
|
|
|
|
error: match error {
|
2023-08-27 21:02:23 +03:00
|
|
|
EngineError::RequestError => "RequestError".to_owned(),
|
|
|
|
EngineError::EmptyResultSet => "EmptyResultSet".to_owned(),
|
|
|
|
EngineError::UnexpectedError => "UnexpectedError".to_owned(),
|
2023-07-14 21:26:29 +03:00
|
|
|
},
|
2023-08-27 21:02:23 +03:00
|
|
|
engine: engine.to_owned(),
|
2023-08-10 04:32:47 +03:00
|
|
|
severity_color: match error {
|
2023-08-27 21:02:23 +03:00
|
|
|
EngineError::RequestError => "green".to_owned(),
|
|
|
|
EngineError::EmptyResultSet => "blue".to_owned(),
|
|
|
|
EngineError::UnexpectedError => "red".to_owned(),
|
2023-08-10 04:32:47 +03:00
|
|
|
},
|
2023-07-14 21:26:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 09:34:46 +00:00
|
|
|
/// A named struct to store, serialize, deserialize the all the search results scraped and
|
2023-05-02 11:58:21 +03:00
|
|
|
/// aggregated from the upstream search engines.
|
2023-04-27 17:53:28 +03:00
|
|
|
/// `SearchResult` structs.
|
2023-09-02 17:48:27 +03:00
|
|
|
#[derive(Serialize, Deserialize, Default)]
|
2023-04-22 14:35:07 +03:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SearchResults {
|
2023-09-03 19:23:34 +03:00
|
|
|
/// Stores the individual serializable `SearchResult` struct into a vector of
|
2023-04-22 14:35:07 +03:00
|
|
|
pub results: Vec<SearchResult>,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// Stores the current pages search query `q` provided in the search url.
|
2023-04-22 14:35:07 +03:00
|
|
|
pub page_query: String,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// Stores the theming options for the website.
|
2023-04-30 18:16:08 +03:00
|
|
|
pub style: Style,
|
2023-09-03 19:23:34 +03:00
|
|
|
/// Stores the information on which engines failed with their engine name
|
|
|
|
/// and the type of error that caused it.
|
2023-07-14 21:26:29 +03:00
|
|
|
pub engine_errors_info: Vec<EngineErrorInfo>,
|
2023-09-12 17:59:33 +03:00
|
|
|
/// Stores the flag option which holds the check value that the following
|
|
|
|
/// search query was disallowed when the safe search level set to 4 and it
|
2023-09-12 17:49:46 +03:00
|
|
|
/// was present in the `Blocklist` file.
|
2023-09-02 17:48:27 +03:00
|
|
|
pub disallowed: bool,
|
2023-09-12 17:59:33 +03:00
|
|
|
/// Stores the flag option which holds the check value that the following
|
|
|
|
/// search query was filtered when the safe search level set to 3 and it
|
2023-09-12 17:49:46 +03:00
|
|
|
/// was present in the `Blocklist` file.
|
2023-09-02 17:48:27 +03:00
|
|
|
pub filtered: bool,
|
2023-04-22 14:35:07 +03:00
|
|
|
}
|
2023-04-25 16:30:04 +03:00
|
|
|
|
|
|
|
impl SearchResults {
|
2023-04-27 17:53:28 +03: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-09-12 17:59:33 +03:00
|
|
|
/// * `engine_errors_info` - Takes an array of structs which contains information regarding
|
|
|
|
/// which engines failed with their names, reason and their severity color name.
|
2023-07-14 21:26:29 +03:00
|
|
|
pub fn new(
|
|
|
|
results: Vec<SearchResult>,
|
2023-08-27 21:02:23 +03:00
|
|
|
page_query: &str,
|
|
|
|
engine_errors_info: &[EngineErrorInfo],
|
2023-07-14 21:26:29 +03:00
|
|
|
) -> Self {
|
2023-08-27 21:02:23 +03:00
|
|
|
Self {
|
2023-04-25 16:30:04 +03:00
|
|
|
results,
|
2023-09-10 18:56:54 +03:00
|
|
|
page_query: page_query.to_owned(),
|
2023-09-02 17:48:27 +03:00
|
|
|
style: Style::default(),
|
2023-09-10 18:56:54 +03:00
|
|
|
engine_errors_info: engine_errors_info.to_owned(),
|
2023-09-02 17:48:27 +03:00
|
|
|
disallowed: Default::default(),
|
|
|
|
filtered: Default::default(),
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-30 18:16:08 +03:00
|
|
|
|
2023-07-15 13:36:46 +03:00
|
|
|
/// A setter function to add website style to the return search results.
|
2023-09-02 17:48:27 +03:00
|
|
|
pub fn add_style(&mut self, style: &Style) {
|
|
|
|
self.style = style.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A setter function that sets disallowed to true.
|
|
|
|
pub fn set_disallowed(&mut self) {
|
|
|
|
self.disallowed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A setter function to set the current page search query.
|
|
|
|
pub fn set_page_query(&mut self, page: &str) {
|
|
|
|
self.page_query = page.to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A setter function that sets the filtered to true.
|
|
|
|
pub fn set_filtered(&mut self) {
|
|
|
|
self.filtered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A getter function that gets the value of `engine_errors_info`.
|
|
|
|
pub fn engine_errors_info(&mut self) -> Vec<EngineErrorInfo> {
|
|
|
|
std::mem::take(&mut self.engine_errors_info)
|
|
|
|
}
|
|
|
|
/// A getter function that gets the value of `results`.
|
|
|
|
pub fn results(&mut self) -> Vec<SearchResult> {
|
|
|
|
self.results.clone()
|
2023-04-30 18:16:08 +03:00
|
|
|
}
|
2023-04-25 16:30:04 +03:00
|
|
|
}
|