refactor: changed Cookie to use Cow to facilitate using references when building

This commit is contained in:
ddotthomas 2024-01-10 14:14:14 -07:00
parent a47e28587c
commit 80e950de3b
2 changed files with 43 additions and 50 deletions

View file

@ -1,5 +1,7 @@
//! 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;
@ -23,23 +25,23 @@ pub struct SearchParams {
#[derive(Deserialize)]
pub struct Cookie<'a> {
/// It stores the theme name used in the website.
pub theme: &'a str,
pub theme: Cow<'a, str>,
/// It stores the colorscheme name used for the website theme.
pub colorscheme: &'a str,
pub colorscheme: Cow<'a, str>,
/// It stores the user selected upstream search engines selected from the UI.
pub engines: Vec<String>,
pub engines: Cow<'a, Vec<Cow<'a, str>>>,
/// It stores the user selected safe search level from the UI.
pub safe_search_level: u8,
}
impl<'a> Cookie<'a> {
/// server_models::Cookie contructor function
pub fn build(style: &'a Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
pub fn build(style: &'a Style, mut engines: Vec<Cow<'a, str>>, safe_search_level: u8) -> Self {
engines.sort();
Self {
theme: &style.theme,
colorscheme: &style.colorscheme,
engines,
theme: Cow::Borrowed(&style.theme),
colorscheme: Cow::Borrowed(&style.colorscheme),
engines: Cow::Owned(engines),
safe_search_level,
}
}