Spaces:
Running
Running
neon_arch
commited on
:recycle: refactor(API): provide a minor redesign to the internal api to support parsing of `json` data (#427)
Browse files- src/models/parser_models.rs +1 -1
- src/models/server_models.rs +15 -14
- src/server/routes/search.rs +5 -7
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)]
|
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, Clone)]
|
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,17 +1,15 @@
|
|
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<
|
15 |
/// It stores the search parameter `page` (or pageno in simple words)
|
16 |
/// of the search url.
|
17 |
pub page: Option<u32>,
|
@@ -22,26 +20,29 @@ pub struct SearchParams {
|
|
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
|
27 |
/// It stores the theme name used in the website.
|
28 |
-
pub theme:
|
29 |
/// It stores the colorscheme name used for the website theme.
|
30 |
-
pub colorscheme:
|
|
|
|
|
31 |
/// It stores the user selected upstream search engines selected from the UI.
|
32 |
-
pub engines:
|
33 |
/// It stores the user selected safe search level from the UI.
|
34 |
pub safe_search_level: u8,
|
35 |
}
|
36 |
|
37 |
-
impl
|
38 |
/// server_models::Cookie contructor function
|
39 |
-
pub fn build(style:
|
40 |
engines.sort();
|
41 |
Self {
|
42 |
-
theme:
|
43 |
-
colorscheme:
|
44 |
-
|
|
|
45 |
safe_search_level,
|
46 |
}
|
47 |
}
|
|
|
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 |
|
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 |
}
|
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
|
19 |
use tokio::{
|
20 |
fs::File,
|
21 |
io::{AsyncBufReadExt, BufReader},
|
@@ -54,17 +54,15 @@ 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
|
58 |
.and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
|
59 |
.unwrap_or_else(|| {
|
60 |
server_models::Cookie::build(
|
61 |
-
|
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,7 +159,7 @@ async fn results(
|
|
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;
|
|
|
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 |
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 |
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;
|