Skip to content

Commit

Permalink
♻️ refactor(API): provide a minor redesign to the internal api to sup…
Browse files Browse the repository at this point in the history
…port parsing of `json` data (#427)
  • Loading branch information
neon-mmd committed Nov 2, 2024
1 parent 65fabe5 commit 3221264
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/models/parser_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 15 additions & 14 deletions src/models/server_models.rs
Original file line number Diff line number Diff line change
@@ -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<Cow<'static, str>>,
pub q: Option<String>,
/// It stores the search parameter `page` (or pageno in simple words)
/// of the search url.
pub page: Option<u32>,
Expand All @@ -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<String>,
/// It stores the user selected upstream search engines selected from the UI.
pub engines: Cow<'a, [Cow<'a, str>]>,
pub engines: Vec<String>,
/// 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<Cow<'a, str>>, safe_search_level: u8) -> Self {
pub fn build(style: Style, mut engines: Vec<String>, 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,
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/server/routes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
// eagerly parse cookie value to evaluate safe search level
let safe_search_level = search_settings.safe_search_level;
Expand Down

0 comments on commit 3221264

Please sign in to comment.