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
use crate::field_types::FieldTypeSubobjectArray;
use crate::index::can_move_object as index_can_move_object;
use crate::model::Model;
use crate::IndexEntryProperty;
use crate::{Hit, HitError};
use std::rc::Rc;

// TODO : that's not generic, it should use the allow_model method
// this prevents from creating custom subobject fields with custom rules :/
fn _can_move_object(
    index: &Hit,
    id: &str,
    target_model: Rc<Model>,
    property: &str,
) -> Result<(), HitError> {
    // TODO : should this be implemented ?
    let model = index
        .get_model(id)
        .ok_or(HitError::NoModelForId(id.to_string()))?;
    let target_field = target_model
        .get_field(property)
        .ok_or(HitError::PropertyNotFound(property.to_string()))?
        .borrow();
    let target_field = target_field
        .downcast_ref::<FieldTypeSubobjectArray>()
        .ok_or(HitError::InvalidMoveDestination())?;
    for allowed_model in target_field.authorized_models.iter() {
        if allowed_model == model.get_name() {
            return Ok(());
        }
        for interface in model.interfaces.iter() {
            if allowed_model == interface {
                return Ok(());
            }
        }
    }
    Err(HitError::ModelNotAllowed(model.get_name().clone()))
}

pub fn can_move_object(
    index: &Hit,
    id: &str,
    target_id: &str,
    target_model: &str,
    property: &str,
) -> Result<(), HitError> {
    index_can_move_object(
        &index.index,
        id,
        IndexEntryProperty {
            id: target_id.to_string(),
            property: property.to_string(),
        },
        None,
    )?;
    let target_model = index.kernel.get_model(target_model)?;
    return _can_move_object(index, id, target_model, property);
}