datex_core/values/value/value_classification/
mod.rs1mod 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 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 pub entity_type: Option<EntityType>,
37 pub impls: Vec<PointerAddress>,
39 pub tag: Option<ValueTag>,
41}
42
43impl ValueClassification {
44 pub fn new_unclassified() -> Self {
46 ValueClassification::default()
47 }
48
49 pub fn new_with_entity(entity: EntityType) -> Self {
51 ValueClassification {
52 entity_type: Some(entity),
53 ..Default::default()
54 }
55 }
56
57 pub fn new_with_impls(impls: Vec<PointerAddress>) -> Self {
59 ValueClassification {
60 impls,
61 ..Default::default()
62 }
63 }
64
65 pub fn new_with_tag(tag: impl Into<ValueTag>) -> Self {
67 ValueClassification {
68 tag: Some(tag.into()),
69 ..Default::default()
70 }
71 }
72
73 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 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 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}