Create function to create fomatted_options

This commit is contained in:
Clémentine Urquizar 2021-06-15 17:16:07 +02:00
parent 5e656bb58a
commit 8d0269fcc4
No known key found for this signature in database
GPG Key ID: D8E7CC7422E77E1A

View File

@ -138,81 +138,26 @@ impl Index {
.sorted() .sorted()
.collect(); .collect();
let mut formatted_options = HashMap::new();
let attr_to_highlight = query let attr_to_highlight = query
.attributes_to_highlight .attributes_to_highlight
.unwrap_or_default(); .unwrap_or_default();
for attr in attr_to_highlight {
let new_format = FormatOptions {
highlight: true,
crop: None,
};
if attr == "*" {
let ids = displayed_ids.clone();
for id in ids {
formatted_options.insert(id, new_format);
}
break;
}
if let Some(id) = fields_ids_map.id(&attr) {
if displayed_ids.contains(&id) {
formatted_options.insert(id, new_format);
}
}
};
let attr_to_crop = query let attr_to_crop = query
.attributes_to_crop .attributes_to_crop
.unwrap_or_default(); .unwrap_or_default();
for attr in attr_to_crop { let formatted_options = parse_formatted_options(
let mut attr_name = attr.clone(); &attr_to_highlight,
let mut attr_len = Some(query.crop_length); &attr_to_crop,
query.crop_length,
if attr_name.contains(':') { &fields_ids_map,
let mut split = attr_name.rsplit(':'); &displayed_ids,
attr_len = match split.next() { );
Some(s) => s.parse::<usize>().ok(),
None => None,
};
attr_name = split.flat_map(|s| s.chars()).collect();
}
if attr_name == "*" {
let ids = displayed_ids.clone();
for id in ids {
let mut highlight = false;
if let Some(f) = formatted_options.get(&id) {
highlight = f.highlight;
}
formatted_options.insert(id, FormatOptions {
highlight,
crop: attr_len,
});
}
}
if let Some(id) = fields_ids_map.id(&attr_name) {
if displayed_ids.contains(&id) {
let mut highlight = false;
if let Some(f) = formatted_options.get(&id) {
highlight = f.highlight;
}
formatted_options.insert(id, FormatOptions {
highlight,
crop: attr_len,
});
}
}
}
// All attributes present in `_formatted`: // All attributes present in `_formatted`:
// - attributes asked to be highlighted or cropped (with `attributesToCrop` or `attributesToHighlight`) // - attributes asked to be highlighted or cropped (with `attributesToCrop` or `attributesToHighlight`)
// - attributes asked to be retrieved: these attributes will not be formatted // - attributes asked to be retrieved: these attributes will not be formatted
// - attributes that are present in displayed attributes
let ids_in_formatted = formatted_options let ids_in_formatted = formatted_options
.keys() .keys()
.cloned() .cloned()
@ -270,6 +215,81 @@ impl Index {
} }
} }
fn parse_formatted_options(
attr_to_highlight: &HashSet<String>,
attr_to_crop: &[String],
query_crop_length: usize,
fields_ids_map: &FieldsIdsMap,
displayed_ids: &HashSet<u8>,
) -> HashMap<FieldId, FormatOptions> {
let mut formatted_options = HashMap::new();
for attr in attr_to_highlight {
let new_format = FormatOptions {
highlight: true,
crop: None,
};
if attr == "*" {
let ids = displayed_ids.clone();
for id in ids {
formatted_options.insert(id, new_format);
}
break;
}
if let Some(id) = fields_ids_map.id(&attr) {
if displayed_ids.contains(&id) {
formatted_options.insert(id, new_format);
}
}
};
for attr in attr_to_crop {
let mut attr_name = attr.clone();
let mut attr_len = Some(query_crop_length);
if attr_name.contains(':') {
let mut split = attr_name.rsplit(':');
attr_len = match split.next() {
Some(s) => s.parse::<usize>().ok(),
None => None,
};
attr_name = split.flat_map(|s| s.chars()).collect();
}
if attr_name == "*" {
let ids = displayed_ids.clone();
for id in ids {
let mut highlight = false;
if let Some(f) = formatted_options.get(&id) {
highlight = f.highlight;
}
formatted_options.insert(id, FormatOptions {
highlight,
crop: attr_len,
});
}
}
if let Some(id) = fields_ids_map.id(&attr_name) {
if displayed_ids.contains(&id) {
let mut highlight = false;
if let Some(f) = formatted_options.get(&id) {
highlight = f.highlight;
}
formatted_options.insert(id, FormatOptions {
highlight,
crop: attr_len,
});
}
}
}
formatted_options
}
fn make_document( fn make_document(
attributes_to_retrieve: &[FieldId], attributes_to_retrieve: &[FieldId],
field_ids_map: &FieldsIdsMap, field_ids_map: &FieldsIdsMap,