neon_arch commited on
Commit
2885f23
1 Parent(s): 2a68081

⚙️ refactor: replace vecs with smallvecs for smaller data sizes & replace to_strings with to_owned (#180)(#178)

Browse files
Files changed (1) hide show
  1. src/results/aggregation_models.rs +30 -30
src/results/aggregation_models.rs CHANGED
@@ -2,6 +2,7 @@
2
  //! data scraped from the upstream search engines.
3
 
4
  use serde::{Deserialize, Serialize};
 
5
 
6
  use crate::{config::parser_models::Style, engines::engine_models::EngineError};
7
 
@@ -16,13 +17,13 @@ use crate::{config::parser_models::Style, engines::engine_models::EngineError};
16
  /// (href url in html in simple words).
17
  /// * `description` - The description of the search result.
18
  /// * `engine` - The names of the upstream engines from which this results were provided.
19
- #[derive(Clone, Serialize, Deserialize)]
20
  #[serde(rename_all = "camelCase")]
21
  pub struct SearchResult {
22
  pub title: String,
23
  pub url: String,
24
  pub description: String,
25
- pub engine: Vec<String>,
26
  }
27
 
28
  impl SearchResult {
@@ -35,12 +36,12 @@ impl SearchResult {
35
  /// (href url in html in simple words).
36
  /// * `description` - The description of the search result.
37
  /// * `engine` - The names of the upstream engines from which this results were provided.
38
- pub fn new(title: String, url: String, description: String, engine: Vec<String>) -> Self {
39
  SearchResult {
40
- title,
41
- url,
42
- description,
43
- engine,
44
  }
45
  }
46
 
@@ -49,8 +50,8 @@ impl SearchResult {
49
  /// # Arguments
50
  ///
51
  /// * `engine` - Takes an engine name provided as a String.
52
- pub fn add_engines(&mut self, engine: String) {
53
- self.engine.push(engine)
54
  }
55
 
56
  /// A function which returns the engine name stored from the struct as a string.
@@ -58,13 +59,12 @@ impl SearchResult {
58
  /// # Returns
59
  ///
60
  /// An engine name stored as a string from the struct.
61
- pub fn engine(self) -> String {
62
- self.engine.get(0).unwrap().to_string()
63
  }
64
  }
65
 
66
- ///
67
- #[derive(Serialize, Deserialize)]
68
  pub struct EngineErrorInfo {
69
  pub error: String,
70
  pub engine: String,
@@ -72,18 +72,18 @@ pub struct EngineErrorInfo {
72
  }
73
 
74
  impl EngineErrorInfo {
75
- pub fn new(error: &EngineError, engine: String) -> Self {
76
  Self {
77
  error: match error {
78
- EngineError::RequestError => String::from("RequestError"),
79
- EngineError::EmptyResultSet => String::from("EmptyResultSet"),
80
- EngineError::UnexpectedError => String::from("UnexpectedError"),
81
  },
82
- engine,
83
  severity_color: match error {
84
- EngineError::RequestError => String::from("green"),
85
- EngineError::EmptyResultSet => String::from("blue"),
86
- EngineError::UnexpectedError => String::from("red"),
87
  },
88
  }
89
  }
@@ -108,7 +108,7 @@ pub struct SearchResults {
108
  pub results: Vec<SearchResult>,
109
  pub page_query: String,
110
  pub style: Style,
111
- pub engine_errors_info: Vec<EngineErrorInfo>,
112
  }
113
 
114
  impl SearchResults {
@@ -124,19 +124,19 @@ impl SearchResults {
124
  /// given search query.
125
  pub fn new(
126
  results: Vec<SearchResult>,
127
- page_query: String,
128
- engine_errors_info: Vec<EngineErrorInfo>,
129
  ) -> Self {
130
- SearchResults {
131
  results,
132
- page_query,
133
- style: Style::new("".to_string(), "".to_string()),
134
- engine_errors_info,
135
  }
136
  }
137
 
138
  /// A setter function to add website style to the return search results.
139
- pub fn add_style(&mut self, style: Style) {
140
- self.style = style;
141
  }
142
  }
 
2
  //! data scraped from the upstream search engines.
3
 
4
  use serde::{Deserialize, Serialize};
5
+ use smallvec::SmallVec;
6
 
7
  use crate::{config::parser_models::Style, engines::engine_models::EngineError};
8
 
 
17
  /// (href url in html in simple words).
18
  /// * `description` - The description of the search result.
19
  /// * `engine` - The names of the upstream engines from which this results were provided.
20
+ #[derive(Clone, Serialize, Deserialize, Debug)]
21
  #[serde(rename_all = "camelCase")]
22
  pub struct SearchResult {
23
  pub title: String,
24
  pub url: String,
25
  pub description: String,
26
+ pub engine: SmallVec<[String; 0]>,
27
  }
28
 
29
  impl SearchResult {
 
36
  /// (href url in html in simple words).
37
  /// * `description` - The description of the search result.
38
  /// * `engine` - The names of the upstream engines from which this results were provided.
39
+ pub fn new(title: &str, url: &str, description: &str, engine: &[&str]) -> Self {
40
  SearchResult {
41
+ title: title.to_owned(),
42
+ url: url.to_owned(),
43
+ description: description.to_owned(),
44
+ engine: engine.iter().map(|name| name.to_string()).collect(),
45
  }
46
  }
47
 
 
50
  /// # Arguments
51
  ///
52
  /// * `engine` - Takes an engine name provided as a String.
53
+ pub fn add_engines(&mut self, engine: &str) {
54
+ self.engine.push(engine.to_owned())
55
  }
56
 
57
  /// A function which returns the engine name stored from the struct as a string.
 
59
  /// # Returns
60
  ///
61
  /// An engine name stored as a string from the struct.
62
+ pub fn engine(&mut self) -> String {
63
+ std::mem::take(&mut self.engine[0])
64
  }
65
  }
66
 
67
+ #[derive(Serialize, Deserialize, Clone)]
 
68
  pub struct EngineErrorInfo {
69
  pub error: String,
70
  pub engine: String,
 
72
  }
73
 
74
  impl EngineErrorInfo {
75
+ pub fn new(error: &EngineError, engine: &str) -> Self {
76
  Self {
77
  error: match error {
78
+ EngineError::RequestError => "RequestError".to_owned(),
79
+ EngineError::EmptyResultSet => "EmptyResultSet".to_owned(),
80
+ EngineError::UnexpectedError => "UnexpectedError".to_owned(),
81
  },
82
+ engine: engine.to_owned(),
83
  severity_color: match error {
84
+ EngineError::RequestError => "green".to_owned(),
85
+ EngineError::EmptyResultSet => "blue".to_owned(),
86
+ EngineError::UnexpectedError => "red".to_owned(),
87
  },
88
  }
89
  }
 
108
  pub results: Vec<SearchResult>,
109
  pub page_query: String,
110
  pub style: Style,
111
+ pub engine_errors_info: SmallVec<[EngineErrorInfo; 0]>,
112
  }
113
 
114
  impl SearchResults {
 
124
  /// given search query.
125
  pub fn new(
126
  results: Vec<SearchResult>,
127
+ page_query: &str,
128
+ engine_errors_info: &[EngineErrorInfo],
129
  ) -> Self {
130
+ Self {
131
  results,
132
+ page_query: page_query.to_owned(),
133
+ style: Style::default(),
134
+ engine_errors_info: SmallVec::from(engine_errors_info),
135
  }
136
  }
137
 
138
  /// A setter function to add website style to the return search results.
139
+ pub fn add_style(&mut self, style: &Style) {
140
+ self.style = style.to_owned();
141
  }
142
  }