datex-core 0.0.13

The DATEX Core Rust implementation
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! This module contains the implementation of the [Value] struct, which represents a value in the DATEX type system.
//! A [Value] consists of a [CoreValue] representation and an optional custom type.
use crate::{
    prelude::*,
    runtime::cache::shared_references_cache::SharedReferencesCache,
    traits::convert_value::ConvertValue,
    types::type_definition::{
        TypeDefinition, callable::CallableTypeDefinition,
    },
    values::{
        core_value::CoreValue,
        core_value_with_classification::CoreValueWithClassification,
        core_values::{
            callable::{Callable, CallableBody},
            native::DatexNative,
        },
        value_container::ValueContainer,
    },
};
mod apply;
pub mod borrowed_value;
mod classification;
mod convert_parts;
mod convert_value;
mod datex_hash;
mod datex_native;
mod equality;
mod get_core_lib_type_id;
mod get_datex_type;
mod iter_parts;
mod local_child_path_resolver;
mod ops;
mod serde_dif;
#[cfg(feature = "ast")]
mod to_datex_expression_data;
mod to_instructions;
mod update_handler;
mod value_access;
pub mod value_classification;

use crate::{
    shared_values::errors::AccessError,
    traits::{
        classification::Classification, convert_parts::FromParts,
        value_access::ValueAccess,
    },
    utils::impl_display_for_datex_value::impl_display_for_datex_value,
    value_updates::update_handler::InternalMutabilityUpdateHandler,
    values::{
        core_values::{
            endpoint::Endpoint, native::NativeCoreValue, text::Text,
        },
        value::value_classification::{
            ValueClassification,
            unresolved_value_classification::UnresolvedValueClassification,
        },
        value_container::borrowed_value_container::{
            BorrowedValueContainer, BorrowedValueContainerMut,
        },
    },
};
use core::{
    fmt::{Debug, Formatter},
    result::Result,
};

mod try_clone;

#[derive(Debug, Clone)]
/// Represents a local DATEX value.
/// This can either be a core value with classification or a
/// native rust value that provides all trait implementations.
pub enum Value {
    /// Core value with classification
    Core(CoreValueWithClassification),
    /// Native rust value with DATEX representation
    Native(NativeCoreValue),
}

impl<T: ConvertValue> From<T> for Value {
    fn from(inner: T) -> Self {
        inner.to_value()
    }
}

impl From<&'static str> for Value {
    fn from(value: &'static str) -> Self {
        Value::core(CoreValue::Text(Text::StaticStr(value)))
    }
}

impl Value {
    /// Creates a new [Value] from a native value that implements the [DatexNative] trait.
    pub fn native(value: impl DatexNative) -> Value {
        Value::Native(NativeCoreValue::new(value))
    }
    pub fn native_boxed(value: Box<dyn DatexNative>) -> Value {
        Value::Native(NativeCoreValue { value })
    }

    pub fn core(inner: impl Into<CoreValueWithClassification>) -> Self {
        Value::Core(inner.into())
    }

    pub fn core_with_classification(
        inner: impl Into<CoreValue>,
        classification: ValueClassification,
    ) -> Self {
        Value::Core(CoreValueWithClassification {
            inner: inner.into(),
            classification,
        })
    }

    pub fn is_collection_value(&self) -> bool {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.is_collection_value()
            }
            Value::Native(_) => false,
        }
    }

    pub fn null() -> Self {
        CoreValue::Null.into()
    }

    pub fn uninitialized() -> Self {
        CoreValue::Uninitialized.into()
    }

    /// Returns the classification of the value, which includes its entity type, impls, and tag.
    pub fn classification(
        &self,
        cache: &mut SharedReferencesCache,
    ) -> ValueClassification {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.classification.clone()
            }
            Value::Native(native) => native.classification(cache),
        }
    }

    /// Returns the unresolved classification of the value, which includes its entity type, impls, and tag.
    /// In contrast to `classification`, this method does not require a mutable reference to a cache
    /// and only returns an address for an entity type if it is present, without resolving it to a full type.
    pub fn unresolved_classification(&self) -> UnresolvedValueClassification {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.classification.clone().into()
            }
            Value::Native(native) => native.unresolved_classification(),
        }
    }

    pub fn new(inner: impl ConvertValue) -> Self {
        inner.to_value()
    }

    /// Checks if the inner [CoreValue] of the [Value] is a [CoreValue::Native].
    pub fn is_native(&self) -> bool {
        matches!(&self, Value::Native(_))
    }

    pub fn is_uninitialized(&self) -> bool {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.is_uninitialized()
            }
            Value::Native(_) => false,
        }
    }

    /// Strips any local observers from the given value container.
    /// This method should be called when a value is moved from its [SharedContainer] parent.
    pub fn without_local_observers(mut self) -> Value {
        self.set_update_callback_data(None);
        self
    }

    /// Creates a new Value representing a boxed value.
    /// This can be used to wrap a [ValueContainer] directly into a local [Value],
    /// e.g. for #Tagged(shared X) or (X | null) | null
    pub fn boxed(value: impl Into<ValueContainer>) -> Self {
        Value::from(CoreValue::Box(Box::new(value.into())))
    }
    pub fn unbox(self) -> Result<ValueContainer, Value> {
        match self {
            Value::Core(core_value_with_classification) => {
                match core_value_with_classification.inner {
                    CoreValue::Box(boxed) => Ok(*boxed),
                    _ => Err(Value::Core(core_value_with_classification)),
                }
            }
            _ => Err(self),
        }
    }

    /// Tries to get a borrow of the current value as the specified type.
    /// Does not perform any type conversion.
    pub fn try_as<T>(&self) -> Option<&T>
    where
        T: ConvertValue + ?Sized,
    {
        T::try_borrow_from_value(self).ok()
    }

    pub fn try_as_mut<T>(&mut self) -> Option<&mut T>
    where
        T: ConvertValue,
    {
        T::try_borrow_mut_from_value(self).ok()
    }

    /// Tries to get an immutable reference to the value as a string slice.
    /// Does not perform any type conversion.
    /// In contrast to [Value::try_as::<String>()], this method also works for [Text] values containing a static string slice.
    pub fn try_as_str(&self) -> Option<&str> {
        if let Some(string) = self.try_as::<String>() {
            Some(string.as_str())
        }
        // FIXME: must be handled explicitly because 'static str cannot be converted to String
        else if let Some(text) = self.try_as::<Text>() {
            Some(text.as_str())
        } else {
            None
        }
    }

    /// Tries to convert the current value into the specific specified type.
    /// Does not perform any type conversion.
    pub fn try_into_value<T>(self) -> Result<T, Value>
    where
        T: ConvertValue,
    {
        T::try_from_value(self)
    }

    /// Tries to convert the current value into the specific specified type.
    /// Performs a cast between native and core values if necessary, e.g. converting a [CoreValue::Map] into a native rust struct.
    pub fn try_convert_into_value<T>(self) -> Result<T, ()>
    where
        T: ConvertValue + FromParts,
    {
        T::try_convert_from_value(self)
    }

    /// Tries to get a reference to the inner [CoreValue] if the [Value] is a [Value::Core] variant.
    pub fn try_as_core_value(&self) -> Option<&CoreValue> {
        match self {
            Value::Core(core_value_with_classification) => {
                Some(&core_value_with_classification.inner)
            }
            Value::Native(_native) => None,
        }
    }

    /// Tries to get a mutable reference to the inner [CoreValue] if the [Value] is a [Value::Core] variant.
    pub fn try_as_core_value_mut(&mut self) -> Option<&mut CoreValue> {
        match self {
            Value::Core(core_value_with_classification) => {
                Some(&mut core_value_with_classification.inner)
            }
            Value::Native(_native) => None,
        }
    }

    /// Tries to convert the current value into a [CoreValue] if the [Value] is a [Value::Core] variant.
    pub fn try_into_core_value(self) -> Result<CoreValue, Value> {
        match self {
            Value::Core(core_value_with_classification) => {
                Ok(core_value_with_classification.inner)
            }
            Value::Native(native) => Err(Value::Native(native)),
        }
    }
}

impl Value {
    pub fn callable(
        name: Option<String>,
        signature: CallableTypeDefinition,
        body: CallableBody,
        creator: Endpoint,
    ) -> Self {
        Value::new(CoreValue::Callable(Callable {
            name,
            signature,
            body,
            creator,
            docs: None,
        }))
    }

    pub fn is_null(&self) -> bool {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.is_null()
            }
            Value::Native(_native_core_value) => false, // TODO: handle native null values
        }
    }

    /// Tries to downcast the CoreValue to a native value of type T.
    /// This method will return Some(Box<T>) if the CoreValue is a Native variant and the underlying value can be downcast to T.
    pub fn downcast_native<T: DatexNative>(self) -> Option<Box<T>> {
        if let Value::Native(native_value) = self {
            native_value.into_any().downcast::<T>().ok()
        } else {
            None
        }
    }

    /// Tries to downcast the CoreValue to a reference of a native value of type T.
    /// This method will return Some(&T) if the CoreValue is a Native variant and the underlying value can be downcast to T.
    pub fn downcast_native_ref<T: DatexNative>(&self) -> Option<&T> {
        if let Value::Native(native_value) = self {
            native_value.as_any().downcast_ref::<T>()
        } else {
            None
        }
    }

    /// Tries to downcast the CoreValue to a mutable reference of a native value of type T.
    /// This method will return Some(&mut T) if the CoreValue is a Native variant and the underlying value can be downcast to T.
    pub fn downcast_native_mut<T: DatexNative>(&mut self) -> Option<&mut T> {
        if let Value::Native(native_value) = self {
            native_value.as_any_mut().downcast_mut::<T>()
        } else {
            None
        }
    }

    /// Gets a property on the value if applicable (e.g. for map and structs)
    pub fn try_get_property(
        &self,
        key: &ValueContainer,
    ) -> Result<BorrowedValueContainer<'_>, AccessError> {
        <Self as ValueAccess>::try_get_property(self, key)
    }

    pub fn try_get_property_mut(
        &mut self,
        key: &ValueContainer,
    ) -> Result<BorrowedValueContainerMut<'_>, AccessError> {
        <Self as ValueAccess>::try_get_property_mut(self, key)
    }

    /// Returns the actual current [TypeDefinition] of the value
    pub fn actual_type(&self) -> TypeDefinition {
        match self {
            Value::Core(core_value_with_classification) => {
                core_value_with_classification.actual_type()
            }
            Value::Native(_native) => todo!(),
        }
    }
}

impl_display_for_datex_value!(
    Value,
    impl core::fmt::Display for Value {
        fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
            match self {
                Value::Core(core_value_with_classification) => {
                    core::write!(f, "{}", core_value_with_classification)
                }
                Value::Native(native) => {
                    core::write!(f, "{}", native)
                }
            }
        }
    }
);

#[cfg(test)]
/// Tests for the Value struct and its methods.
/// This module contains unit tests for the Value struct, including its methods and operations.
/// The value is a holder for a combination of a CoreValue representation and its actual type.
mod tests {
    use super::*;
    use crate::{
        traits::structural_eq::assert_structural_eq,
        values::core_values::{
            endpoint::Endpoint,
            integer::{Integer, typed_integer::TypedInteger},
            list::{List, datex_list},
        },
    };
    use core::assert_matches;
    use log::info;

    #[test]
    fn endpoint() {
        let endpoint = Value::from(Endpoint::new("@test"));
        assert!(!endpoint.is_native());

        let endpoint_value = endpoint.try_into_value::<Endpoint>().unwrap();
        assert_eq!(endpoint_value.to_string(), "@test");

        let endpoint = Value::from(CoreValue::Endpoint(Endpoint::new("@test")));
        assert!(!endpoint.is_native());
        assert_eq!(
            endpoint.try_into_value::<Endpoint>().unwrap().to_string(),
            "@test"
        );
    }

    #[test]
    fn new_addition_assignments() {
        let mut x = Value::from(42i8);
        let y = Value::from(27i8);

        x += y.clone();
        assert_eq!(x, Value::from(69i8));
    }

    #[test]
    fn new_additions() {
        let x = Value::from(42i8);
        let y = Value::from(27i8);

        let z = (x.clone() + y.clone()).unwrap();
        assert_eq!(z, Value::from(69i8));
    }

    #[test]
    fn list() {
        let mut a = List::from(vec![
            Value::from("42".to_string()),
            Value::from(42),
            Value::from(true),
        ]);

        a.push(Value::from(42));
        a.push(4);

        assert_eq!(a.len(), 5);

        let b = List::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        assert_eq!(b.len(), 11);

        let c = datex_list![1, "test".to_string(), 3, true, false];
        assert_eq!(c.len(), 5);
        assert_eq!(c[0], 1.into());
        assert_eq!(c[1], "test".to_string().into());
        assert_eq!(c[2], 3.into());
    }

    #[test]
    fn boolean() {
        let a = Value::from(CoreValue::Boolean(true.into()));
        let b = Value::from(CoreValue::Boolean(false.into()));
        let c = Value::from(CoreValue::Boolean(false.into()));
        assert_ne!(a, b);
        assert_eq!(b, c);

        let d = (!&b).unwrap();
        assert_eq!(a, d);

        // We can't add two booleans together, so this should return None
        let a_plus_b = &a + &b;
        assert!(a_plus_b.is_err());
    }

    #[test]
    fn equality_same_type() {
        let a = Value::from(42i8);
        let b = Value::from(42i8);
        let c = Value::from(27i8);

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);

        info!("{} === {}", a.clone(), b.clone());
        info!("{} !== {}", a.clone(), c.clone());
    }

    #[test]
    fn decimal() {
        let a = Value::from(42.1f32);
        let b = Value::from(27f32);

        let a_plus_b = (a.clone() + b.clone()).unwrap();
        assert_eq!(a_plus_b, Value::from(69.1f32));
        info!("{} + {} = {}", a.clone(), b.clone(), a_plus_b);
    }

    #[test]
    fn null() {
        let null_value = Value::null();
        assert_eq!(null_value.to_string(), "null");

        let maybe_value: Option<i8> = None;
        let null_value = Value::from(maybe_value);
        // can only get None via cast
        assert_eq!(
            null_value.try_convert_into_value::<Option<i8>>().unwrap(),
            None
        );
    }

    #[test]
    fn addition() {
        let a = Value::from(42i8);
        let b = Value::from(27i8);

        let a_plus_b = (a.clone() + b.clone()).unwrap();
        assert_eq!(a_plus_b, Value::from(69i8));
        info!("{} + {} = {}", a.clone(), b.clone(), a_plus_b);
    }

    #[test]
    fn string_concatenation() {
        let a = Value::from("Hello ".to_string());
        let b = Value::from(TypedInteger::I8(42i8));
        assert!(matches!(a.try_as_core_value().unwrap(), CoreValue::Text(_)));
        assert!(matches!(
            b.try_as::<TypedInteger>().unwrap(),
            TypedInteger::I8(_)
        ));

        let a_plus_b = (&a + &b).unwrap();
        let b_plus_a = (&b + &a).unwrap();

        assert!(matches!(
            a_plus_b.try_as_core_value().unwrap(),
            CoreValue::Text(_)
        ));
        assert!(matches!(
            b_plus_a.try_as_core_value().unwrap(),
            CoreValue::Text(_)
        ));

        assert_eq!(a_plus_b, Value::from("Hello 42".to_string()));
        assert_eq!(b_plus_a, Value::from("42Hello ".to_string()));

        info!("{} + {} = {}", a.clone(), b.clone(), a_plus_b);
        info!("{} + {} = {}", b.clone(), a.clone(), b_plus_a);
    }

    #[test]
    fn structural_equality() {
        let a = Value::from(42_i8);
        let b = Value::from(42_i32);
        assert_matches!(
            a.try_as_core_value().unwrap(),
            CoreValue::TypedInteger(TypedInteger::I8(_))
        );
        assert_matches!(
            b.try_as_core_value().unwrap(),
            CoreValue::TypedInteger(TypedInteger::I32(_))
        );
        assert_ne!(a, b);

        assert_structural_eq!(a, b);

        assert_structural_eq!(
            Value::from(TypedInteger::I8(42)),
            Value::from(TypedInteger::U32(42)),
        );

        assert_structural_eq!(
            Value::from(42_i8),
            Value::from(Integer::from(42_i8))
        );
    }
}