Skip to main content

datex_core/values/value/value_classification/
mod.rs

1mod equality;
2pub mod serde_dif;
3pub mod unresolved_value_classification;
4use crate::{
5    prelude::*, shared_values::PointerAddress, types::entity_type::EntityType,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
9pub struct ValueTag {
10    pub name: String,
11    /// if set to true, the inner value is expected to be null but treated as non-existing (#Example instead of #Example(null))
12    pub is_empty: bool,
13}
14
15impl From<String> for ValueTag {
16    fn from(tag: String) -> Self {
17        ValueTag {
18            name: tag,
19            is_empty: false,
20        }
21    }
22}
23
24impl From<&str> for ValueTag {
25    fn from(tag: &str) -> Self {
26        ValueTag {
27            name: tag.to_string(),
28            is_empty: false,
29        }
30    }
31}
32
33#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
34pub struct ValueClassification {
35    /// a nominal (entity) type associated with the value (e.g. Example {...})
36    pub entity_type: Option<EntityType>,
37    /// list of impls that are associated with the value (e.g. null + $1234 + $5678)
38    pub impls: Vec<PointerAddress>,
39    /// a value tag, e.g. #Tagged(42)
40    pub tag: Option<ValueTag>,
41}
42
43impl ValueClassification {
44    /// Creates a new [ValueClassification] with no entity, no impls, and no tag.
45    pub fn new_unclassified() -> Self {
46        ValueClassification::default()
47    }
48
49    /// Creates a new []ValueClassification with the specified entity type.
50    pub fn new_with_entity(entity: EntityType) -> Self {
51        ValueClassification {
52            entity_type: Some(entity),
53            ..Default::default()
54        }
55    }
56
57    /// Creates a new [ValueClassification] with the specified impls.
58    pub fn new_with_impls(impls: Vec<PointerAddress>) -> Self {
59        ValueClassification {
60            impls,
61            ..Default::default()
62        }
63    }
64
65    /// Creates a new [ValueClassification] with the specified tag.
66    pub fn new_with_tag(tag: impl Into<ValueTag>) -> Self {
67        ValueClassification {
68            tag: Some(tag.into()),
69            ..Default::default()
70        }
71    }
72
73    /// Creates a new [ValueClassification] with an optional tag.
74    /// If the tag is None, the classification will be unclassified.
75    pub fn new_with_maybe_tag(tag: Option<impl Into<ValueTag>>) -> Self {
76        ValueClassification {
77            tag: tag.map(|t| t.into()),
78            ..Default::default()
79        }
80    }
81
82    /// Merges the current [ValueClassification] with another one, combining their entity, impls, and tag.
83    /// Prioritizes the current entity and tag if they exist; otherwise, uses the other classification's values.
84    pub fn merge(&self, other: ValueClassification) -> ValueClassification {
85        let mut merged_impls = self.impls.clone();
86        merged_impls.extend(other.impls);
87
88        ValueClassification {
89            entity_type: self.entity_type.clone().or(other.entity_type),
90            impls: merged_impls,
91            tag: self.tag.clone().or(other.tag),
92        }
93    }
94
95    /// Returns the tag string if it exists, otherwise returns None.
96    pub fn tag_str(&self) -> Option<&str> {
97        match &self.tag {
98            Some(value_tag) => Some(&value_tag.name),
99            None => None,
100        }
101    }
102
103    pub fn is_unclassified(&self) -> bool {
104        self.entity_type.is_none()
105            && self.impls.is_empty()
106            && self.tag.is_none()
107    }
108}
109
110impl From<EntityType> for ValueClassification {
111    fn from(entity_type: EntityType) -> Self {
112        ValueClassification {
113            entity_type: Some(entity_type),
114            ..Default::default()
115        }
116    }
117}
118
119impl From<Option<EntityType>> for ValueClassification {
120    fn from(entity_type: Option<EntityType>) -> Self {
121        ValueClassification {
122            entity_type,
123            ..Default::default()
124        }
125    }
126}
127
128impl From<Vec<PointerAddress>> for ValueClassification {
129    fn from(impls: Vec<PointerAddress>) -> Self {
130        ValueClassification::new_with_impls(impls)
131    }
132}
133
134impl From<String> for ValueClassification {
135    fn from(tag: String) -> Self {
136        ValueClassification::new_with_tag(tag)
137    }
138}