0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 05:58:21 -05:00

⚙️ refactor: replace vecs with smallvecs for smaller data sizes & replace to_strings with to_owned (#180)(#178)

This commit is contained in:
neon_arch 2023-08-27 21:02:23 +03:00
parent 2a68081ae2
commit 2885f23ec9

View File

@ -2,6 +2,7 @@
//! data scraped from the upstream search engines. //! data scraped from the upstream search engines.
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use crate::{config::parser_models::Style, engines::engine_models::EngineError}; use crate::{config::parser_models::Style, engines::engine_models::EngineError};
@ -16,13 +17,13 @@ use crate::{config::parser_models::Style, engines::engine_models::EngineError};
/// (href url in html in simple words). /// (href url in html in simple words).
/// * `description` - The description of the search result. /// * `description` - The description of the search result.
/// * `engine` - The names of the upstream engines from which this results were provided. /// * `engine` - The names of the upstream engines from which this results were provided.
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SearchResult { pub struct SearchResult {
pub title: String, pub title: String,
pub url: String, pub url: String,
pub description: String, pub description: String,
pub engine: Vec<String>, pub engine: SmallVec<[String; 0]>,
} }
impl SearchResult { impl SearchResult {
@ -35,12 +36,12 @@ impl SearchResult {
/// (href url in html in simple words). /// (href url in html in simple words).
/// * `description` - The description of the search result. /// * `description` - The description of the search result.
/// * `engine` - The names of the upstream engines from which this results were provided. /// * `engine` - The names of the upstream engines from which this results were provided.
pub fn new(title: String, url: String, description: String, engine: Vec<String>) -> Self { pub fn new(title: &str, url: &str, description: &str, engine: &[&str]) -> Self {
SearchResult { SearchResult {
title, title: title.to_owned(),
url, url: url.to_owned(),
description, description: description.to_owned(),
engine, engine: engine.iter().map(|name| name.to_string()).collect(),
} }
} }
@ -49,8 +50,8 @@ impl SearchResult {
/// # Arguments /// # Arguments
/// ///
/// * `engine` - Takes an engine name provided as a String. /// * `engine` - Takes an engine name provided as a String.
pub fn add_engines(&mut self, engine: String) { pub fn add_engines(&mut self, engine: &str) {
self.engine.push(engine) self.engine.push(engine.to_owned())
} }
/// A function which returns the engine name stored from the struct as a string. /// A function which returns the engine name stored from the struct as a string.
@ -58,13 +59,12 @@ impl SearchResult {
/// # Returns /// # Returns
/// ///
/// An engine name stored as a string from the struct. /// An engine name stored as a string from the struct.
pub fn engine(self) -> String { pub fn engine(&mut self) -> String {
self.engine.get(0).unwrap().to_string() std::mem::take(&mut self.engine[0])
} }
} }
/// #[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize)]
pub struct EngineErrorInfo { pub struct EngineErrorInfo {
pub error: String, pub error: String,
pub engine: String, pub engine: String,
@ -72,18 +72,18 @@ pub struct EngineErrorInfo {
} }
impl EngineErrorInfo { impl EngineErrorInfo {
pub fn new(error: &EngineError, engine: String) -> Self { pub fn new(error: &EngineError, engine: &str) -> Self {
Self { Self {
error: match error { error: match error {
EngineError::RequestError => String::from("RequestError"), EngineError::RequestError => "RequestError".to_owned(),
EngineError::EmptyResultSet => String::from("EmptyResultSet"), EngineError::EmptyResultSet => "EmptyResultSet".to_owned(),
EngineError::UnexpectedError => String::from("UnexpectedError"), EngineError::UnexpectedError => "UnexpectedError".to_owned(),
}, },
engine, engine: engine.to_owned(),
severity_color: match error { severity_color: match error {
EngineError::RequestError => String::from("green"), EngineError::RequestError => "green".to_owned(),
EngineError::EmptyResultSet => String::from("blue"), EngineError::EmptyResultSet => "blue".to_owned(),
EngineError::UnexpectedError => String::from("red"), EngineError::UnexpectedError => "red".to_owned(),
}, },
} }
} }
@ -108,7 +108,7 @@ pub struct SearchResults {
pub results: Vec<SearchResult>, pub results: Vec<SearchResult>,
pub page_query: String, pub page_query: String,
pub style: Style, pub style: Style,
pub engine_errors_info: Vec<EngineErrorInfo>, pub engine_errors_info: SmallVec<[EngineErrorInfo; 0]>,
} }
impl SearchResults { impl SearchResults {
@ -124,19 +124,19 @@ impl SearchResults {
/// given search query. /// given search query.
pub fn new( pub fn new(
results: Vec<SearchResult>, results: Vec<SearchResult>,
page_query: String, page_query: &str,
engine_errors_info: Vec<EngineErrorInfo>, engine_errors_info: &[EngineErrorInfo],
) -> Self { ) -> Self {
SearchResults { Self {
results, results,
page_query, page_query: page_query.to_owned(),
style: Style::new("".to_string(), "".to_string()), style: Style::default(),
engine_errors_info, engine_errors_info: SmallVec::from(engine_errors_info),
} }
} }
/// A setter function to add website style to the return search results. /// A setter function to add website style to the return search results.
pub fn add_style(&mut self, style: Style) { pub fn add_style(&mut self, style: &Style) {
self.style = style; self.style = style.to_owned();
} }
} }