mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-21 21:48:21 -05:00
refactor: changed Cookie to use Cow to facilitate using references when building
This commit is contained in:
parent
a47e28587c
commit
80e950de3b
@ -1,5 +1,7 @@
|
||||
//! 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;
|
||||
@ -23,23 +25,23 @@ pub struct SearchParams {
|
||||
#[derive(Deserialize)]
|
||||
pub struct Cookie<'a> {
|
||||
/// It stores the theme name used in the website.
|
||||
pub theme: &'a str,
|
||||
pub theme: Cow<'a, str>,
|
||||
/// It stores the colorscheme name used for the website theme.
|
||||
pub colorscheme: &'a str,
|
||||
pub colorscheme: Cow<'a, str>,
|
||||
/// It stores the user selected upstream search engines selected from the UI.
|
||||
pub engines: Vec<String>,
|
||||
pub engines: Cow<'a, Vec<Cow<'a, str>>>,
|
||||
/// It stores the user selected safe search level from the UI.
|
||||
pub safe_search_level: u8,
|
||||
}
|
||||
|
||||
impl<'a> Cookie<'a> {
|
||||
/// server_models::Cookie contructor function
|
||||
pub fn build(style: &'a Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
|
||||
pub fn build(style: &'a Style, mut engines: Vec<Cow<'a, str>>, safe_search_level: u8) -> Self {
|
||||
engines.sort();
|
||||
Self {
|
||||
theme: &style.theme,
|
||||
colorscheme: &style.colorscheme,
|
||||
engines,
|
||||
theme: Cow::Borrowed(&style.theme),
|
||||
colorscheme: Cow::Borrowed(&style.colorscheme),
|
||||
engines: Cow::Owned(engines),
|
||||
safe_search_level,
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ use crate::{
|
||||
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
||||
use regex::Regex;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::File,
|
||||
io::{BufRead, BufReader, Read},
|
||||
};
|
||||
@ -48,37 +49,30 @@ pub async fn search(
|
||||
.finish());
|
||||
}
|
||||
|
||||
// Closure to build a server_models::Cookie capturing local references
|
||||
let build_cookie = || {
|
||||
server_models::Cookie::build(
|
||||
&config.style,
|
||||
config
|
||||
.upstream_search_engines
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter_map(|engine_map| engine_map.1.then_some(engine_map.0))
|
||||
.collect(),
|
||||
config.safe_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<'_> = match cookie {
|
||||
Some(ref cookie_value) => {
|
||||
match serde_json::from_str(cookie_value.value()) {
|
||||
Ok(cookie) => cookie,
|
||||
// If there's an issue parsing the cookie's value, default to the config
|
||||
Err(_) => build_cookie(),
|
||||
}
|
||||
}
|
||||
// If there's no cookie saved, use the server's config
|
||||
None => build_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
|
||||
.upstream_search_engines
|
||||
.iter()
|
||||
.filter_map(|(engine, enabled)| {
|
||||
enabled.then_some(Cow::Borrowed(engine.as_str()))
|
||||
})
|
||||
.collect(),
|
||||
config.safe_search,
|
||||
)
|
||||
});
|
||||
|
||||
search_settings.safe_search_level =
|
||||
get_safesearch_level(¶ms.safesearch, config.safe_search);
|
||||
get_safesearch_level(
|
||||
&mut search_settings.safe_search_level,
|
||||
¶ms.safesearch,
|
||||
config.safe_search,
|
||||
);
|
||||
|
||||
// Closure wrapping the results function capturing local references
|
||||
let get_results = |page| results(&config, &cache, query, page, &search_settings);
|
||||
@ -178,8 +172,7 @@ async fn results(
|
||||
config.debug,
|
||||
&search_settings
|
||||
.engines
|
||||
.clone()
|
||||
.into_iter()
|
||||
.iter()
|
||||
.filter_map(|engine| EngineHandler::new(&engine).ok())
|
||||
.collect::<Vec<EngineHandler>>(),
|
||||
config.request_timeout,
|
||||
@ -233,23 +226,21 @@ fn is_match_from_filter_list(
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// A helper function which returns the safe search level based on the url params
|
||||
/// and cookie value.
|
||||
/// A helper function to modify the safe search level based on the url params.
|
||||
/// The `safe_search` is the one in the user's cookie or
|
||||
/// the default set by the server config if the cookie was missing.
|
||||
///
|
||||
/// # Argurments
|
||||
///
|
||||
/// * `safe_search` - Safe search level from the url.
|
||||
/// * `cookie` - User's cookie
|
||||
/// * `default` - Safe search level to fall back to
|
||||
fn get_safesearch_level(safe_search: &Option<u8>, default: u8) -> u8 {
|
||||
match safe_search {
|
||||
Some(ss) => {
|
||||
if *ss >= 3 {
|
||||
default
|
||||
} else {
|
||||
*ss
|
||||
}
|
||||
/// * `url_level` - Safe search level from the url.
|
||||
/// * `safe_search` - User's cookie, or the safe search level set by the server
|
||||
/// * `config_level` - Safe search level to fall back to
|
||||
fn get_safesearch_level(safe_search: &mut u8, url_level: &Option<u8>, config_level: u8) {
|
||||
if let Some(search_level) = url_level {
|
||||
if *search_level >= 3 {
|
||||
*safe_search = config_level
|
||||
} else {
|
||||
*safe_search = *search_level;
|
||||
}
|
||||
None => default,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user