Skip to main content

datex_core/traits/
classification.rs

1use crate::{
2    prelude::*,
3    runtime::cache::shared_references_cache::SharedReferencesCache,
4    shared_values::PointerAddress,
5    types::entity_type::EntityType,
6    values::value::value_classification::{
7        ValueClassification, ValueTag,
8        unresolved_value_classification::UnresolvedValueClassification,
9    },
10};
11
12pub trait Classification {
13    /// Returns the DATEX [EntityType] of the native value if it has an entity type.
14    /// For structural types, this will return None.
15    /// The default implementation returns None, indicating that the value does not have an entity type.
16    fn entity_type(
17        &self,
18        _cache: &mut SharedReferencesCache,
19    ) -> Option<EntityType> {
20        None
21    }
22
23    /// Returns the [PointerAddress] of the entity type if it has one.
24    /// The default implementation returns None, indicating that the value does not have an entity type address.
25    /// It must be ensured that the entity type for the given address is already registered in the runtime.
26    fn entity_type_address(&self) -> Option<PointerAddress> {
27        None
28    }
29
30    /// Returns a [ValueTag] if the value has a tag.
31    /// The default implementation returns None, indicating that the value does not have a tag.
32    fn tag(&self) -> Option<ValueTag> {
33        None
34    }
35
36    /// Returns a list of [PointerAddress]es of the impls associated with the value.
37    /// The default implementation returns an empty list, indicating that the value does not have any impls.
38    fn impls(&self) -> Vec<PointerAddress> {
39        Vec::new()
40    }
41
42    /// Returns the DATEX [ValueClassification] of the native value.
43    /// This tries to resolve the entity type and tag, assuming at most one of them is present.
44    fn classification(
45        &self,
46        cache: &mut SharedReferencesCache,
47    ) -> ValueClassification {
48        let impls = self.impls();
49        let entity_type = self.entity_type(cache);
50        let tag = self.tag();
51
52        ValueClassification {
53            entity_type,
54            impls,
55            tag,
56        }
57    }
58
59    /// Returns the unresolved DATEX [UnresolvedValueClassification] of the native value.
60    /// This does not try to resolve the entity, but instead returns the address of the entity type if it has one.
61    fn unresolved_classification(&self) -> UnresolvedValueClassification {
62        let impls = self.impls();
63        let entity_type_address = self.entity_type_address();
64        let tag = self.tag();
65
66        UnresolvedValueClassification {
67            entity_type_address,
68            impls,
69            tag,
70        }
71    }
72}