stam 0.18.7

STAM is a powerful library for dealing with stand-off annotations on text. This is the Rust library.
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
    STAM Library (Stand-off Text Annotation Model)
        by Maarten van Gompel <proycon@anaproy.nl>
        Digital Infrastucture, KNAW Humanities Cluster

        Licensed under the GNU General Public License v3

        https://github.com/annotation/stam-rust
*/

//! This module contains the API for [`DataValue`]. It defines and implements the
//! struct, the handle, and things like serialisation, deserialisation to STAM JSON.
//! It also implements the [`DataOperator`].
//! This API is used both on high and low levels.

use chrono::{DateTime, FixedOffset};
use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;

use crate::cbor::{cbor_decode_datetime, cbor_encode_datetime};
use crate::error::StamError;
use crate::types::*;
use datasize::{data_size, DataSize};
use sealed::sealed;
use std::ops::Deref;

#[sealed]
impl TypeInfo for DataValue {
    fn typeinfo() -> Type {
        Type::DataValue
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Encode, Decode)]
#[serde(tag = "@type", content = "value")]
/// This type encapsulates a value and its type.
/// It is held by [`AnnotationData`](crate::AnnotationData) alongside a reference to a [`DataKey`](crate::DataKey), resulting in a key/value pair.
pub enum DataValue {
    /// No value
    #[n(0)]
    Null,

    /// A string value
    #[n(1)]
    String(#[n(0)] String),

    /// A boolean value
    #[n(2)]
    Bool(#[n(0)] bool),

    /// A numeric integer value
    #[n(3)]
    Int(#[n(0)] isize),

    /// A numeric floating-point value
    #[n(4)]
    Float(#[n(0)] f64),

    /// Value is an unordered set
    //Set(HashSet<DataValue>),

    /// The value is an ordered list
    #[n(5)]
    List(#[n(0)] Vec<DataValue>),

    /// The value is a date/timestamp
    #[cbor(n(6))]
    Datetime(
        #[cbor(
            n(0),
            decode_with = "cbor_decode_datetime",
            encode_with = "cbor_encode_datetime"
        )]
        DateTime<FixedOffset>,
    ),

    /// The value is a map
    #[cbor(n(7))]
    Map(#[n(0)] BTreeMap<String, DataValue>),
}

impl DataSize for DataValue {
    // `MyType` contains a `Vec` and a `String`, so `IS_DYNAMIC` is set to true.
    const IS_DYNAMIC: bool = true;
    const STATIC_HEAP_SIZE: usize = 8; //the descriminator/tag of the enum (worst case estimate)

    #[inline]
    fn estimate_heap_size(&self) -> usize {
        match self {
            Self::Null => 8, //discriminator base size only
            Self::Bool(v) => 8 + data_size(v),
            Self::String(v) => 8 + data_size(v),
            Self::Int(v) => 8 + data_size(v),
            Self::Float(v) => 8 + data_size(v),
            Self::List(v) => 8 + data_size(v),
            Self::Map(v) => 8 + data_size(v),
            Self::Datetime(_) => 8 + (4 * 4), //4*u32, guessed based on chrono source code, may not be accurate
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
/// This type defines a test that can be done on a [`DataValue`] (via [`DataValue::test()`]).
/// The operator does not merely consist of the operator-part, but also holds the value that is tested against, which may
/// be one of various types, hence the many variants of this type.
///
/// [`DataOperator::Any`] is a special variant of this operator that will always pass.
pub enum DataOperator<'a> {
    Null,
    Any,
    /// Tests against a string
    Equals(Cow<'a, str>),
    /// Tests against a numeric integer
    EqualsInt(isize),
    /// Tests against a numeric floating-point value
    EqualsFloat(f64),
    True,
    False,
    /// The datavalue must be numeric and greater than the value with the operator
    GreaterThan(isize),
    /// The datavalue must be numeric and greater than or equal to the value with the operator
    GreaterThanOrEqual(isize),
    /// The datavalue must be numeric and greater than the value with the operator
    GreaterThanFloat(f64),
    /// The datavalue must be numeric and greater than or equal to the value with the operator
    GreaterThanOrEqualFloat(f64),
    /// The datavalue must be numeric and less than the value with the operator
    LessThan(isize),
    /// The datavalue must be numeric and less than or equal to the value with the operator
    LessThanOrEqual(isize),
    /// The datavalue must be numeric and less than or equal to the value with the operator
    LessThanFloat(f64),
    /// The datavalue must be numeric and less than or equal to the value with the operator
    LessThanOrEqualFloat(f64),
    /// The datavalue must be a datetime and match this reference exactly
    ExactDatetime(DateTime<FixedOffset>),
    /// The datavalue must be a datetime and come after this reference datetime
    AfterDatetime(DateTime<FixedOffset>),
    /// The datavalue must be a datetime and come before this reference datetime
    BeforeDatetime(DateTime<FixedOffset>),
    /// The datavalue must be a datetime and come at or after this reference datetime
    AtOrAfterDatetime(DateTime<FixedOffset>),
    /// The datavalue must be a datetime and come at or before this reference datetime
    AtOrBeforeDatetime(DateTime<FixedOffset>),

    HasElement(Cow<'a, str>),
    HasElementInt(isize),
    HasElementFloat(f64),

    /// Get an item from a List
    GetIndex(isize, Option<Box<DataOperator<'a>>>),
    /// Get an item from a map
    GetKey(Cow<'a, str>, Option<Box<DataOperator<'a>>>),

    /// Logical negation, reverses the operator
    Not(Box<DataOperator<'a>>),
    /// Logical AND operator (conjunction) to combine multiple operators into one
    And(Vec<DataOperator<'a>>),
    /// Logical OR operator (disjunction) to combine multiple operators into one
    Or(Vec<DataOperator<'a>>),
}

impl<'a> DataValue {
    /// This applies a [`DataOperator`] to the data value, and returns a boolean if the values passes the constraints posed by the operator.
    /// Note that the [`DataOperator`] itself holds the value that is tested against.
    pub fn test(&self, operator: &DataOperator<'a>) -> bool {
        match (self, operator) {
            (_, DataOperator::Any) => true,
            (Self::Null, DataOperator::Null) => true,
            (Self::Bool(true), DataOperator::True) => true,
            (Self::Bool(false), DataOperator::False) => true,
            (Self::Bool(true), DataOperator::Equals(s2)) => match s2.to_lowercase().as_str() {
                "yes" | "1" | "enable" | "enabled" | "on" | "true" => true,
                _ => false,
            },
            (Self::Bool(false), DataOperator::Equals(s2)) => match s2.to_lowercase().as_str() {
                "yes" | "1" | "enable" | "enabled" | "on" | "true" => false,
                _ => true,
            },
            (Self::String(s), DataOperator::Equals(s2)) => &s.as_str() == s2,
            (Self::Int(n), DataOperator::EqualsInt(n2)) => *n == *n2,
            (Self::Int(n), DataOperator::GreaterThan(n2)) => *n > *n2,
            (Self::Int(n), DataOperator::GreaterThanOrEqual(n2)) => *n >= *n2,
            (Self::Int(n), DataOperator::LessThan(n2)) => *n < *n2,
            (Self::Int(n), DataOperator::LessThanOrEqual(n2)) => *n <= *n2,
            (Self::Int(n), DataOperator::Equals(s2)) => {
                if let Ok(n2) = s2.parse::<isize>() {
                    *n == n2
                } else {
                    false
                }
            }
            (Self::Float(n), DataOperator::EqualsFloat(n2)) => *n == *n2,
            (Self::Float(n), DataOperator::GreaterThanFloat(n2)) => *n > *n2,
            (Self::Float(n), DataOperator::GreaterThanOrEqualFloat(n2)) => *n >= *n2,
            (Self::Float(n), DataOperator::LessThanFloat(n2)) => *n < *n2,
            (Self::Float(n), DataOperator::LessThanOrEqualFloat(n2)) => *n <= *n2,
            (Self::Float(n), DataOperator::Equals(s2)) => {
                if let Ok(n2) = s2.parse::<f64>() {
                    *n == n2
                } else {
                    false
                }
            }
            (Self::Datetime(v), DataOperator::ExactDatetime(v2)) => v == v2,
            (Self::Datetime(v), DataOperator::AfterDatetime(v2)) => v > v2,
            (Self::Datetime(v), DataOperator::BeforeDatetime(v2)) => v < v2,
            (Self::Datetime(v), DataOperator::AtOrAfterDatetime(v2)) => v >= v2,
            (Self::Datetime(v), DataOperator::AtOrBeforeDatetime(v2)) => v <= v2,
            (Self::Datetime(v), DataOperator::Equals(s2)) => {
                if let Ok(v2) = DateTime::parse_from_rfc3339(s2) {
                    *v == v2
                } else {
                    false
                }
            }
            (Self::List(v), DataOperator::HasElement(s)) => {
                v.iter().any(|e| e.test(&DataOperator::Equals(s.clone())))
            }
            (Self::List(v), DataOperator::HasElementInt(n)) => {
                v.iter().any(|e| e.test(&DataOperator::EqualsInt(*n)))
            }
            (Self::List(v), DataOperator::HasElementFloat(f)) => {
                v.iter().any(|e| e.test(&DataOperator::EqualsFloat(*f)))
            }
            (Self::Map(v), DataOperator::HasElement(s)) => v.contains_key(s.as_ref()),
            (Self::Map(v), DataOperator::GetKey(s, Some(op))) => {
                v.get(s.as_ref()).map(|e| e.test(&op)).unwrap_or(false)
            }
            (Self::Map(v), DataOperator::GetKey(s, None)) => v.contains_key(s.as_ref()),
            (Self::List(v), DataOperator::GetIndex(i, Some(op))) => v
                .iter()
                .nth(*i as usize)
                .map(|e| e.test(&op))
                .unwrap_or(false),
            (Self::List(v), DataOperator::GetIndex(i, None)) => v.len() > *i as usize,
            (value, DataOperator::Not(operator)) => !value.test(operator),
            (value, DataOperator::And(operators)) => {
                operators.iter().all(|operator| value.test(operator))
            }
            (value, DataOperator::Or(operators)) => {
                operators.iter().any(|operator| value.test(operator))
            }
            _ => false,
        }
    }

    /// Writes a datavalue to one STAM JSON string, with appropriate formatting
    pub fn to_json(&self) -> Result<String, StamError> {
        //note: this function is not invoked during regular serialisation via the store
        serde_json::to_string_pretty(&self).map_err(|e| {
            StamError::SerializationError(format!("Writing datavalue to string: {}", e))
        })
    }

    /// Writes a datavalue to one STAM JSON string, without any indentation
    pub fn to_json_compact(&self) -> Result<String, StamError> {
        //note: this function is not invoked during regular serialisation via the store
        serde_json::to_string(&self).map_err(|e| {
            StamError::SerializationError(format!("Writing datavalue to string: {}", e))
        })
    }
}

impl fmt::Display for DataValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Null => write!(f, "null"),
            Self::String(v) => write!(f, "{}", v),
            Self::Bool(v) => write!(f, "{}", v),
            Self::Int(v) => write!(f, "{}", v),
            Self::Float(v) => write!(f, "{}", v),
            Self::Datetime(v) => write!(f, "{}", v.to_rfc3339()),
            Self::List(v) => {
                for (i, item) in v.iter().enumerate() {
                    if i < v.len() - 1 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", item)?;
                }
                Ok(())
            }
            Self::Map(v) => {
                for (i, (key, item)) in v.iter().enumerate() {
                    if i < v.len() - 1 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}: {}", key, item)?;
                }
                Ok(())
            }
        }
    }
}

impl From<&str> for DataValue {
    fn from(item: &str) -> Self {
        Self::String(item.to_string())
    }
}

impl From<String> for DataValue {
    fn from(item: String) -> Self {
        Self::String(item)
    }
}

impl From<f64> for DataValue {
    fn from(item: f64) -> Self {
        Self::Float(item)
    }
}

impl From<f32> for DataValue {
    fn from(item: f32) -> Self {
        Self::Float(item as f64)
    }
}

impl From<isize> for DataValue {
    fn from(item: isize) -> Self {
        Self::Int(item)
    }
}

impl From<i64> for DataValue {
    fn from(item: i64) -> Self {
        Self::Int(item as isize)
    }
}

impl From<i32> for DataValue {
    fn from(item: i32) -> Self {
        Self::Int(item as isize)
    }
}

impl From<i16> for DataValue {
    fn from(item: i16) -> Self {
        Self::Int(item as isize)
    }
}

impl From<i8> for DataValue {
    fn from(item: i8) -> Self {
        Self::Int(item as isize)
    }
}

impl From<usize> for DataValue {
    fn from(item: usize) -> Self {
        Self::Int(
            item.try_into()
                .expect("integer out of bounds (u64 -> i64 failed)"),
        )
    }
}

impl From<u64> for DataValue {
    fn from(item: u64) -> Self {
        Self::Int(
            item.try_into()
                .expect("integer out of bounds (u64 -> i64 failed)"),
        )
    }
}

impl From<u32> for DataValue {
    fn from(item: u32) -> Self {
        Self::Int(item.try_into().unwrap())
    }
}

impl From<u16> for DataValue {
    fn from(item: u16) -> Self {
        Self::Int(item.try_into().unwrap())
    }
}

impl From<u8> for DataValue {
    fn from(item: u8) -> Self {
        Self::Int(item.try_into().unwrap())
    }
}

impl From<bool> for DataValue {
    fn from(item: bool) -> Self {
        Self::Bool(item)
    }
}

impl From<Vec<DataValue>> for DataValue {
    fn from(item: Vec<DataValue>) -> Self {
        Self::List(item)
    }
}

impl From<BTreeMap<String, DataValue>> for DataValue {
    fn from(item: BTreeMap<String, DataValue>) -> Self {
        Self::Map(item)
    }
}

impl From<Vec<(String, DataValue)>> for DataValue {
    fn from(item: Vec<(String, DataValue)>) -> Self {
        let mut map = BTreeMap::new();
        for (k, v) in item {
            map.insert(k, v);
        }
        Self::Map(map)
    }
}

impl From<DateTime<FixedOffset>> for DataValue {
    fn from(item: DateTime<FixedOffset>) -> Self {
        Self::Datetime(item)
    }
}

// These PartialEq implementation allow for more direct comparisons

impl PartialEq<str> for DataValue {
    fn eq(&self, other: &str) -> bool {
        match self {
            Self::String(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<&str> for DataValue {
    fn eq(&self, other: &&str) -> bool {
        match self {
            Self::String(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for str {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::String(v) => v.as_str() == self,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for &str {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::String(v) => v.as_str() == *self,
            _ => false,
        }
    }
}

impl PartialEq<f64> for DataValue {
    fn eq(&self, other: &f64) -> bool {
        match self {
            Self::Float(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for f64 {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::Float(v) => v == self,
            _ => false,
        }
    }
}

impl PartialEq<isize> for DataValue {
    fn eq(&self, other: &isize) -> bool {
        match self {
            Self::Int(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for isize {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::Int(v) => v == self,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for BTreeMap<String, DataValue> {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::Map(v) => v == self,
            _ => false,
        }
    }
}

impl PartialEq<DataValue> for DateTime<FixedOffset> {
    fn eq(&self, other: &DataValue) -> bool {
        match other {
            DataValue::Datetime(v) => v == self,
            _ => false,
        }
    }
}

impl<'a> TryFrom<DataOperator<'a>> for DataValue {
    type Error = StamError;

    fn try_from(operator: DataOperator<'a>) -> Result<Self, Self::Error> {
        match operator {
            DataOperator::Null => Ok(Self::Null),
            DataOperator::Equals(s) => Ok(Self::String(s.to_string())),
            DataOperator::EqualsFloat(f) => Ok(Self::Float(f)),
            DataOperator::EqualsInt(i) => Ok(Self::Int(i)),
            DataOperator::True => Ok(Self::Bool(true)),
            DataOperator::False => Ok(Self::Bool(false)),
            DataOperator::ExactDatetime(v) => Ok(Self::Datetime(v)),
            _ => Err(StamError::OtherError(
                "Data operator can not be converted to a single DataValue",
            )),
        }
    }
}

impl<'a> From<&'a DataValue> for DataOperator<'a> {
    fn from(v: &'a DataValue) -> Self {
        match v {
            DataValue::Null => DataOperator::Null,
            DataValue::String(s) => DataOperator::Equals(s.as_str().into()),
            DataValue::Int(v) => DataOperator::EqualsInt(*v),
            DataValue::Float(v) => DataOperator::EqualsFloat(*v),
            DataValue::Bool(true) => DataOperator::True,
            DataValue::Bool(false) => DataOperator::False,
            DataValue::Datetime(v) => DataOperator::ExactDatetime(*v),
            DataValue::List(_) => {
                eprintln!("STAM warning: Automatic conversion from list values to operators is not supported!");
                DataOperator::Null
            }
            DataValue::Map(_) => {
                eprintln!("STAM warning: Automatic conversion from map values to operators is not supported!");
                DataOperator::Null
            }
        }
    }
}

impl<'a> From<&'a str> for DataOperator<'a> {
    fn from(s: &'a str) -> Self {
        DataOperator::Equals(s.into())
    }
}

impl<'a> From<isize> for DataOperator<'a> {
    fn from(v: isize) -> Self {
        DataOperator::EqualsInt(v)
    }
}

impl<'a> From<usize> for DataOperator<'a> {
    fn from(v: usize) -> Self {
        DataOperator::EqualsInt(v as isize)
    }
}

impl<'a> From<f64> for DataOperator<'a> {
    fn from(v: f64) -> Self {
        DataOperator::EqualsFloat(v)
    }
}

impl<'a> From<DateTime<FixedOffset>> for DataOperator<'a> {
    fn from(v: DateTime<FixedOffset>) -> Self {
        DataOperator::ExactDatetime(v)
    }
}

impl<'a> From<bool> for DataOperator<'a> {
    fn from(v: bool) -> Self {
        if v {
            DataOperator::True
        } else {
            DataOperator::False
        }
    }
}

impl<'a> DataOperator<'a> {
    /// Turns the DataOperator to a string, compatible with STAMQL
    pub fn to_string(&self) -> Result<String, StamError> {
        match self {
            DataOperator::Any => Ok(format!("= any")),
            DataOperator::Null => Ok(format!("= null")),
            DataOperator::True => Ok(format!("= true")),
            DataOperator::False => Ok(format!("= false")),
            DataOperator::Equals(s) => Ok(format!("= \"{}\"", s)),
            DataOperator::Not(expr) => match expr.deref() {
                DataOperator::Equals(..)
                | DataOperator::EqualsInt(..)
                | DataOperator::EqualsFloat(..)
                | DataOperator::Any
                | DataOperator::Null
                | DataOperator::True
                | DataOperator::False => Ok(format!("!{}", expr.to_string()?)),
                _ => Err(StamError::QuerySyntaxError(
                    format!(
                        "There is no query syntax yet for this dataoperator expression: {:?}",
                        self
                    ),
                    "DataOperator::to_string()",
                )),
            },
            DataOperator::EqualsInt(n) => Ok(format!("= {}", n)),
            DataOperator::EqualsFloat(n) => Ok(format!("= {}", n)),
            DataOperator::GreaterThan(n) => Ok(format!("> {}", n)),
            DataOperator::GreaterThanOrEqual(n) => Ok(format!(">= {}", n)),
            DataOperator::LessThan(n) => Ok(format!("< {}", n)),
            DataOperator::LessThanOrEqual(n) => Ok(format!("<= {}", n)),
            DataOperator::GreaterThanFloat(n) => Ok(format!("> {}", n)),
            DataOperator::GreaterThanOrEqualFloat(n) => Ok(format!(">= {}", n)),
            DataOperator::LessThanOrEqualFloat(n) => Ok(format!("<= {}", n)),
            DataOperator::LessThanFloat(n) => Ok(format!("< {}", n)),
            DataOperator::ExactDatetime(d) => Ok(format!("= {}", d.to_rfc3339())),
            DataOperator::AfterDatetime(d) => Ok(format!("> {}", d.to_rfc3339())),
            DataOperator::AtOrAfterDatetime(d) => Ok(format!(">= {}", d.to_rfc3339())),
            DataOperator::BeforeDatetime(d) => Ok(format!("< {}", d.to_rfc3339())),
            DataOperator::AtOrBeforeDatetime(d) => Ok(format!("<= {}", d.to_rfc3339())),
            DataOperator::HasElement(s) => Ok(format!("HAS {}", s)),
            DataOperator::HasElementInt(n) => Ok(format!("HAS {}", n)),
            DataOperator::HasElementFloat(n) => Ok(format!("HAS {}", n)),
            DataOperator::GetKey(s, None) => Ok(format!(". {}", s)),
            DataOperator::GetIndex(n, None) => Ok(format!(". {}", n)),
            DataOperator::GetKey(s, Some(op)) => Ok(format!(". {} {}", s, &op.to_string()?)),
            DataOperator::GetIndex(n, Some(op)) => Ok(format!(". {} {}", n, &op.to_string()?)),
            _ => {
                //And, Or //TODO: implement
                Err(StamError::QuerySyntaxError(
                    format!(
                        "There is no query syntax yet for this dataoperator expression: {:?}",
                        self
                    ),
                    "DataOperator::to_string()",
                ))
            }
        }
    }
}