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
use crate::node_wrappers::{BaseNodeTrait, FinalNode};
use crate::tao::archetype::ArchetypeTrait;
use crate::tao::form::FormTrait;
use crate::tao::relation::attribute::{Owner, Value};
use crate::Wrapper;

/// Interface for all attributes.
pub trait AttributeTrait: FormTrait + Wrapper<BaseType = FinalNode> {
    /// The Form representing the owner.
    type OwnerForm: FormTrait + From<FinalNode>;
    /// The Form representing the value.
    type ValueForm: FormTrait + From<FinalNode>;

    /// Set the owner for this attribute.
    fn set_owner(&mut self, owner: &Self::OwnerForm) {
        self.essence_mut()
            .add_outgoing(Owner::TYPE_ID, owner.essence());
    }

    /// The owner of an attribute, if it exists.
    fn owner(&self) -> Option<Self::OwnerForm> {
        self.essence()
            .outgoing_nodes(Owner::TYPE_ID)
            .get(0)
            .map(|n| Self::OwnerForm::from(*n))
    }

    /// Set the value for this attribute.
    fn set_value(&mut self, value: &Self::ValueForm) {
        self.essence_mut()
            .add_outgoing(Value::TYPE_ID, value.essence());
    }

    /// The value of an attribute, if it exists.
    fn value(&self) -> Option<Self::ValueForm> {
        self.essence()
            .outgoing_nodes(Value::TYPE_ID)
            .get(0)
            .map(|n| Self::ValueForm::from(*n))
    }
}