hit_data/hit_mod/helpers/
hit_move_helper.rs

1use crate::field_types::FieldTypeSubobjectArray;
2use crate::index::can_move_object as index_can_move_object;
3use crate::model::Model;
4use crate::IndexEntryProperty;
5use crate::{Hit, HitError};
6use std::rc::Rc;
7
8// TODO : that's not generic, it should use the allow_model method
9// this prevents from creating custom subobject fields with custom rules :/
10fn _can_move_object(
11    index: &Hit,
12    id: &str,
13    target_model: Rc<Model>,
14    property: &str,
15) -> Result<(), HitError> {
16    // TODO : should this be implemented ?
17    let model = index
18        .get_model(id)
19        .ok_or(HitError::NoModelForId(id.to_string()))?;
20    let target_field = target_model
21        .get_field(property)
22        .ok_or(HitError::PropertyNotFound(property.to_string()))?
23        .borrow();
24    let target_field = target_field
25        .downcast_ref::<FieldTypeSubobjectArray>()
26        .ok_or(HitError::InvalidMoveDestination())?;
27    for allowed_model in target_field.authorized_models.iter() {
28        if allowed_model == model.get_name() {
29            return Ok(());
30        }
31        for interface in model.interfaces.iter() {
32            if allowed_model == interface {
33                return Ok(());
34            }
35        }
36    }
37    Err(HitError::ModelNotAllowed(model.get_name().clone()))
38}
39
40pub fn can_move_object(
41    index: &Hit,
42    id: &str,
43    target_id: &str,
44    target_model: &str,
45    property: &str,
46) -> Result<(), HitError> {
47    index_can_move_object(
48        &index.index,
49        id,
50        IndexEntryProperty {
51            id: target_id.to_string(),
52            property: property.to_string(),
53        },
54        None,
55    )?;
56    let target_model = index.kernel.get_model(target_model)?;
57    return _can_move_object(index, id, target_model, property);
58}