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

use cows just as before

Browse files
Cargo.lock CHANGED
@@ -1,6 +1,6 @@
1
  # This file is automatically @generated by Cargo.
2
  # It is not intended for manual editing.
3
- version = 4
4
 
5
  [[package]]
6
  name = "actix-codec"
 
1
  # This file is automatically @generated by Cargo.
2
  # It is not intended for manual editing.
3
+ version = 3
4
 
5
  [[package]]
6
  name = "actix-codec"
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
@@ -2,7 +2,7 @@
2
  //! engine website.
3
  use std::borrow::Cow;
4
 
5
- use serde::Deserialize;
6
 
7
  use super::parser_models::Style;
8
 
@@ -22,16 +22,22 @@ 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<'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> {
@@ -43,6 +49,10 @@ impl<'a> Cookie<'a> {
43
  colorscheme: Cow::Borrowed(&style.colorscheme),
44
  engines: Cow::Owned(engines),
45
  safe_search_level,
 
 
 
 
46
  }
47
  }
48
  }
 
2
  //! engine website.
3
  use std::borrow::Cow;
4
 
5
+ use serde::{Deserialize, Serialize};
6
 
7
  use super::parser_models::Style;
8
 
 
22
 
23
  /// A named struct which is used to deserialize the cookies fetched from the client side.
24
  #[allow(dead_code)]
25
+ #[derive(Deserialize, Serialize)]
26
  pub struct Cookie<'a> {
27
+ #[serde(borrow)]
28
  /// It stores the theme name used in the website.
29
  pub theme: Cow<'a, str>,
30
+ #[serde(borrow)]
31
  /// It stores the colorscheme name used for the website theme.
32
  pub colorscheme: Cow<'a, str>,
33
+ #[serde(borrow)]
34
  /// It stores the user selected upstream search engines selected from the UI.
35
  pub engines: Cow<'a, [Cow<'a, str>]>,
36
  /// It stores the user selected safe search level from the UI.
37
  pub safe_search_level: u8,
38
+ #[serde(borrow)]
39
+ /// It stores the animation name used for the website theme.
40
+ pub animation: Option<Cow<'a, str>>,
41
  }
42
 
43
  impl<'a> Cookie<'a> {
 
49
  colorscheme: Cow::Borrowed(&style.colorscheme),
50
  engines: Cow::Owned(engines),
51
  safe_search_level,
52
+ animation: style
53
+ .animation
54
+ .as_ref()
55
+ .map(|str| Cow::Borrowed(str.as_str())),
56
  }
57
  }
58
  }
src/server/routes/export_import.rs CHANGED
@@ -13,7 +13,9 @@ use actix_web::{
13
  },
14
  get, post, web, HttpRequest, HttpResponse,
15
  };
 
16
  use std::io::Read;
 
17
  use tokio::fs::read_dir;
18
 
19
  /// A helper function that helps in building the list of all available colorscheme/theme/animation
@@ -29,8 +31,10 @@ use tokio::fs::read_dir;
29
  ///
30
  /// Returns a list of colorscheme/theme names as a vector of tuple strings on success otherwise
31
  /// returns a standard error message.
32
- async fn style_option_list(style_type: &str) -> Result<Box<[String]>, Box<dyn std::error::Error>> {
33
- let mut style_options: Vec<String> = Vec::new();
 
 
34
  let mut dir = read_dir(format!(
35
  "{}static/{}/",
36
  file_path(FileType::Theme)?,
@@ -39,11 +43,11 @@ async fn style_option_list(style_type: &str) -> Result<Box<[String]>, Box<dyn st
39
  .await?;
40
  while let Some(file) = dir.next_entry().await? {
41
  let style_name = file.file_name().to_str().unwrap().replace(".css", "");
42
- style_options.push(style_name.clone());
43
  }
44
 
45
  if style_type == "animations" {
46
- style_options.push(String::default())
47
  }
48
 
49
  Ok(style_options.into_boxed_slice())
@@ -61,42 +65,49 @@ async fn style_option_list(style_type: &str) -> Result<Box<[String]>, Box<dyn st
61
  /// returns a standard error message on failure otherwise it returns the unit type.
62
  async fn sanitize(
63
  config: web::Data<&'static Config>,
64
- setting_value: &mut models::server_models::Cookie,
65
  ) -> Result<(), Box<dyn std::error::Error>> {
66
  // Check whether the theme, colorscheme and animation option is valid by matching it against
67
  // the available option list. If the option provided by the user via the JSON file is invalid
68
  // then replace the user provided by the default one used by the server via the config file.
 
69
  if !style_option_list("themes")
70
  .await?
71
- .contains(&setting_value.theme.to_string())
72
  {
73
- setting_value.theme = config.style.theme.clone()
74
  } else if !style_option_list("colorschemes")
75
  .await?
76
- .contains(&setting_value.colorscheme.to_string())
77
  {
78
- setting_value.colorscheme = config.style.colorscheme.clone()
79
  } else if !style_option_list("animations")
80
  .await?
81
- .contains(&setting_value.animation.clone().unwrap_or_default())
82
  {
83
- setting_value.animation = config.style.animation.clone()
 
 
 
 
84
  }
85
 
86
  // Filters out any engines in the list that are invalid by matching each engine against the
87
  // available engine list.
88
- setting_value.engines = setting_value
89
  .engines
90
- .clone()
91
- .into_iter()
92
  .filter_map(|engine| {
93
  config
94
  .upstream_search_engines
95
  .keys()
96
- .any(|other_engine| &engine == other_engine)
97
- .then_some(engine)
 
98
  })
99
  .collect();
 
100
 
101
  setting_value.safe_search_level = match setting_value.safe_search_level {
102
  0..2 => setting_value.safe_search_level,
@@ -129,7 +140,7 @@ pub async fn set_settings(
129
  let mut data = String::new();
130
  form.file.file.read_to_string(&mut data).unwrap();
131
 
132
- let mut unsanitized_json_data: models::server_models::Cookie =
133
  serde_json::from_str(&data)?;
134
 
135
  sanitize(config, &mut unsanitized_json_data).await?;
@@ -162,15 +173,18 @@ pub async fn download(
162
  let cookie = req.cookie("appCookie");
163
 
164
  // Get search settings using the user's cookie or from the server's config
165
- let preferences: server_models::Cookie = cookie
 
166
  .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
167
  .unwrap_or_else(|| {
168
  server_models::Cookie::build(
169
- config.style.clone(),
170
  config
171
  .upstream_search_engines
172
  .iter()
173
- .filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
 
 
174
  .collect(),
175
  u8::default(),
176
  )
 
13
  },
14
  get, post, web, HttpRequest, HttpResponse,
15
  };
16
+ use std::borrow::Cow;
17
  use std::io::Read;
18
+
19
  use tokio::fs::read_dir;
20
 
21
  /// A helper function that helps in building the list of all available colorscheme/theme/animation
 
31
  ///
32
  /// Returns a list of colorscheme/theme names as a vector of tuple strings on success otherwise
33
  /// returns a standard error message.
34
+ async fn style_option_list<'a>(
35
+ style_type: &'a str,
36
+ ) -> Result<Box<[Cow<'a, str>]>, Box<dyn std::error::Error>> {
37
+ let mut style_options = Vec::new();
38
  let mut dir = read_dir(format!(
39
  "{}static/{}/",
40
  file_path(FileType::Theme)?,
 
43
  .await?;
44
  while let Some(file) = dir.next_entry().await? {
45
  let style_name = file.file_name().to_str().unwrap().replace(".css", "");
46
+ style_options.push(Cow::Owned(style_name));
47
  }
48
 
49
  if style_type == "animations" {
50
+ style_options.push(Cow::default())
51
  }
52
 
53
  Ok(style_options.into_boxed_slice())
 
65
  /// returns a standard error message on failure otherwise it returns the unit type.
66
  async fn sanitize(
67
  config: web::Data<&'static Config>,
68
+ setting_value: &mut models::server_models::Cookie<'_>,
69
  ) -> Result<(), Box<dyn std::error::Error>> {
70
  // Check whether the theme, colorscheme and animation option is valid by matching it against
71
  // the available option list. If the option provided by the user via the JSON file is invalid
72
  // then replace the user provided by the default one used by the server via the config file.
73
+
74
  if !style_option_list("themes")
75
  .await?
76
+ .contains(&setting_value.theme)
77
  {
78
+ setting_value.theme = Cow::Borrowed(&config.style.theme)
79
  } else if !style_option_list("colorschemes")
80
  .await?
81
+ .contains(&setting_value.colorscheme)
82
  {
83
+ setting_value.colorscheme = Cow::Borrowed(&config.style.colorscheme)
84
  } else if !style_option_list("animations")
85
  .await?
86
+ .contains(setting_value.animation.as_ref().unwrap())
87
  {
88
+ setting_value.animation = config
89
+ .style
90
+ .animation
91
+ .as_ref()
92
+ .map(|str| Cow::Borrowed(str.as_str()));
93
  }
94
 
95
  // Filters out any engines in the list that are invalid by matching each engine against the
96
  // available engine list.
97
+ let engines: Vec<_> = setting_value
98
  .engines
99
+ .iter()
100
+ .cloned()
101
  .filter_map(|engine| {
102
  config
103
  .upstream_search_engines
104
  .keys()
105
+ .cloned()
106
+ .any(|other_engine| *engine == other_engine)
107
+ .then_some(engine.clone())
108
  })
109
  .collect();
110
+ setting_value.engines = Cow::Owned(engines);
111
 
112
  setting_value.safe_search_level = match setting_value.safe_search_level {
113
  0..2 => setting_value.safe_search_level,
 
140
  let mut data = String::new();
141
  form.file.file.read_to_string(&mut data).unwrap();
142
 
143
+ let mut unsanitized_json_data: models::server_models::Cookie<'_> =
144
  serde_json::from_str(&data)?;
145
 
146
  sanitize(config, &mut unsanitized_json_data).await?;
 
173
  let cookie = req.cookie("appCookie");
174
 
175
  // Get search settings using the user's cookie or from the server's config
176
+ let preferences: server_models::Cookie<'_> = cookie
177
+ .as_ref()
178
  .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
179
  .unwrap_or_else(|| {
180
  server_models::Cookie::build(
181
+ &config.style,
182
  config
183
  .upstream_search_engines
184
  .iter()
185
+ .filter_map(|(engine, enabled)| {
186
+ enabled.then_some(Cow::Borrowed(engine.as_str()))
187
+ })
188
  .collect(),
189
  u8::default(),
190
  )
src/server/routes/search.rs CHANGED
@@ -55,6 +55,7 @@ pub async fn search(
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(
 
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
+ .as_ref()
59
  .and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
60
  .unwrap_or_else(|| {
61
  server_models::Cookie::build(