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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! This module contains the implementation of the [ValueContainer] enum, which represents a container for values in the DATEX type system.
//! A [ValueContainer] can either be a local value, which directly contains a [Value], or a shared value, which contains a reference to a [SharedContainer].
use crate::values::value_container::value_key::BorrowedValueKey;
pub mod equality;
pub mod identity;
use core::result::Result;
pub mod serde_dif;
use super::value::Value;
use crate::{
prelude::*,
shared_values::{SharedContainer, errors::AccessError},
types::{
r#type::Type,
type_definition::TypeDefinition,
type_definition_with_metadata::{
TypeDefinitionWithMetadata, TypeMetadata,
},
},
values::core_value::CoreValue,
};
pub mod apply;
pub mod ops;
pub mod update_handler;
pub mod value_key;
use core::{
fmt::Display,
hash::{Hash, Hasher},
ops::FnOnce,
};
pub mod datex_proxy;
pub mod error;
#[derive(Debug, Eq, Clone)]
pub enum ValueContainer {
Local(Value),
Shared(SharedContainer),
}
impl ValueContainer {
/// Creates a new [ValueContainer::Local] from a [Value]
pub fn local(value: impl Into<Value>) -> Self {
ValueContainer::Local(value.into())
}
/// Calls a fn with a reference to the current inner collapsed value of the container
pub fn with_collapsed_value<R, F: FnOnce(&Value) -> R>(&self, f: F) -> R {
match self {
ValueContainer::Local(value) => f(value),
ValueContainer::Shared(shared) => shared.with_collapsed_value(f),
}
}
/// Calls a fn with a mutable reference to the current inner collapsed value of the container
pub(crate) fn with_collapsed_value_mut<R, F: FnOnce(&mut Value) -> R>(
&mut self,
f: F,
) -> R {
match self {
ValueContainer::Local(value) => f(value),
ValueContainer::Shared(shared) => {
shared.with_collapsed_value_mut(f)
}
}
}
/// Gets a cloned, collapsed inner value.
/// Use [ValueContainer::with_collapsed_value] instead whenever possible
/// or match the [ValueContainer]
pub fn get_cloned_value(&self) -> Value {
self.with_collapsed_value(|value| value.clone())
}
/// Tries to get an immutable reference to the value as a specified type.
/// Does not perform any type conversion.
/// This only works for local values, not for shared values.
pub fn try_as<'a, T>(&'a self) -> Option<&'a T>
where
&'a T: TryFrom<&'a CoreValue>,
{
match self {
ValueContainer::Local(value) => value.inner.try_as(),
ValueContainer::Shared(_) => None,
}
}
/// Tries to get a mutable reference to the value as a specified type.
/// Does not perform any type conversion.
/// This only works for local values, not for shared values.
pub fn try_as_mut<'a, T>(&'a mut self) -> Option<&'a mut T>
where
&'a mut T: TryFrom<&'a mut CoreValue>,
{
match self {
ValueContainer::Local(value) => value.inner.try_as_mut(),
ValueContainer::Shared(_) => None,
}
}
/// Tries to get the current collapsed value as a specified type.
/// Does not perform any type conversion.
/// Runs the provided closure with a reference to the typed value if the conversion was successful, otherwise returns None.
pub fn try_with<T, R, F>(&self, f: F) -> Option<R>
where
F: for<'a> FnOnce(&'a T) -> R,
for<'a> &'a T: TryFrom<&'a CoreValue>,
{
self.with_collapsed_value(|value| value.inner.try_as().map(f))
}
/// Tries to get the current collapsed value as a specified type.
/// Does not perform any type conversion.
/// This only works for local values, not for shared values.
pub fn try_into_value<T>(self) -> Option<T>
where
T: TryFrom<CoreValue>,
{
match self {
ValueContainer::Local(value) => value.inner.try_into().ok(),
ValueContainer::Shared(_) => None,
}
}
/// Performs a clone used by the "clone" command
/// Local values are just cloned normally
/// For shared value, the inner value container is cloned (shared x -> x)
pub fn get_cloned(&self) -> ValueContainer {
match self {
ValueContainer::Local(value) => {
ValueContainer::Local(value.clone())
}
ValueContainer::Shared(shared) => shared.value_container().clone(),
}
}
/// Returns the actual type of the contained value, resolving shared values if necessary.
pub fn actual_type(&self) -> TypeDefinition {
match self {
ValueContainer::Local(local) => local.actual_type().clone(),
ValueContainer::Shared(shared) => shared.actual_type().clone(),
}
}
/// Returns the actual type that describes the value container (e.g. integer or 'mut shared mut integer).
pub fn actual_container_type(&self) -> TypeDefinitionWithMetadata {
match self {
ValueContainer::Local(value) => TypeDefinitionWithMetadata {
definition: value.actual_type(),
metadata: TypeMetadata::default(),
reference_name: None,
},
ValueContainer::Shared(shared) => {
let inner_type =
shared.value_container().actual_container_type();
TypeDefinitionWithMetadata {
definition: TypeDefinition::Nested(Box::new(Type::from(
inner_type,
))),
metadata: TypeMetadata::Shared {
mutability: shared.container_mutability(),
ownership: shared.ownership(),
},
reference_name: None,
}
}
}
}
/// For local values, returns the actual type of the value container
/// For shared values, returns the allowed type of the value container
pub fn allowed_or_actual_type(&self) -> TypeDefinition {
match self {
ValueContainer::Local(value) => value.actual_type(),
ValueContainer::Shared(shared) => shared.allowed_type().clone(),
}
}
/// Returns the contained SharedContainer if it is a SharedContainer, otherwise returns None.
pub fn maybe_shared(&self) -> Option<&SharedContainer> {
if let ValueContainer::Shared(shared) = self {
Some(shared)
} else {
None
}
}
/// Runs a closure with the contained SharedContainer if it is a SharedContainer, otherwise returns None.
pub fn with_maybe_shared<F, R>(&self, f: F) -> Option<R>
where
F: FnOnce(&SharedContainer) -> R,
{
if let ValueContainer::Shared(shared) = self {
Some(f(shared))
} else {
None
}
}
/// Returns a reference to the contained SharedContainer, panics if it is not a SharedContainer.
pub fn shared_unchecked(&self) -> &SharedContainer {
match self {
ValueContainer::Shared(shared) => shared,
_ => {
core::panic!("Cannot convert ValueContainer to SharedContainer")
}
}
}
pub fn try_get_property<'a>(
&self,
key: impl Into<BorrowedValueKey<'a>>,
) -> Result<ValueContainer, AccessError> {
match self {
ValueContainer::Local(value) => value.try_get_property(key),
ValueContainer::Shared(reference) => {
reference.try_get_property(key)
}
}
}
}
impl<T: Into<Value>> From<T> for ValueContainer {
fn from(value: T) -> Self {
ValueContainer::Local(value.into())
}
}
impl<'a> From<BorrowedValueKey<'a>> for ValueContainer {
fn from(value_key: BorrowedValueKey) -> Self {
match value_key {
BorrowedValueKey::Text(text) => {
ValueContainer::Local(text.into_owned().into())
}
BorrowedValueKey::Index(index) => {
ValueContainer::Local(index.into())
}
BorrowedValueKey::Value(value_container) => {
value_container.into_owned()
}
}
}
}
impl Hash for ValueContainer {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
ValueContainer::Local(value) => value.hash(state),
ValueContainer::Shared(pointer) => pointer.hash(state),
}
}
}
impl Display for ValueContainer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ValueContainer::Local(value) => core::write!(f, "{value}"),
// TODO #118: only simple temporary way to distinguish between Value and Pointer
ValueContainer::Shared(reference) => reference
.with_collapsed_value_mut(|reference| {
write!(f, "&({})", reference)
}),
}
}
}