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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
use serde::ser::{SerializeSeq, SerializeStruct, Serializer};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::borrow::Cow;
use datasize::{DataSize,data_size};
use minicbor::{Encode,Decode};
use smallvec::SmallVec;

use crate::annotation::{Annotation, AnnotationHandle};
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::annotationstore::AnnotationStore;
use crate::error::*;
use crate::annotationdata::{AnnotationData,AnnotationDataHandle};
use crate::datakey::{DataKey,DataKeyHandle};
use crate::resources::{TextResource, TextResourceHandle};
use crate::textselection::{TextSelection, TextSelectionHandle};
use crate::types::*;
use crate::store::*;
use crate::text::Text;

/// Text selection offset. Specifies begin and end offsets to select a range of a text, via two [`Cursor`] instances.
/// The end-point is non-inclusive.
#[derive(Debug, Clone, Deserialize, PartialEq, DataSize, Encode, Decode)]
pub struct Offset {
    #[n(0)]  //these macros are field index numbers for cbor binary (de)serialisation
    pub begin: Cursor,

    #[n(1)] 
    pub end: Cursor,
}

impl Offset {
    pub fn new(begin: Cursor, end: Cursor) -> Self {
        Offset { begin, end }
    }

    /// Shortcut constructor to create a simple begin-aligned offset (less boilerplate)
    pub fn simple(begin: usize, end: usize) -> Self {
        Offset {
            begin: Cursor::BeginAligned(begin),
            end: Cursor::BeginAligned(end),
        }
    }

    /// Shortcut constructor to create a constructor that selects everything of the target
    pub fn whole() -> Self {
        Offset {
            begin: Cursor::BeginAligned(0),
            end: Cursor::EndAligned(0),
        }
    }

    /// Returns true if this Offset only uses begin-aligned cursors 
    pub fn is_simple(&self) -> bool {
        match (self.begin, self.end) {
            (Cursor::BeginAligned(_), Cursor::BeginAligned(_)) => true,
            _ => false
        }
    }

    /// Returns true if this Offset only uses begin-aligned cursors, or if it selects the whole target
    pub fn is_simple_or_whole(&self) -> bool {
        match (self.begin, self.end) {
            (Cursor::BeginAligned(_), Cursor::BeginAligned(_)) => true,
            (Cursor::BeginAligned(0), Cursor::EndAligned(0)) => true,
            _ => 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 called during normal serialisation
        serde_json::to_string_pretty(&self).map_err(|e| {
            StamError::SerializationError(format!("Writing textselection 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 called during normal serialisation
        serde_json::to_string(&self).map_err(|e| {
            StamError::SerializationError(format!("Writing textselection to string: {}", e))
        })
    }

    pub fn mode(&self) -> OffsetMode {
        self.into()
    }

    /// Create a new offset by moving this one right (positive distance) or left (negative distance)
    /// This will never produce negative offsets! If the cursor type doesn't support shifting (that far) in a direction then an error will be raised.
    pub fn shift(&self, distance: isize) -> Result<Offset,StamError> {
        Ok(Offset {
            begin: self.begin.shift(distance)?,
            end: self.end.shift(distance)?,
        })
    }

    /// Returns the length of the offset.
    /// If the underlying cursor types are not of the same type, the length
    /// is undefined and None is returned.
    pub fn len(&self) -> Option<usize> {
        match (self.begin, self.end) {
            (Cursor::BeginAligned(begin), Cursor::BeginAligned(end)) => Some(end - begin),
            (Cursor::EndAligned(begin), Cursor::EndAligned(end)) => Some((end - begin).abs() as usize),
            _ => None
        }
    }
}

impl Default for Offset {
    /// The default constructor selects the text as a whole
    fn default() -> Self {
        Offset {
            begin: Cursor::BeginAligned(0),
            end: Cursor::EndAligned(0),
        }
    }
}

impl Serialize for Offset {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("AnnotationData", 2)?;
        state.serialize_field("@type", "Offset")?;
        state.serialize_field("begin", &self.begin)?;
        state.serialize_field("end", &self.end)?;
        state.end()
    }
}

/// The offset mode represents the ways in which the user can specify an [`Offset`], it expresses
/// whether the cursors ([`Cursor`]) for the begin and end positions of the offset are specified as begin-aligned or end-aligned.
#[derive(Debug, Clone, Copy, PartialEq, Encode, Decode,DataSize)]
pub enum OffsetMode {
    /// Offset where both the begin and end positions are specified using a begin-aligned cursor ([`Cursor::BeginAligned`])
    /// This is by-far the most common offset mode.
    #[n(0)]
    BeginBegin,

    /// Offset where the begin position is specified using a begin-aligned cursor ([`Cursor::BeginAligned`])
    /// and the end position using an end-aligned cursor ([`Cursor::EndAligned`])
    #[n(1)]
    BeginEnd,

    /// Offset where both the begin and end positions are specified using an end-aligned cursor ([`Cursor::EndAligned`])
    #[n(2)]
    EndEnd,

    /// Offset where the end position is specified using a begin-aligned cursor ([`Cursor::BeginAligned`])
    /// and the begin position using an end-aligned cursor ([`Cursor::EndAligned`])
    #[n(3)]
    EndBegin,
}

impl Default for OffsetMode {
    fn default() -> Self {
        Self::BeginBegin
    }
}

impl From<&Offset> for OffsetMode {
    fn from(offset: &Offset) -> OffsetMode {
        match offset.begin {
            Cursor::BeginAligned(_) => {
                match offset.end {
                    Cursor::BeginAligned(_) => {
                        Self::BeginBegin
                    },
                    Cursor::EndAligned(_) => {
                        Self::BeginEnd
                    }
                }
            },
            Cursor::EndAligned(_) => {
                match offset.end {
                    Cursor::BeginAligned(_) => {
                        Self::EndBegin
                    },
                    Cursor::EndAligned(_) => {
                        Self::EndEnd
                    }
                }
            }
        }
    }
}
impl From<(isize,isize)> for OffsetMode {
    fn from(value: (isize,isize)) -> Self {
        if value.0 >= 0 && value.1 > 0 {
            Self::BeginBegin
        }  else if value.0 >= 0 && value.1 <= 0 {
            Self::BeginEnd
        }  else if value.0 < 0 && value.1 <= 0 {
            Self::EndEnd
        } else {
            Self::EndBegin
        }
    }

}

impl From<(isize,isize)> for Offset {
    fn from(value: (isize,isize)) -> Self {
        if value.0 >= 0 && value.1 > 0 {
            Offset::new(Cursor::BeginAligned(value.0 as usize), Cursor::BeginAligned(value.1 as usize))
        }  else if value.0 >= 0 && value.1 <= 0 {
            Offset::new(Cursor::BeginAligned(value.0 as usize), Cursor::EndAligned(value.1))
        }  else if value.0 < 0 && value.1 <= 0 {
            Offset::new(Cursor::EndAligned(value.0), Cursor::EndAligned(value.1))
        } else {
            Offset::new(Cursor::EndAligned(value.0), Cursor::BeginAligned(value.1 as usize))
        }
    }
}

impl TryFrom<&str> for Offset {
    type Error = StamError;
    fn try_from(offset: &str) -> Result<Self, Self::Error> {
        let fields: SmallVec<[&str; 2]> = offset.split(|c| [':',','].contains(&c)).collect();
        let begin: isize = if fields.len() >= 1 && fields.get(0) != Some(&"") {
            fields.get(0).unwrap().parse().map_err(|_| {
                StamError::ValueError(offset.into(), "offset string must be in 'start:end' or 'start,end' form, with start and end being a (possibly signed) integer")
            })?
        } else {
            0
        };
        let end: isize = if fields.len() == 2 && fields.get(1) != Some(&"") {
            fields.get(1).unwrap().parse().map_err(|_| {
                StamError::ValueError(offset.into(), "offset string must be in 'start:end' or 'start,end' form, with start and end being a (possibly signed) integer")
            })?
        } else {
            0
        };
        Ok((begin,end).into())
    }
}

/// A `Selector` identifies the target of an annotation and the part of the
/// target that the annotation applies to. Selectors can be considered the labelled edges of the graph model, tying all nodes together.
/// There are multiple types of selectors, all captured in this enum.
///
/// You usually do not instantiate these directly but via [`SelectorBuilder`].
/// In searching, you also don't need direct access to this structure as
/// the various search methods on AnnotationStore will resolve the selectors
/// transparently.
#[derive(Debug, Clone, PartialEq, Encode, Decode)]
pub enum Selector {
    /// Refers to the [`TextResource`] and a [`TextSelection`] within
    #[n(0)] //these macros are field index numbers for cbor binary (de)serialisation
    TextSelector(
        #[n(0)]
        TextResourceHandle,
        #[n(1)]
        TextSelectionHandle,
        #[n(2)]
        OffsetMode, //this allows us to know what kind of offset to compute on serialisation
    ),

    /// Refers to an [`Annotation`] (as owned by the AnnotationStore) and optionally a *relative* text selection offset in it
    #[n(1)] //these macros are field index numbers for cbor binary (de)serialisation
    AnnotationSelector(
        #[n(0)]
        AnnotationHandle, 
        #[n(1)]
        Option<(TextResourceHandle, TextSelectionHandle, OffsetMode)>
    ),

    /// Refers to a [`TextResource`] as a whole (as opposed to a text fragment inside it), as owned by an AnnotationStore.
    /// Annotations using this selector can be considered metadata of a text
    #[n(2)]
    ResourceSelector(
        #[n(0)]
        TextResourceHandle
    ),

    /// Refers to an [`crate::AnnotationDataSet`] as owned by an [`AnnotationStore`]
    /// Annotations using this selector can be considered metadata.
    #[n(3)]
    DataSetSelector(
        #[n(0)]
        AnnotationDataSetHandle
    ),

    /// A selector that combines selectors, where the annotation applies to each target
    /// individually.  without any relation between the different targets. Leaving one out or
    /// adding one MUST NOT affect the interpretation of any of the others nor of the whole. This
    /// is a way to express multiple annotations as one, a more condensed representation. This
    /// selector SHOULD be used sparingly in your modelling, as it is generally RECOMMENDED to
    /// simply use multiple [`Annotation`] instances instead. In STAM, even with multiple annotations, you
    /// benefit from the fact that multiple annotations may share the same [`crate::AnnotationData`], and can
    /// therefore easily retrieve all annotations that share particular data.
    #[n(4)]
    MultiSelector(
        #[n(0)]
        Vec<Selector>
    ),

    /// A selector that consists of multiple other selectors, used to select more complex targets
    /// that transcend the idea of a single simple selection. This MUST be interpreted as the
    /// annotation applying equally to the conjunction as a whole, its parts being inter-dependent
    /// and for any of them it goes that they MUST NOT be omitted for the annotation to make sense.
    /// The interpretation of the whole relies on all its parts. Note that the order of the
    /// selectors is not significant (use a [`Self::DirectionalSelector`] instead if they are). When there is
    /// no dependency relation between the selectors, you MUST simply use multiple [`Annotation`] instances or a
    /// [`Self::MultiSelector`] instead. When grouping things into a set, do use this [`Self::CompositeSelector`], as the
    /// set as a whole is considered a composite entity.
    #[n(5)]
    CompositeSelector(
        #[n(0)]
        Vec<Selector>
    ),

    /// Combines selectors and expresseds a direction between two or more selectors in the exact order specified (from -> to)
    #[n(6)]
    DirectionalSelector(
        #[n(0)]
        Vec<Selector>
    ),


    /// Refers to a [`DataKey`] to annotate.
    /// Annotations using this selector are considered metadata of a key
    #[n[7]]
    DataKeySelector(
        #[n(0)]
        AnnotationDataSetHandle,
        #[n(1)]
        DataKeyHandle
    ),

    /// Refers to a [`AnnotationData`] to annotate.
    /// Annotations using this selector can be considered metadata on specific data, it is fairly rare.
    #[n[8]]
    AnnotationDataSelector(
        #[n(0)]
        AnnotationDataSetHandle,
        #[n(1)]
        AnnotationDataHandle
    ),


    /// Internal ranged selector, used as subselector for MultiSelector/CompositeSelector/DirectionalSelector
    // Conserves memory by pointing to a internal ID range, end is inclusive
    #[n(52)]
    RangedTextSelector {
        #[n(0)]
        resource: TextResourceHandle,
        #[n(1)]
        begin: TextSelectionHandle,
        #[n(2)]
        end: TextSelectionHandle,
    },

    /// Internal ranged selector, used as subselector for MultiSelector/CompositeSelector/DirectionalSelector
    /// Conserves memory by pointing to a internal ID range, end is inclusive
    #[n(53)]
    RangedAnnotationSelector {
        #[n(0)]
        begin: AnnotationHandle,
        #[n(1)]
        end: AnnotationHandle,
        #[n(2)]
        with_text: bool, //if set, each individual AnnotationSelector will get a Offset::whole(), otherwise it gets no text reference
    },
}

impl Selector {
    /// Returns a [`SelectorKind`] identifying the type of selector
    pub fn kind(&self) -> SelectorKind {
        self.into()
    }

    /// A complex selector targets multiple targets. Note the internal ranged selector is not counted as part of this category.
    pub fn is_complex(&self) -> bool {
        self.kind().is_complex()
    }

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

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

    /// Returns all subselectors. Use [`self.iter()`] instead if you want an iterator
    /// with more functionality.
    pub fn subselectors(&self) -> Option<&[Selector]> {
        match self {
            Selector::MultiSelector(v) | Selector::CompositeSelector(v) | Selector::DirectionalSelector(v) => Some(v),
            _ => None,
        }
    }   

    /// Returns the number of subselectors under this selector (by definition 0 for simple selectors)
    pub fn len(&self) -> usize {
        match self {
            Selector::MultiSelector(v) | Selector::CompositeSelector(v) | Selector::DirectionalSelector(v) => v.len(),
            _ => 0,
        }
    }

    /// Returns the textselection this selector points at, if any
    pub fn textselection<'store>(&self, store: &'store AnnotationStore) -> Option<&'store TextSelection> {
        match self {
            Selector::TextSelector(res_handle,tsel_handle,_) | Selector::AnnotationSelector(_, Some((res_handle,tsel_handle,_))) => {
                let resource: &TextResource = store.get(*res_handle).expect("handle must be valid");
                let textselection: &TextSelection = resource.get(*tsel_handle).expect("handle must be valid");
                Some(textselection)
            }
            _ => None
        }
    }

    /// Returns the textselection handle this selector points at, if any
    pub fn textselection_handle(&self) -> Option<TextSelectionHandle> {
        match self {
            Selector::TextSelector(_,tsel_handle,_) | Selector::AnnotationSelector(_, Some((_,tsel_handle,_))) => {
                Some(*tsel_handle)
            }
            _ => None
        }
    }

    /// Returns the handle of the resource this selector points at, if any
    pub fn resource_handle(&self) -> Option<TextResourceHandle> {
        match self {
            Selector::ResourceSelector(res_handle) | Selector::TextSelector(res_handle,_,_) | Selector::AnnotationSelector(_, Some((res_handle,_,_))) => {
                Some(*res_handle)
            }
            _ => None
        }
    }


    /// Returns the associated offset if the selector carries one
    pub fn offset(&self, store: &AnnotationStore) -> Option<Offset> {
        self.offset_with_mode(store, None)
}

    /// Returns the associated offset if the selector carries one
    /// You may set `override_mode` if you want to override the OffsetMode rather than take it from the selector
    pub fn offset_with_mode(&self, store: &AnnotationStore, override_mode: Option<OffsetMode>) -> Option<Offset> {
        match self {
            Selector::TextSelector(res_handle,tsel_handle,offsetmode) => {
                let offsetmode = if let Some(override_mode) = override_mode {
                    override_mode 
                } else {
                    *offsetmode
                };
                let resource: &TextResource = store.get(*res_handle).expect("handle must be valid");
                let textselection: &TextSelection = resource.get(*tsel_handle).expect("handle must be valid");
                match offsetmode {
                    OffsetMode::BeginBegin => //this is the easy one, the From<TextSelection> trait provides it:
                        Some(textselection.into()),
                    OffsetMode::BeginEnd => {
                        let begin = textselection.begin();
                        let end: isize = textselection.end() as isize - resource.textlen() as isize;
                        Some(Offset::new(Cursor::BeginAligned(begin), Cursor::EndAligned(end)))
                    }
                    OffsetMode::EndBegin => { 
                        let begin: isize = textselection.begin() as isize - resource.textlen() as isize;
                        let end = textselection.end();
                        Some(Offset::new(Cursor::EndAligned(begin), Cursor::BeginAligned(end)))
                    }
                    OffsetMode::EndEnd => {
                        let begin: isize = textselection.begin() as isize - resource.textlen() as isize;
                        let end: isize = textselection.end() as isize - resource.textlen() as isize;
                        Some(Offset::new(Cursor::EndAligned(begin), Cursor::EndAligned(end)))
                    }
                }
            }
            Selector::AnnotationSelector(annotation_handle, Some((res_handle, tsel_handle, offsetmode))) => {
                let offsetmode = if let Some(override_mode) = override_mode {
                    override_mode 
                } else {
                    *offsetmode
                };
                let resource: &TextResource = store.get(*res_handle).expect("handle must be valid");
                let textselection: &TextSelection = resource.get(*tsel_handle).expect("handle must be valid");
                let target: &Annotation = store.get(*annotation_handle).expect("handle must be valid");
                if let Some(parent_textselection) = target.target().textselection(store) {
                    textselection.relative_offset(parent_textselection, offsetmode)
                } else {
                    None
                }
            }
            _ => None
        }
    }
}

impl DataSize for Selector {
    // `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::TextSelector(handle, handle2 ,mode) => 8 + data_size(handle) + data_size(handle2) + data_size(mode),
            Self::AnnotationSelector(handle, handle2) => 8 + data_size(handle) + data_size(handle2),
            Self::ResourceSelector(handle) => 8 + data_size(handle),
            Self::DataSetSelector(handle) => 8 + data_size(handle),
            Self::DataKeySelector(handle,handle2) => 8 + data_size(handle)+ data_size(handle2),
            Self::AnnotationDataSelector(handle,handle2) => 8 + data_size(handle)+ data_size(handle2),
            Self::MultiSelector(v) => 8 + data_size(v),
            Self::CompositeSelector(v) => 8 + data_size(v),
            Self::DirectionalSelector(v) => 8 + data_size(v),
            Self::RangedTextSelector { resource, begin, end } => 8 + data_size(resource) + data_size(begin) + data_size(end),
            Self::RangedAnnotationSelector { begin, end, with_text } => 8 + data_size(with_text) + data_size(begin) + data_size(end),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize,Deserialize)]
/// See [`Selector`], this is a simplified variant that carries only the type, not the target.
pub enum SelectorKind {
    ResourceSelector = 1,
    AnnotationSelector = 2,
    TextSelector = 3,
    DataSetSelector = 4,
    MultiSelector = 5,
    CompositeSelector = 6,
    DirectionalSelector = 7,
    InternalRangedSelector = 8,
    DataKeySelector = 9,
    AnnotationDataSelector = 10,
}

impl SelectorKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::ResourceSelector => "ResourceSelector",
            Self::AnnotationSelector => "AnnotationSelector",
            Self::TextSelector => "TextSelector",
            Self::DataSetSelector => "DataSetSelector",
            Self::MultiSelector => "MultiSelector",
            Self::CompositeSelector => "CompositeSelector",
            Self::DirectionalSelector => "DirectionalSelector",
            Self::InternalRangedSelector => "InternalRangedSelector",
            Self::DataKeySelector => "DataKeySelector",
            Self::AnnotationDataSelector => "AnnotationDataSelector",
        }
    }

    pub fn is_complex(&self) -> bool {
        match self {
            Self::MultiSelector => true,
            Self::DirectionalSelector => true,
            Self::CompositeSelector => true,
            _ => false,
        }
    }

}

impl TryFrom<&str> for SelectorKind {
    type Error = StamError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "ResourceSelector" | "resourceselector" | "resource" => Ok(Self::ResourceSelector),
            "AnnotationSelector" | "annotationselector" | "annotation"  => Ok(Self::AnnotationSelector),
            "TextSelector" | "textselector" | "text" => Ok(Self::TextSelector),
            "DataSetSelector" | "datasetselector" | "set" | "annotationset" | "dataset" => Ok(Self::DataSetSelector),
            "DataKeySelector" | "datakeyselector" | "key" => Ok(Self::DataKeySelector),
            "AnnotationDataSelector" | "annotationdataselector" | "dataselector" | "data" => Ok(Self::AnnotationDataSelector),
            "MultiSelector" | "multiselector" | "multi"  => Ok(Self::MultiSelector),
            "CompositeSelector" | "compositeselector" | "composite"  => Ok(Self::CompositeSelector),
            "DirectionalSelector" | "directionalselector" | "directional"  => Ok(Self::DirectionalSelector),
            _ => Err(StamError::ValueError(value.to_string(), "Expected a valid SelectorKind"))
        }
    }

}

impl From<&Selector> for SelectorKind {
    fn from(selector: &Selector) -> Self {
        match selector {
            Selector::ResourceSelector(..) => Self::ResourceSelector,
            Selector::AnnotationSelector(..) => Self::AnnotationSelector,
            Selector::TextSelector(..) => Self::TextSelector,
            Selector::DataSetSelector(_) => Self::DataSetSelector,
            Selector::DataKeySelector(..) => Self::DataKeySelector,
            Selector::AnnotationDataSelector(..) => Self::AnnotationDataSelector,
            Selector::MultiSelector(..) => Self::MultiSelector,
            Selector::CompositeSelector(..) => Self::CompositeSelector,
            Selector::DirectionalSelector(..) => Self::DirectionalSelector,
            Selector::RangedTextSelector {
                ..
            }
            | Selector::RangedAnnotationSelector { .. } => {
                Self::InternalRangedSelector
            }
        }
    }
}

impl<'a> From<&SelectorBuilder<'a>> for SelectorKind {
    fn from(selector: &SelectorBuilder<'a>) -> Self {
        match selector {
            SelectorBuilder::ResourceSelector(..) => Self::ResourceSelector,
            SelectorBuilder::AnnotationSelector(..) => Self::AnnotationSelector,
            SelectorBuilder::TextSelector(..) => Self::TextSelector,
            SelectorBuilder::DataSetSelector(..) => Self::DataSetSelector,
            SelectorBuilder::DataKeySelector(..) => Self::DataKeySelector,
            SelectorBuilder::AnnotationDataSelector(..) => Self::AnnotationDataSelector,
            SelectorBuilder::MultiSelector(..) => Self::MultiSelector,
            SelectorBuilder::CompositeSelector(..) => Self::CompositeSelector,
            SelectorBuilder::DirectionalSelector(..) => Self::DirectionalSelector,
        }
    }
}

/// A `SelectorBuilder` is a recipe that, when applied, identifies the target of an annotation and the part of the
/// target that the annotation applies to. They produce a `Selector`. You turn a `SelectorBuilder` into a [`Selector`] using [`AnnotationStore::selector`](crate::AnnotationStore::selector).
///
/// A `SelectorBuilder` can refer to anything and is not validated yet, a `Selector` is and should not fail.
///
/// There are multiple types of selectors, all captured in this enum.
#[derive(Debug, Deserialize)]
#[serde(from = "SelectorJson")]
pub enum SelectorBuilder<'a> {
    ResourceSelector(BuildItem<'a,TextResource>),
    AnnotationSelector(BuildItem<'a,Annotation>, Option<Offset>),
    TextSelector(BuildItem<'a,TextResource>, Offset),
    DataSetSelector(BuildItem<'a,AnnotationDataSet>),
    DataKeySelector(BuildItem<'a, AnnotationDataSet>, BuildItem<'a,DataKey>),
    AnnotationDataSelector(BuildItem<'a, AnnotationDataSet>, BuildItem<'a,AnnotationData>),
    MultiSelector(Vec<SelectorBuilder<'a>>),
    CompositeSelector(Vec<SelectorBuilder<'a>>),
    DirectionalSelector(Vec<SelectorBuilder<'a>>),
}

impl<'a> SelectorBuilder<'a> {
    /// Returns a [`SelectorKind`]
    pub fn kind(&self) -> SelectorKind {
        self.into()
    }

    /// A complex selector targets multiple targets. Note the internal ranged selector is not counted as part of this category.
    pub fn is_complex(&self) -> bool {
        self.kind().is_complex()
    }

    /// Creates a new ResourceSelector
    pub fn resourceselector(resource: impl Into<BuildItem<'a,TextResource>>) -> Self {
        Self::ResourceSelector(resource.into())
    }

    /// Creates a new TextSelector
    pub fn textselector(resource: impl Into<BuildItem<'a,TextResource>>, offset: impl Into<Offset>) -> Self {
        Self::TextSelector(resource.into(), offset.into())
    }

    /// Creates a new AnnotationSelector
    pub fn annotationselector(annotation: impl Into<BuildItem<'a,Annotation>>, offset: Option<Offset>) -> Self {
        Self::AnnotationSelector(annotation.into(), offset)
    }

    /// Creates a new DataSetSelector
    pub fn datasetselector(dataset: impl Into<BuildItem<'a,AnnotationDataSet>>) -> Self {
        Self::DataSetSelector(dataset.into())
    }

    /// Creates a new DataKeySelector
    pub fn datakeyselector(dataset: impl Into<BuildItem<'a,AnnotationDataSet>>, datakey: impl Into<BuildItem<'a,DataKey>>) -> Self {
        Self::DataKeySelector(dataset.into(), datakey.into())
    }

    /// Creates a new AnnotationDataSelector
    pub fn annotationdataselector(dataset: impl Into<BuildItem<'a,AnnotationDataSet>>,annotationdata: impl Into<BuildItem<'a,AnnotationData>>) -> Self {
        Self::AnnotationDataSelector(dataset.into(), annotationdata.into())
    }

    /// Creates a new MultiSelector from an iterator
    pub fn multiselector<I>(iter: I) -> Self where I: IntoIterator<Item = SelectorBuilder<'a>>, {
        Self::MultiSelector(iter.into_iter().collect())
    }

    /// Creates a new CompositeSelector from an iterator
    pub fn compositeselector<I>(iter: I) -> Self where I: IntoIterator<Item = SelectorBuilder<'a>>  {
        Self::CompositeSelector(iter.into_iter().collect())
    }

    /// Creates a new DirectionalSelector from an iterator
    pub fn directionalselector<I>(iter: I) -> Self where I: IntoIterator<Item = SelectorBuilder<'a>> {
        Self::DirectionalSelector(iter.into_iter().collect())
    }

    /// Returns the offset associated with the selector (if any)
    pub fn offset(&self) -> Option<&Offset> {
        if let Self::AnnotationSelector(_, Some(offset)) | Self::TextSelector(_, offset) = self {
            Some(offset)
        } else {
            None
        }
    }

    /// Returns the resource associated with the selector (if any)
    pub fn resource(&self) -> Option<&BuildItem<'a, TextResource>> {
        if let Self::ResourceSelector(resource) | Self::TextSelector(resource, _) = self {
            Some(resource)
        } else {
            None
        }
    }
}




/// Helper structure for Json deserialisation, we need named fields for the serde tag macro to work
#[derive(Debug, Deserialize)]
#[serde(tag = "@type")]
enum SelectorJson where
    {
    ResourceSelector {
        resource: String,
    },
    AnnotationSelector {
        annotation: String,
        offset: Option<Offset>,
    },
    TextSelector {
        resource: String,
        offset: Offset,
    },
    DataSetSelector {
        annotationset: String,
    },
    DataKeySelector {
        annotationset: String,
        key: String,
    },
    AnnotationDataSelector {
        annotationset: String,
        data: String,
    },
    MultiSelector { selectors: Vec<SelectorJson> },
    CompositeSelector { selectors: Vec<SelectorJson>},
    DirectionalSelector{ selectors: Vec<SelectorJson>},
}

impl<'a> From<SelectorJson> for SelectorBuilder<'a> {
    fn from(helper: SelectorJson) -> Self {
        match helper {
            SelectorJson::ResourceSelector { resource: res } => Self::ResourceSelector(res.into()),
            SelectorJson::TextSelector {
                resource: res,
                offset: o,
            } => Self::TextSelector(res.into(), o),
            SelectorJson::AnnotationSelector {
                annotation: a,
                offset: o,
            } => Self::AnnotationSelector(a.into(), o),
            SelectorJson::DataSetSelector { annotationset: s } => Self::DataSetSelector(s.into()),
            SelectorJson::DataKeySelector { annotationset: s, key: k } => Self::DataKeySelector(s.into(), k.into()),
            SelectorJson::AnnotationDataSelector { annotationset: s, data: d } => Self::AnnotationDataSelector(s.into(), d.into()),
            SelectorJson::MultiSelector { selectors: v } => Self::MultiSelector(v.into_iter().map(|j| j.into()).collect()),
            SelectorJson::CompositeSelector { selectors: v } => Self::CompositeSelector(v.into_iter().map(|j| j.into()).collect()),
            SelectorJson::DirectionalSelector { selectors: v } => Self::DirectionalSelector(v.into_iter().map(|j| j.into()).collect()),
        }
    }
}


/// This trait is implemented by types that can return a Selector to themselves
pub trait SelfSelector {
    /// Returns a selector that points to this resource
    fn to_selector(&self) -> Result<Selector, StamError>;
}


/// This is a smart pointer that encapsulates both a selector and the annotationstore in which it can be resolved.
/// We need the wrapped structure for serialization.
pub struct WrappedSelector<'a> {
    selector: &'a Selector,
    store: &'a AnnotationStore,
}

impl<'a> Deref for WrappedSelector<'a> {
    type Target = Selector;

    fn deref(&self) -> &Self::Target {
        self.selector
    }
}

impl<'a> WrappedSelector<'a> {
    pub(crate) fn new(selector: &'a Selector, store: &'a AnnotationStore) -> Self {
        WrappedSelector { selector, store }
    }

    pub(crate) fn store(&'a self) -> &'a AnnotationStore {
        self.store
    }
}

/// This structure is used for serializing subselectors
pub struct WrappedSelectors<'a> {
    selectors: &'a Vec<Selector>,
    store: &'a AnnotationStore,
}

impl<'a> Serialize for WrappedSelectors<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.selectors.len()))?;
        for subselector in self.selectors.iter() {
            if subselector.kind() != SelectorKind::InternalRangedSelector {
                //normal case
                let wrappedselector = WrappedSelector {
                    selector: subselector,
                    store: self.store,
                };
                seq.serialize_element(&wrappedselector)?;
            } else {
                //we have an internal ranged selector
                for subselector in subselector.iter(self.store, false) {
                    let wrappedselector = WrappedSelector {
                        selector: &subselector,
                        store: self.store,
                    };
                    seq.serialize_element(&wrappedselector)?;
                }
            }
        }
        seq.end()
    }
}

impl<'a> Serialize for WrappedSelector<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self.selector {
            Selector::ResourceSelector(res_handle) => {
                let textresource: Result<&TextResource, _> = self.store().get(*res_handle);
                let textresource = textresource.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 2)?;
                state.serialize_field("@type", "ResourceSelector")?;
                if let Some(id) = textresource.id() {
                    state.serialize_field("resource", &id)?;
                } else {
                    state.serialize_field("resource", &textresource.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                state.end()
            }
            Selector::TextSelector(res_handle, _, _) => {
                let textresource: Result<&TextResource, _> = self.store().get(*res_handle);
                let textresource = textresource.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 3)?;
                state.serialize_field("@type", "TextSelector")?;
                if let Some(id) = textresource.id() {
                    state.serialize_field("resource", &id)?;
                } else {
                    state.serialize_field("resource", &textresource.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                let offset: Offset = self.selector.offset(self.store()).expect("must have offset");
                state.serialize_field("offset", &offset)?;
                state.end()
            }
            Selector::DataSetSelector(dataset_handle) => {
                let dataset: Result<&AnnotationDataSet, _> =
                    self.store().get(*dataset_handle);
                let dataset = dataset.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 2)?;
                state.serialize_field("@type", "DataSetSelector")?;
                if let Some(id) = dataset.id() {
                    state.serialize_field("annotationset", &id)?;
                } else {
                    state.serialize_field("annotationset", &dataset.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                state.end()
            }
            Selector::AnnotationSelector(annotation_handle, _) => {
                let annotation: Result<&Annotation, _> = self.store().get(*annotation_handle);
                let annotation = annotation.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 3)?;
                state.serialize_field("@type", "AnnotationSelector")?;
                if let Some(id) = annotation.id() {
                    state.serialize_field("annotation", &id)?;
                } else {
                    state.serialize_field("annotation", &annotation.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                if let Some(offset) = self.selector.offset(self.store()) {
                    state.serialize_field("offset", &offset)?;
                }
                state.end()
            }
            Selector::DataKeySelector(dataset_handle, key_handle) => {
                let dataset: Result<&AnnotationDataSet, _> =
                    self.store().get(*dataset_handle);
                let dataset = dataset.map_err(serde::ser::Error::custom)?;
                let key: Result<&DataKey, _> =
                    dataset.get(*key_handle);
                let key = key.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 3)?;
                state.serialize_field("@type", "DataKeySelector")?;
                if let Some(id) = dataset.id() {
                    state.serialize_field("annotationset", &id)?;
                } else {
                    state.serialize_field("annotationset", &dataset.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                if let Some(id) = key.id() {
                    state.serialize_field("key", &id)?;
                } else {
                    state.serialize_field("key", &dataset.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                state.end()
            }
            Selector::AnnotationDataSelector(dataset_handle, data_handle) => {
                let dataset: Result<&AnnotationDataSet, _> =
                    self.store().get(*dataset_handle);
                let dataset = dataset.map_err(serde::ser::Error::custom)?;
                let data: Result<&AnnotationData, _> =
                    dataset.get(*data_handle);
                let data = data.map_err(serde::ser::Error::custom)?;
                let mut state = serializer.serialize_struct("Selector", 3)?;
                state.serialize_field("@type", "DataKeySelector")?;
                if let Some(id) = dataset.id() {
                    state.serialize_field("annotationset", &id)?;
                } else {
                    state.serialize_field("annotationset", &dataset.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                if let Some(id) = data.id() {
                    state.serialize_field("data", &id)?;
                } else {
                    state.serialize_field("data", &dataset.temp_id().map_err(serde::ser::Error::custom)?)?;
                }
                state.end()
            }
            Selector::MultiSelector(subselectors) => {
                let mut state = serializer.serialize_struct("Selector", 2)?;
                state.serialize_field("@type", "MultiSelector")?;
                let subselectors = WrappedSelectors {
                    selectors: subselectors,
                    store: self.store,
                };
                state.serialize_field("selectors", &subselectors)?;
                state.end()
            }
            Selector::CompositeSelector(subselectors) => {
                let mut state = serializer.serialize_struct("Selector", 2)?;
                state.serialize_field("@type", "CompositeSelector")?;
                let subselectors = WrappedSelectors {
                    selectors: subselectors,
                    store: self.store,
                };
                state.serialize_field("selectors", &subselectors)?;
                state.end()
            }
            Selector::DirectionalSelector(subselectors) => {
                let mut state = serializer.serialize_struct("Selector", 2)?;
                state.serialize_field("@type", "DirectionalSelector")?;
                let subselectors = WrappedSelectors {
                    selectors: subselectors,
                    store: self.store,
                };
                state.serialize_field("selectors", &subselectors)?;
                state.end()
            }
            Selector::RangedTextSelector { .. } 
            | Selector::RangedAnnotationSelector { .. }
            => {
                Err(serde::ser::Error::custom(
                    "Internal Ranged selectors can not be serialized directly, they can be serialized only when under a complex selector",
                ))
            }
        }
    }
}

impl Selector {
    /// Returns an iterator that yields all Selectors under a particular selector, including the selcetor in question as well.
    /// The parameter `recurse_annotation` determines whether an AnnotationSelector will be resolved recursively or not (finding all it points at)
    pub fn iter<'a>(
        &'a self,
        store: &'a AnnotationStore,
        recurse_annotation: bool,
    ) -> SelectorIter<'a> {
        SelectorIter {
            selector: self,
            subiterstack: Vec::new(),
            cursor_in_range: 0,
            recurse_annotation,
            store,
            done: false,
        }
    }
}

/// Iterator that returns the selector itself, plus all selectors under it (recursively)
pub struct SelectorIter<'a> {
    selector: &'a Selector, //we keep the root item out of subiterstack to save ourselves the Vec<> allocation
    done: bool,
    subiterstack: Vec<SelectorIter<'a>>,
    ///used to track iteration of InternalRangedSelectors, starts at 0 (not begin)
    cursor_in_range: usize,
    /// follow AnnotationSelectors recursively
    pub(crate) recurse_annotation: bool,
    pub(crate) store: &'a AnnotationStore,
}


impl<'a> SelectorIter<'a> {
    fn get_internal_ranged_item(&self, selector: &'a Selector) -> Cow<'a,Selector> {
        match selector {
            Selector::RangedAnnotationSelector { begin, with_text, .. } => {
                let handle = AnnotationHandle::new(begin.as_usize() + self.cursor_in_range);
                if *with_text {
                    let annotation: &Annotation = self.store.get(handle).expect("annotation handle must be valid");
                    if let (Some(textselection_handle), Some(resource_handle)) = (annotation.target().textselection_handle(), annotation.target().resource_handle()) {
                        Cow::Owned(Selector::AnnotationSelector(handle, Some((resource_handle, textselection_handle, OffsetMode::default()))))
                    } else {
                        Cow::Owned(Selector::AnnotationSelector(handle, None))
                    }
                } else {
                        Cow::Owned(Selector::AnnotationSelector(handle, None))
                }
            }
            Selector::RangedTextSelector { resource, begin, .. } => {
                Cow::Owned(Selector::TextSelector(*resource, TextSelectionHandle::new(begin.as_usize() + self.cursor_in_range), OffsetMode::default()) )
            }
            _ => {
                unreachable!()
            }
        }
    }
}

impl<'a> Iterator for SelectorIter<'a> {
    type Item = Cow<'a, Selector>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.subiterstack.is_empty() {
                if !self.done {
                    match self.selector {
                        //  higher-order annotation, appends to the subiterstack
                        Selector::AnnotationSelector(a_handle, _) => {
                            if self.recurse_annotation {
                                let annotation: &Annotation = self
                                    .store
                                    .get(*a_handle)
                                    .expect("referenced annotation must exist");
                                self.subiterstack.push(SelectorIter {
                                    selector: annotation.target(),
                                    //note: Vec::new() should be cheap as Vec only allocates on push!
                                    subiterstack: Vec::new(),
                                    cursor_in_range: 0,
                                    recurse_annotation: self.recurse_annotation,
                                    store: self.store,
                                    done: false,
                                });
                            }
                        }
                        // complex iterators, these append to the subiterstack
                        Selector::MultiSelector(v)
                        | Selector::CompositeSelector(v)
                        | Selector::DirectionalSelector(v) => {
                            for subselector in v.iter().rev() {
                                self.subiterstack.push(SelectorIter {
                                    selector: subselector,
                                    //note: Vec::new() should be cheap as Vec only allocates on push!
                                    subiterstack: Vec::new(),
                                    cursor_in_range: 0,
                                    recurse_annotation: self.recurse_annotation,
                                    store: self.store,
                                    done: false,
                                });
                            }
                        }
                        Selector::RangedAnnotationSelector { begin, end, .. } => {
                            if begin.as_usize() + self.cursor_in_range > end.as_usize() { //end is inclusive
                                //we're done with this iterator
                                self.done = true; //this flags that we have processed this selector
                                return None;
                            } else {
                                let result = self.get_internal_ranged_item(self.selector);
                                self.cursor_in_range += 1;
                                return Some(result);
                            }
                        }
                        Selector::RangedTextSelector { resource: _, begin, end } => {
                            if begin.as_usize() + self.cursor_in_range > end.as_usize() { //end is inclusive
                                //we're done with this iterator
                                self.done = true; //this flags that we have processed this selector
                                return None;
                            } else {
                                let result = self.get_internal_ranged_item(self.selector);
                                self.cursor_in_range += 1;
                                return Some(result);
                            }
                        },
                        // simple selectors fall back to the default behaviour after this match clause
                        Selector::TextSelector(..) => {},
                        Selector::DataSetSelector(_) => {}
                        Selector::ResourceSelector(_) => {}
                        Selector::DataKeySelector(..) => {}
                        Selector::AnnotationDataSelector(..) => {}
                    };
                    self.done = true; //this flags that we have processed the selector
                    return Some(Cow::Borrowed(self.selector));
                } else {
                    return None;
                }
            } else {
                let result = self.subiterstack.last_mut().unwrap().next();
                if result.is_none() {
                    self.subiterstack.pop();
                    if self.subiterstack.is_empty() {
                        return None;
                    } else {
                        continue; //recursion
                    }
                } else {
                    return result;
                }
            }
        }
    }
}