diff --git a/src/models/parser_models.rs b/src/models/parser_models.rs index 24b3fd8..68b6b76 100644 --- a/src/models/parser_models.rs +++ b/src/models/parser_models.rs @@ -10,7 +10,7 @@ /// order to allow the deserializing the json back to struct in aggregate function in /// aggregator.rs and create a new struct out of it and then serialize it back to json and pass /// it to the template files. -#[derive(Default)] +#[derive(Default, Clone)] pub struct Style { /// It stores the parsed theme option used to set a theme for the website. pub theme: String, diff --git a/src/models/server_models.rs b/src/models/server_models.rs index 7a12ee5..f2bc211 100644 --- a/src/models/server_models.rs +++ b/src/models/server_models.rs @@ -1,17 +1,15 @@ //! This module provides the models to parse cookies and search parameters from the search //! engine website. -use std::borrow::Cow; - -use serde::Deserialize; use super::parser_models::Style; +use serde::{Deserialize, Serialize}; /// A named struct which deserializes all the user provided search parameters and stores them. #[derive(Deserialize)] pub struct SearchParams { /// It stores the search parameter option `q` (or query in simple words) /// of the search url. - pub q: Option>, + pub q: Option, /// It stores the search parameter `page` (or pageno in simple words) /// of the search url. pub page: Option, @@ -22,26 +20,29 @@ pub struct SearchParams { /// A named struct which is used to deserialize the cookies fetched from the client side. #[allow(dead_code)] -#[derive(Deserialize)] -pub struct Cookie<'a> { +#[derive(Deserialize, Serialize)] +pub struct Cookie { /// It stores the theme name used in the website. - pub theme: Cow<'a, str>, + pub theme: String, /// It stores the colorscheme name used for the website theme. - pub colorscheme: Cow<'a, str>, + pub colorscheme: String, + /// It stores the animation name used for the website theme. + pub animation: Option, /// It stores the user selected upstream search engines selected from the UI. - pub engines: Cow<'a, [Cow<'a, str>]>, + pub engines: Vec, /// It stores the user selected safe search level from the UI. pub safe_search_level: u8, } -impl<'a> Cookie<'a> { +impl Cookie { /// server_models::Cookie contructor function - pub fn build(style: &'a Style, mut engines: Vec>, safe_search_level: u8) -> Self { + pub fn build(style: Style, mut engines: Vec, safe_search_level: u8) -> Self { engines.sort(); Self { - theme: Cow::Borrowed(&style.theme), - colorscheme: Cow::Borrowed(&style.colorscheme), - engines: Cow::Owned(engines), + theme: style.theme.clone(), + colorscheme: style.colorscheme.clone(), + animation: style.animation.clone(), + engines, safe_search_level, } } diff --git a/src/server/routes/search.rs b/src/server/routes/search.rs index d43ddec..13d6025 100644 --- a/src/server/routes/search.rs +++ b/src/server/routes/search.rs @@ -15,7 +15,7 @@ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse}; use itertools::Itertools; use regex::Regex; use std::time::{SystemTime, UNIX_EPOCH}; -use std::{borrow::Cow, time::Duration}; +use tokio::time::Duration; use tokio::{ fs::File, io::{AsyncBufReadExt, BufReader}, @@ -54,17 +54,15 @@ pub async fn search( let cookie = req.cookie("appCookie"); // Get search settings using the user's cookie or from the server's config - let mut search_settings: server_models::Cookie<'_> = cookie + let mut search_settings: server_models::Cookie = cookie .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok()) .unwrap_or_else(|| { server_models::Cookie::build( - &config.style, + config.style.clone(), config .upstream_search_engines .iter() - .filter_map(|(engine, enabled)| { - enabled.then_some(Cow::Borrowed(engine.as_str())) - }) + .filter_map(|(engine, enabled)| enabled.then_some(engine.clone())) .collect(), config.safe_search, ) @@ -160,7 +158,7 @@ async fn results( cache: &'static SharedCache, query: &str, page: u32, - search_settings: &server_models::Cookie<'_>, + search_settings: &server_models::Cookie, ) -> Result<(SearchResults, String, bool), Box> { // eagerly parse cookie value to evaluate safe search level let safe_search_level = search_settings.safe_search_level;