Spencerjibz commited on
Commit
765dcbd
·
unverified ·
1 Parent(s): 14d8bf9

Revert ":recycle: refactor(API): provide a minor redesign to the internal api to support parsing of `json` data (#427)"

Browse files

This reverts commit 729c55068844b2c69600ccb19cdfc9099f93e6c8.

src/models/parser_models.rs CHANGED
@@ -10,7 +10,7 @@
10
  /// order to allow the deserializing the json back to struct in aggregate function in
11
  /// aggregator.rs and create a new struct out of it and then serialize it back to json and pass
12
  /// it to the template files.
13
- #[derive(Default, Clone)]
14
  pub struct Style {
15
  /// It stores the parsed theme option used to set a theme for the website.
16
  pub theme: String,
 
10
  /// order to allow the deserializing the json back to struct in aggregate function in
11
  /// aggregator.rs and create a new struct out of it and then serialize it back to json and pass
12
  /// it to the template files.
13
+ #[derive(Default)]
14
  pub struct Style {
15
  /// It stores the parsed theme option used to set a theme for the website.
16
  pub theme: String,
src/models/server_models.rs CHANGED
@@ -1,15 +1,17 @@
1
  //! This module provides the models to parse cookies and search parameters from the search
2
  //! engine website.
 
 
 
3
 
4
  use super::parser_models::Style;
5
- use serde::{Deserialize, Serialize};
6
 
7
  /// A named struct which deserializes all the user provided search parameters and stores them.
8
  #[derive(Deserialize)]
9
  pub struct SearchParams {
10
  /// It stores the search parameter option `q` (or query in simple words)
11
  /// of the search url.
12
- pub q: Option<String>,
13
  /// It stores the search parameter `page` (or pageno in simple words)
14
  /// of the search url.
15
  pub page: Option<u32>,
@@ -20,29 +22,26 @@ pub struct SearchParams {
20
 
21
  /// A named struct which is used to deserialize the cookies fetched from the client side.
22
  #[allow(dead_code)]
23
- #[derive(Deserialize, Serialize)]
24
- pub struct Cookie {
25
  /// It stores the theme name used in the website.
26
- pub theme: String,
27
  /// It stores the colorscheme name used for the website theme.
28
- pub colorscheme: String,
29
- /// It stores the animation name used for the website theme.
30
- pub animation: Option<String>,
31
  /// It stores the user selected upstream search engines selected from the UI.
32
- pub engines: Vec<String>,
33
  /// It stores the user selected safe search level from the UI.
34
  pub safe_search_level: u8,
35
  }
36
 
37
- impl Cookie {
38
  /// server_models::Cookie contructor function
39
- pub fn build(style: Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
40
  engines.sort();
41
  Self {
42
- theme: style.theme.clone(),
43
- colorscheme: style.colorscheme.clone(),
44
- animation: style.animation.clone(),
45
- engines,
46
  safe_search_level,
47
  }
48
  }
 
1
  //! This module provides the models to parse cookies and search parameters from the search
2
  //! engine website.
3
+ use std::borrow::Cow;
4
+
5
+ use serde::Deserialize;
6
 
7
  use super::parser_models::Style;
 
8
 
9
  /// A named struct which deserializes all the user provided search parameters and stores them.
10
  #[derive(Deserialize)]
11
  pub struct SearchParams {
12
  /// It stores the search parameter option `q` (or query in simple words)
13
  /// of the search url.
14
+ pub q: Option<Cow<'static, str>>,
15
  /// It stores the search parameter `page` (or pageno in simple words)
16
  /// of the search url.
17
  pub page: Option<u32>,
 
22
 
23
  /// A named struct which is used to deserialize the cookies fetched from the client side.
24
  #[allow(dead_code)]
25
+ #[derive(Deserialize)]
26
+ pub struct Cookie<'a> {
27
  /// It stores the theme name used in the website.
28
+ pub theme: Cow<'a, str>,
29
  /// It stores the colorscheme name used for the website theme.
30
+ pub colorscheme: Cow<'a, str>,
 
 
31
  /// It stores the user selected upstream search engines selected from the UI.
32
+ pub engines: Cow<'a, [Cow<'a, str>]>,
33
  /// It stores the user selected safe search level from the UI.
34
  pub safe_search_level: u8,
35
  }
36
 
37
+ impl<'a> Cookie<'a> {
38
  /// server_models::Cookie contructor function
39
+ pub fn build(style: &'a Style, mut engines: Vec<Cow<'a, str>>, safe_search_level: u8) -> Self {
40
  engines.sort();
41
  Self {
42
+ theme: Cow::Borrowed(&style.theme),
43
+ colorscheme: Cow::Borrowed(&style.colorscheme),
44
+ engines: Cow::Owned(engines),
 
45
  safe_search_level,
46
  }
47
  }
src/server/routes/search.rs CHANGED
@@ -15,7 +15,7 @@ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
15
  use itertools::Itertools;
16
  use regex::Regex;
17
  use std::time::{SystemTime, UNIX_EPOCH};
18
- use tokio::time::Duration;
19
  use tokio::{
20
  fs::File,
21
  io::{AsyncBufReadExt, BufReader},
@@ -54,15 +54,17 @@ pub async fn search(
54
  let cookie = req.cookie("appCookie");
55
 
56
  // Get search settings using the user's cookie or from the server's config
57
- let mut search_settings: server_models::Cookie = cookie
58
  .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
59
  .unwrap_or_else(|| {
60
  server_models::Cookie::build(
61
- config.style.clone(),
62
  config
63
  .upstream_search_engines
64
  .iter()
65
- .filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
 
 
66
  .collect(),
67
  config.safe_search,
68
  )
@@ -159,7 +161,7 @@ async fn results(
159
  cache: &'static SharedCache,
160
  query: &str,
161
  page: u32,
162
- search_settings: &server_models::Cookie,
163
  ) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> {
164
  // eagerly parse cookie value to evaluate safe search level
165
  let safe_search_level = search_settings.safe_search_level;
 
15
  use itertools::Itertools;
16
  use regex::Regex;
17
  use std::time::{SystemTime, UNIX_EPOCH};
18
+ use std::{borrow::Cow, time::Duration};
19
  use tokio::{
20
  fs::File,
21
  io::{AsyncBufReadExt, BufReader},
 
54
  let cookie = req.cookie("appCookie");
55
 
56
  // Get search settings using the user's cookie or from the server's config
57
+ let mut search_settings: server_models::Cookie<'_> = cookie
58
  .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
59
  .unwrap_or_else(|| {
60
  server_models::Cookie::build(
61
+ &config.style,
62
  config
63
  .upstream_search_engines
64
  .iter()
65
+ .filter_map(|(engine, enabled)| {
66
+ enabled.then_some(Cow::Borrowed(engine.as_str()))
67
+ })
68
  .collect(),
69
  config.safe_search,
70
  )
 
161
  cache: &'static SharedCache,
162
  query: &str,
163
  page: u32,
164
+ search_settings: &server_models::Cookie<'_>,
165
  ) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> {
166
  // eagerly parse cookie value to evaluate safe search level
167
  let safe_search_level = search_settings.safe_search_level;