Skip to content

Commit 154612b

Browse files
committed
Implement solution
1 parent a78ec53 commit 154612b

File tree

1 file changed

+92
-15
lines changed

1 file changed

+92
-15
lines changed

src/settings.rs

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use std::collections::HashMap;
2+
use std::{collections::HashMap, convert, hash::Hash};
33
use crate::{indexes::Index, errors::Error, request::{request, Method}, progress::{Progress, ProgressJson}};
44

55
/// Struct reprensenting a set of settings.
@@ -52,6 +52,75 @@ pub struct Settings {
5252
pub displayed_attributes: Option<Vec<String>>,
5353
}
5454

55+
pub trait IntoVecString {
56+
fn convert(self) -> Vec<String>;
57+
}
58+
59+
impl IntoVecString for &[&str] {
60+
#[inline]
61+
fn convert(self) -> Vec<String> {
62+
let mut vec = Vec::new();
63+
for item in self {
64+
vec.push((*item).into())
65+
}
66+
vec
67+
}
68+
}
69+
70+
impl IntoVecString for Vec<&str> {
71+
#[inline]
72+
fn convert(self) -> Vec<String> {
73+
let mut vec = Vec::new();
74+
for item in self {
75+
vec.push((*item).into())
76+
}
77+
vec
78+
}
79+
}
80+
81+
impl IntoVecString for Vec<String> {
82+
#[inline]
83+
fn convert(self) -> Vec<String> {
84+
self
85+
}
86+
}
87+
88+
impl IntoVecString for &[String] {
89+
#[inline]
90+
fn convert(self) -> Vec<String> {
91+
let mut vec = Vec::new();
92+
for item in self {
93+
vec.push(item.clone())
94+
}
95+
vec
96+
}
97+
}
98+
99+
impl IntoVecString for &[&String] {
100+
#[inline]
101+
fn convert(self) -> Vec<String> {
102+
let mut vec = Vec::new();
103+
for item in self {
104+
vec.push((*item).clone())
105+
}
106+
vec
107+
}
108+
}
109+
110+
/*
111+
TODO: Implement IntoVecString trought const generics as soon as they are stable.
112+
113+
impl<const N: usize> IntoVecString for &[String; N] {
114+
fn convert(self) -> Vec<String> {
115+
let mut vec = Vec::new();
116+
for item in self {
117+
vec.push((*item).clone())
118+
}
119+
vec
120+
}
121+
}
122+
*/
123+
55124
#[allow(missing_docs)]
56125
impl Settings {
57126
/// Create undefined settings
@@ -66,45 +135,53 @@ impl Settings {
66135
displayed_attributes: None,
67136
}
68137
}
69-
pub fn with_synonyms(self, synonyms: HashMap<String, Vec<String>>) -> Settings {
138+
pub fn with_synonyms<T: Into<String>, U: IntoVecString>(self, synonyms: HashMap<T, U>) -> Settings {
139+
let mut converted_synonyms = HashMap::new();
140+
for (key, array) in synonyms {
141+
let key: String = key.into();
142+
let array: Vec<String> = array.convert();
143+
converted_synonyms.insert(key, array);
144+
}
145+
70146
Settings {
71-
synonyms: Some(synonyms),
147+
synonyms: Some(converted_synonyms),
72148
..self
73149
}
74150
}
75-
pub fn with_stop_words(self, stop_words: Vec<String>) -> Settings {
151+
pub fn with_stop_words(self, stop_words: impl IntoVecString) -> Settings {
76152
Settings {
77-
stop_words: Some(stop_words),
153+
stop_words: Some(stop_words.convert()),
78154
..self
79155
}
80156
}
81-
pub fn with_ranking_rules(self, ranking_rules: Vec<String>) -> Settings {
157+
pub fn with_ranking_rules<T: IntoVecString>(self, ranking_rules: T) -> Settings
158+
{
82159
Settings {
83-
ranking_rules: Some(ranking_rules),
160+
ranking_rules: Some(ranking_rules.convert()),
84161
..self
85162
}
86163
}
87-
pub fn with_attributes_for_faceting(self, attributes_for_faceting: Vec<String>) -> Settings {
164+
pub fn with_attributes_for_faceting<T: IntoVecString>(self, attributes_for_faceting: T) -> Settings {
88165
Settings {
89-
attributes_for_faceting: Some(attributes_for_faceting),
166+
attributes_for_faceting: Some(attributes_for_faceting.convert()),
90167
..self
91168
}
92169
}
93-
pub fn with_distinct_attribute(self, distinct_attribute: String) -> Settings {
170+
pub fn with_distinct_attribute<T: Into<String>>(self, distinct_attribute: T) -> Settings {
94171
Settings {
95-
distinct_attribute: Some(distinct_attribute),
172+
distinct_attribute: Some(distinct_attribute.into()),
96173
..self
97174
}
98175
}
99-
pub fn with_searchable_attributes(self, searchable_attributes: Vec<String>) -> Settings {
176+
pub fn with_searchable_attributes<T: IntoVecString>(self, searchable_attributes: T) -> Settings {
100177
Settings {
101-
searchable_attributes: Some(searchable_attributes),
178+
searchable_attributes: Some(searchable_attributes.convert()),
102179
..self
103180
}
104181
}
105-
pub fn with_displayed_attributes(self, displayed_attributes: Vec<String>) -> Settings {
182+
pub fn with_displayed_attributes<T: IntoVecString>(self, displayed_attributes: T) -> Settings {
106183
Settings {
107-
displayed_attributes: Some(displayed_attributes),
184+
displayed_attributes: Some(displayed_attributes.convert()),
108185
..self
109186
}
110187
}

0 commit comments

Comments
 (0)