0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-12-22 20:38:22 -05:00

feat: add setter functions to SearchResults struct

This commit is contained in:
neon_arch 2023-07-14 21:26:29 +03:00
parent 9ee516e9b1
commit 3aeb3b3e73

View File

@ -3,7 +3,7 @@
use serde::{Deserialize, Serialize};
use crate::config_parser::parser_models::Style;
use crate::{config_parser::parser_models::Style, engines::engine_models::EngineError};
/// A named struct to store, serialize and deserializes the individual search result from all the
/// scraped and aggregated search results from the upstream search engines.
@ -16,7 +16,7 @@ use crate::config_parser::parser_models::Style;
/// * `url` - The url to be displayed below the search result title in html.
/// * `description` - The description of the search result.
/// * `engine` - The names of the upstream engines from which this results were provided.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchResult {
pub title: String,
@ -116,6 +116,25 @@ impl RawSearchResult {
}
}
#[derive(Serialize, Deserialize)]
pub struct EngineErrorInfo {
pub error: String,
pub engine: String,
}
impl EngineErrorInfo {
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,
}
}
}
/// A named struct to store, serialize, deserialize the all the search results scraped and
/// aggregated from the upstream search engines.
///
@ -130,6 +149,8 @@ pub struct SearchResults {
pub results: Vec<SearchResult>,
pub page_query: String,
pub style: Style,
pub engine_errors_info: Vec<EngineErrorInfo>,
pub empty_result_set: bool,
}
impl SearchResults {
@ -141,15 +162,29 @@ impl SearchResults {
/// 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.
pub fn new(results: Vec<SearchResult>, page_query: String) -> Self {
pub fn new(
results: Vec<SearchResult>,
page_query: String,
engine_errors_info: Vec<EngineErrorInfo>,
) -> Self {
SearchResults {
results,
page_query,
style: Style::new("".to_string(), "".to_string()),
engine_errors_info,
empty_result_set: false,
}
}
pub fn add_style(&mut self, style: Style) {
self.style = style;
}
pub fn is_empty_result_set(&self) -> bool {
self.results.is_empty()
}
pub fn set_empty_result_set(&mut self) {
self.empty_result_set = true;
}
}