1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
use crate::model::Model; use crate::object_data::ObjectValues; use crate::plugins::DeletePlugin; use crate::plugins::InitEntryPlugin; use crate::plugins::Plugin; use crate::HitError; use crate::{hit_mod::HitEntry, ObjectValue}; use crate::{index::IndexEntryProperty, Hit}; use std::collections::HashMap; use std::rc::Rc; pub struct ModelTypeIndexer { index: HashMap<String, Vec<String>>, } impl ModelTypeIndexer { pub fn new() -> Self { ModelTypeIndexer { index: HashMap::new(), } } pub fn get(&self, model_type: &str) -> Option<&Vec<String>> { self.index.get(model_type) } fn add_to_index(&mut self, model: Rc<Model>, id: &str) { let vector = self .index .entry(model.get_name().to_string()) .or_insert(vec![]); vector.push(id.to_string()); } } impl InitEntryPlugin for ModelTypeIndexer { fn on_init_add_entry( &mut self, model: Rc<Model>, id: &str, _data: ObjectValues, _parent: Option<IndexEntryProperty>, ) { self.add_to_index(model, id); } } impl DeletePlugin for ModelTypeIndexer { fn on_before_delete_entry( &mut self, _entry: &HitEntry, _instance: &Hit, ) -> Result<(), HitError> { Ok(()) } fn on_after_delete_entry(&mut self, entry: &HitEntry, _instance: &Hit) -> Result<(), HitError> { let model = entry.get_model(); let model_type = model.get_name(); let id = entry.get_id(); match self.index.get_mut(model_type) { Some(vector) => vector.retain(|i| i != &id), None => {} }; Ok(()) } } impl Plugin for ModelTypeIndexer { fn on_before_add_entry( &mut self, _model: Rc<Model>, _id: &str, _data: ObjectValues, _parent: IndexEntryProperty, _instance: &Hit, ) { } fn on_after_add_entry( &mut self, model: Rc<Model>, id: &str, _data: ObjectValues, _parent: IndexEntryProperty, _instance: &Hit, ) { self.add_to_index(model, id); } fn on_after_set_value( &mut self, _property: IndexEntryProperty, _value: &ObjectValue, _old_value: &Option<ObjectValue>, _instance: &Hit, ) { } fn on_before_set_value( &mut self, _property: IndexEntryProperty, _value: &ObjectValue, _old_value: &Option<ObjectValue>, _instance: &Hit, ) { } fn on_before_move_subobject( &mut self, _id: &str, _target: IndexEntryProperty, _before_id: Option<String>, _instance: &Hit, ) -> Result<(), HitError> { Ok(()) } fn on_after_move_subobject( &mut self, _id: &str, _target: IndexEntryProperty, _original_parent: IndexEntryProperty, _before_id: Option<String>, _instance: &Hit, ) -> Result<(), HitError> { Ok(()) } }