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
use crate::index::list_helpers::get_parent_property_value; use crate::index::{Index, IndexEntryProperty}; use crate::object_data::ObjectValue; use crate::object_data::Reference; use crate::HitError; pub fn remove_reference_from_parent_array_from_property( index: &mut Index, parent: IndexEntryProperty, id: &str, ) -> Result<ObjectValue, HitError> { let entry = index .get(&parent.id) .ok_or(HitError::IDNotFound(parent.id.to_string()))?; let data = get_parent_property_value(&entry, &parent); let new_data = mutate_remove_from_reference_array(data, id)?; match new_data { Some(new_data) => { entry .borrow_mut() .data .insert(parent.property, ObjectValue::VecReference(new_data.clone())); Ok(ObjectValue::VecReference(new_data)) } None => { entry .borrow_mut() .data .insert(parent.property, ObjectValue::Null); Ok(ObjectValue::Null) } } } fn mutate_remove_from_reference_array( data: ObjectValue, id: &str, ) -> Result<Option<Vec<Reference>>, HitError> { match data { ObjectValue::VecReference(data) => { let mut data = data.clone(); data.retain(|x| x.id != id); if data.len() == 0 { return Ok(None); } Ok(Some(data)) } _ => Err(HitError::CannotRemoveReferenceFromThisDataType()), } }