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
/*
    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 high-level API for [`TextResource`]. This API is implemented on
//! [`ResultItem<TextResource>`].

use crate::annotation::Annotation;
use crate::annotationdata::AnnotationData;
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::api::*;
use crate::datakey::DataKey;
use crate::datavalue::DataOperator;
use crate::error::*;
use crate::resources::{PositionMode, TextResource, TextResourceHandle};
use crate::selector::Offset;
use crate::store::*;
use crate::text::Text;
use crate::textselection::{ResultTextSelection, TextSelectionOperator, TextSelectionSet};
use crate::{Filter, FilterMode};

use rayon::prelude::*;

impl<'store> FullHandle<TextResource> for ResultItem<'store, TextResource> {
    fn fullhandle(&self) -> <TextResource as Storable>::FullHandleType {
        self.handle()
    }
}

/// This is the implementation of the high-level API for [`TextResource`].
impl<'store> ResultItem<'store, TextResource> {
    /// Returns an iterator over all annotations about this resource as a whole, i.e. Annotations with a ResourceSelector.
    /// Such annotations can be considered metadata.
    pub fn annotations_as_metadata(
        &self,
    ) -> ResultIter<impl Iterator<Item = ResultItem<'store, Annotation>>> {
        if let Some(annotations) = self.store().annotations_by_resource_metadata(self.handle()) {
            ResultIter::new_sorted(FromHandles::new(annotations.iter().copied(), self.store()))
        } else {
            ResultIter::new_empty()
        }
    }

    /// Returns an iterator over all annotations about any text in this resource i.e. Annotations with a TextSelector.
    pub fn annotations(&self) -> ResultIter<impl Iterator<Item = ResultItem<'store, Annotation>>> {
        if let Some(iter) = self.store().annotations_by_resource(self.handle()) {
            let mut data: Vec<_> = iter.collect();
            data.sort_unstable();
            data.dedup();
            ResultIter::new_sorted(FromHandles::new(data.into_iter(), self.store()))
        } else {
            ResultIter::new_empty()
        }
    }

    /// Returns an iterator over all text selections that are marked in this resource (i.e. there are one or more annotations on it).
    /// They are returned in textual order, but this does not come with any significant performance overhead. If you want an unsorted version use [`self.as_ref().textselections_unsorted()`] instead.
    /// Note: This is a double-ended iterator that can be traversed in both directions.
    pub fn textselections(&self) -> impl DoubleEndedIterator<Item = ResultTextSelection<'store>> {
        let resource = self.as_ref();
        let rootstore = self.rootstore();
        ResultTextSelections::new(
            resource
                .iter()
                .map(|x| x.as_resultitem(resource, rootstore)),
        )
    }

    pub fn textselection_by_handle(
        &self,
        handle: TextSelectionHandle,
    ) -> Result<ResultTextSelection<'store>, StamError> {
        let textselection: &TextSelection = self.as_ref().get(handle)?;
        Ok(ResultTextSelection::Bound(
            textselection.as_resultitem(self.as_ref(), self.store()),
        ))
    }

    /// Get a text selection pointing to the whole resource, you can also call this implicitly via the `From` trait (`resource.into()`).
    pub fn to_textselection(self) -> ResultTextSelection<'store> {
        self.textselection(&Offset::whole())
            .expect("to_textselection() should never fail")
    }

    /// Returns a sorted double-ended iterator over a range of all textselections and returns all
    /// textselections that either start or end in this range (depending on the direction you're
    /// iterating in)
    pub fn textselections_in_range(
        &self,
        begin: usize,
        end: usize,
    ) -> impl DoubleEndedIterator<Item = ResultTextSelection<'store>> {
        let resource = self.as_ref();
        let rootstore = self.rootstore();
        ResultTextSelections::new(
            resource
                .range(begin, end)
                .map(|x| x.as_resultitem(resource, rootstore)),
        )
    }

    /// Returns the number of textselections that are marked in this resource (i.e. there are one or more annotations on it).
    pub fn textselections_len(&self) -> usize {
        self.as_ref().textselections_len()
    }

    /// Find textselections by applying a text selection operator ([`TextSelectionOperator`]) to a
    /// one or more querying textselections. Returns an iterator over all matching
    /// text selections in the resource.
    pub fn related_text(
        &self,
        operator: TextSelectionOperator,
        refset: impl Into<TextSelectionSet>,
    ) -> impl Iterator<Item = ResultTextSelection<'store>> {
        let resource = self.as_ref();
        let rootstore = self.rootstore();
        ResultTextSelections::new(
            resource
                .textselections_by_operator(operator, refset.into())
                .filter_map(|handle| {
                    resource
                        .get(handle)
                        .ok()
                        .map(|x| x.as_resultitem(resource, rootstore))
                }),
        )
    }

    /// Search for annotations *about* this resource, satisfying certain exact data that is already known.
    /// For a higher-level variant, see `find_data_about`, this method is more efficient.
    /// Both the matching data as well as the matching annotation will be returned in an iterator.
    pub fn annotations_by_metadata_about(
        &self,
        data: ResultItem<'store, AnnotationData>,
    ) -> impl Iterator<Item = ResultItem<'store, Annotation>> + 'store {
        self.annotations_as_metadata()
            .filter(move |annotation| annotation.has_data(&data))
    }

    /// Tests if the resource has certain data in annotations that reference this textselection, returns a boolean.
    /// If you don't have a data instance yet, use `test_data_about()` instead.
    /// This method is much more efficient than `test_data_about()`.
    pub fn has_metadata_about(&self, data: ResultItem<'store, AnnotationData>) -> bool {
        self.annotations_by_metadata_about(data).test()
    }

    /// Iterator covering the full text of the resource as a sequence of minimum-length non-overlapping TextSelections, in textual order
    pub fn segmentation(&self) -> SegmentationIter<'store> {
        SegmentationIter {
            positions: self.as_ref().positions(PositionMode::Both),
            resource: self.clone(),
            cursor: 0,
            end: self.as_ref().textlen(),
        }
    }

    /// Iterator covering a range of text of the resource as a sequence of minimum-length non-overlapping TextSelections, in textual order
    pub fn segmentation_in_range(&self, begin: usize, end: usize) -> SegmentationIter<'store> {
        SegmentationIter {
            positions: self
                .as_ref()
                .positions_in_range(PositionMode::Both, begin, end),
            resource: self.clone(),
            cursor: begin,
            end,
        }
    }
}

/// Holds a collection of [`TextResource`] (by reference to an [`AnnotationStore`] and handles). This structure is produced by calling
/// [`ToHandles::to_handles()`], which is available on all iterators over resources ([`ResultItem<TextResource>`]).
pub type Resources<'store> = Handles<'store, TextResource>;

impl<'store, I> FullHandleToResultItem<'store, TextResource>
    for FromHandles<'store, TextResource, I>
where
    I: Iterator<Item = TextResourceHandle>,
{
    fn get_item(&self, handle: TextResourceHandle) -> Option<ResultItem<'store, TextResource>> {
        self.store.resource(handle)
    }
}

impl<'store, I> FullHandleToResultItem<'store, TextResource>
    for FilterAllIter<'store, TextResource, I>
where
    I: Iterator<Item = ResultItem<'store, TextResource>>,
{
    fn get_item(&self, handle: TextResourceHandle) -> Option<ResultItem<'store, TextResource>> {
        self.store.resource(handle)
    }
}

/// Trait for iteration over resources ([`ResultItem<TextResource>`]; encapsulation over
/// [`TextResource`]). Implements numerous filter methods to further constrain the iterator, as well
/// as methods to map from resources to other items.
pub trait ResourcesIterator<'store>: Iterator<Item = ResultItem<'store, TextResource>>
where
    Self: Sized,
{
    fn parallel(self) -> rayon::vec::IntoIter<ResultItem<'store, TextResource>> {
        let annotations: Vec<_> = self.collect();
        annotations.into_par_iter()
    }

    /// Iterates over all the annotations for all resources in this iterator.
    /// This only returns annotations that target the resource via a ResourceSelector.
    ///
    /// The iterator will be consumed and an extra buffer is allocated.
    /// Annotations will be returned sorted chronologically and returned without duplicates
    ///
    /// If you want annotations unsorted and with possible duplicates, then just do:  `.map(|res| res.annotations()).flatten()` instead
    fn annotations_as_metadata(
        self,
    ) -> ResultIter<<Vec<ResultItem<'store, Annotation>> as IntoIterator>::IntoIter> {
        let mut annotations: Vec<_> = self
            .map(|resource| resource.annotations_as_metadata())
            .flatten()
            .collect();
        annotations.sort_unstable();
        annotations.dedup();
        ResultIter::new_sorted(annotations.into_iter())
    }

    /// Iterates over all the annotations for all resources in this iterator.
    /// This only returns annotations that target the resource via a TextSelector.
    ///
    /// The iterator will be consumed and an extra buffer is allocated.
    /// Annotations will be returned sorted chronologically and returned without duplicates
    fn annotations(
        self,
    ) -> ResultIter<<Vec<ResultItem<'store, Annotation>> as IntoIterator>::IntoIter> {
        let mut annotations: Vec<_> = self
            .map(|resource| resource.annotations())
            .flatten()
            .collect();
        annotations.sort_unstable();
        annotations.dedup();
        ResultIter::new_sorted(annotations.into_iter())
    }

    /// Iterates over all textselections for all resources in this iterator, in resource order and textual order.
    fn textselections(
        self,
    ) -> ResultIter<<Vec<ResultTextSelection<'store>> as IntoIterator>::IntoIter> {
        let textselections: Vec<_> = self
            .map(|resource| resource.textselections())
            .flatten()
            .collect();
        ResultIter::new_unsorted(textselections.into_iter()) //not chronologically sorted
    }

    /// Constrain this iterator to filter only a single resource (by handle). This is a lower-level method, use [`Self::filter_one()`] instead.
    /// This method can only be used once! Use [`Self::filter_any()`], [`Self::filter_any_byref()`] to filter on multiple annotations (disjunction), or [`Self::filter_all()`] (conjunction).
    fn filter_handle(self, handle: TextResourceHandle) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::TextResource(handle, SelectionQualifier::Normal),
        }
    }

    /// Constrain this iterator to only a single resource
    /// This method can only be used once! Use [`Self::filter_any()`], [`Self::filter_any_byref()`] to filter on multiple annotations (disjunction), or [`Self::filter_all()`] (conjunction).
    fn filter_one(self, resource: &ResultItem<TextResource>) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::TextResource(resource.handle(), SelectionQualifier::Normal),
        }
    }

    /// Constrain this iterator to filter on one of the mentioned resources
    fn filter_any(self, resources: Resources<'store>) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Resources(resources, FilterMode::Any, SelectionQualifier::Normal),
        }
    }

    /// Constrain this iterator to filter on one of the mentioned resources
    fn filter_any_byref(
        self,
        resources: &'store Resources<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::BorrowedResources(
                resources,
                FilterMode::Any,
                SelectionQualifier::Normal,
            ),
        }
    }

    /// Constrain this iterator to filter on *all* of the mentioned resources.
    /// If not all of the items in the parameter exist in the iterator, the iterator returns nothing.
    fn filter_all(
        self,
        resources: Resources<'store>,
        store: &'store AnnotationStore,
    ) -> FilterAllIter<'store, TextResource, Self> {
        FilterAllIter::new(self, resources, store)
    }

    /// Constrain the iterator to only return resources with metadata annotations (i.e. via a ResourceSelector) that have data that corresponds with any of the items in the passed data.
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations and data for each resource.
    fn filter_metadata(
        self,
        data: Data<'store>,
        mode: FilterMode,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Data(data, mode, SelectionQualifier::Metadata),
        }
    }

    /// Constrain the iterator to only return resources with annotations via a TextSelector that have data that corresponds with any of the items in the passed data.
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations and data for each resource.
    fn filter_data_on_text(
        self,
        data: Data<'store>,
        mode: FilterMode,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Data(data, mode, SelectionQualifier::Normal),
        }
    }

    /// Constrain the iterator to only return resources with annotations that match the ones passed
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations for each resource
    fn filter_annotations_on_text(
        self,
        annotations: Annotations<'store>,
        mode: FilterMode,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Annotations(
                annotations,
                mode,
                SelectionQualifier::Normal,
                AnnotationDepth::default(),
            ),
        }
    }

    /// Constrain the iterator to only return resources with annotations that match the ones passed
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations for each resource
    fn filter_annotations_byref(
        self,
        annotations: &'store Annotations<'store>,
        mode: FilterMode,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::BorrowedAnnotations(
                annotations,
                mode,
                SelectionQualifier::Normal,
                AnnotationDepth::default(),
            ),
        }
    }

    /// Constrain this iterator to resources with this specific annotation
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations for each resource.
    fn filter_annotation_on_text(
        self,
        annotation: &ResultItem<Annotation>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Annotation(
                annotation.handle(),
                SelectionQualifier::Normal,
                AnnotationDepth::default(),
            ),
        }
    }

    /// Constrain the iterator to only return resources with annotations (as metadata, i.e. via a ResourceSelector) that match the ones passed
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations for each resource
    fn filter_annotations_as_metadata(
        self,
        annotations: Annotations<'store>,
        mode: FilterMode,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Annotations(
                annotations,
                mode,
                SelectionQualifier::Metadata,
                AnnotationDepth::default(),
            ),
        }
    }

    /// Constrain this iterator to resources with this specific annotation (as metadata, i.e. via a ResourceSelector)
    ///
    /// This filter is evaluated lazily, it will obtain and check the annotations for each resource.
    fn filter_annotation_as_metadata(
        self,
        annotation: &ResultItem<Annotation>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::Annotation(
                annotation.handle(),
                SelectionQualifier::Metadata,
                AnnotationDepth::default(),
            ),
        }
    }

    fn filter_annotationdata_on_text(
        self,
        data: &ResultItem<'store, AnnotationData>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationData(
                data.set().handle(),
                data.handle(),
                SelectionQualifier::Normal,
            ),
        }
    }

    /// Constrain the iterator to return only the resources that have this exact data item.
    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_annotationdata_in_metadata(
        self,
        data: &ResultItem<'store, AnnotationData>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationData(
                data.set().handle(),
                data.handle(),
                SelectionQualifier::Metadata,
            ),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_key_value_in_metadata(
        self,
        key: &ResultItem<'store, DataKey>,
        value: DataOperator<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKeyAndOperator(
                key.set().handle(),
                key.handle(),
                value,
                SelectionQualifier::Metadata,
            ),
        }
    }

    fn filter_key_value_on_text(
        self,
        key: &ResultItem<'store, DataKey>,
        value: DataOperator<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKeyAndOperator(
                key.set().handle(),
                key.handle(),
                value,
                SelectionQualifier::Normal,
            ),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_key_on_text(
        self,
        key: &ResultItem<'store, DataKey>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKey(key.set().handle(), key.handle(), SelectionQualifier::Normal),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_key_in_metadata(
        self,
        key: &ResultItem<'store, DataKey>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKey(
                key.set().handle(),
                key.handle(),
                SelectionQualifier::Metadata,
            ),
        }
    }

    /// This filter considers only annotations on the text (i.e. via a TextSelector)
    fn filter_key_handle_on_text(
        self,
        set: AnnotationDataSetHandle,
        key: DataKeyHandle,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKey(set, key, SelectionQualifier::Normal),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_key_handle_in_metadata(
        self,
        set: AnnotationDataSetHandle,
        key: DataKeyHandle,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKey(set, key, SelectionQualifier::Metadata),
        }
    }

    /// This filter considers only annotations on the text (i.e. via a TextSelector)
    fn filter_value_on_text(self, value: DataOperator<'store>) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataOperator(value, SelectionQualifier::Normal),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_value_in_metadata(
        self,
        value: DataOperator<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataOperator(value, SelectionQualifier::Metadata),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_key_handle_value_in_metadata(
        self,
        set: AnnotationDataSetHandle,
        key: DataKeyHandle,
        value: DataOperator<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Metadata),
        }
    }

    /// This filter considers only annotations on the text (i.e. via a TextSelector)
    fn filter_key_handle_value_on_text(
        self,
        set: AnnotationDataSetHandle,
        key: DataKeyHandle,
        value: DataOperator<'store>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Normal),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_set_in_metadata(
        self,
        set: &ResultItem<'store, AnnotationDataSet>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationDataSet(set.handle(), SelectionQualifier::Metadata),
        }
    }

    /// This filter considers only annotations on the text (i.e. via a TextSelector)
    fn filter_set_on_text(
        self,
        set: &ResultItem<'store, AnnotationDataSet>,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationDataSet(set.handle(), SelectionQualifier::Normal),
        }
    }

    /// This filter considers only annotations as metadata (i.e. via a ResourceSelector)
    fn filter_set_handle_in_metadata(
        self,
        set: AnnotationDataSetHandle,
    ) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationDataSet(set, SelectionQualifier::Metadata),
        }
    }

    /// This filter considers only annotations on the text (i.e. via a TextSelector)
    fn filter_set_handle(self, set: AnnotationDataSetHandle) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter: Filter::AnnotationDataSet(set, SelectionQualifier::Normal),
        }
    }
}

/*
//not needed yet
pub(crate) trait ResourcesPrivIterator<'store>:
    Iterator<Item = ResultItem<'store, TextResource>>
where
    Self: Sized,
{
    fn filter_custom(self, filter: Filter) -> FilteredResources<'store, Self> {
        FilteredResources {
            inner: self,
            filter,
        }
    }
}
*/

impl<'store, I> ResourcesIterator<'store> for I
where
    I: Iterator<Item = ResultItem<'store, TextResource>>,
{
    //blanket implementation
}

/// An iterator that applies a filter to constrain resources.
/// This iterator implements [`ResourcesIterator`]
/// and is itself produced by the various `filter*()` methods on that trait.
pub struct FilteredResources<'store, I>
where
    I: Iterator<Item = ResultItem<'store, TextResource>>,
{
    inner: I,
    filter: Filter<'store>,
}

impl<'store, I> Iterator for FilteredResources<'store, I>
where
    I: Iterator<Item = ResultItem<'store, TextResource>>,
{
    type Item = ResultItem<'store, TextResource>;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(resource) = self.inner.next() {
                if self.test_filter(&resource) {
                    return Some(resource);
                }
            } else {
                return None;
            }
        }
    }
}

impl<'store, I> FilteredResources<'store, I>
where
    I: Iterator<Item = ResultItem<'store, TextResource>>,
{
    fn test_filter(&self, resource: &ResultItem<'store, TextResource>) -> bool {
        match &self.filter {
            Filter::TextResource(handle, _) => resource.handle() == *handle,
            Filter::Resources(handles, FilterMode::Any, _) => {
                handles.contains(&resource.fullhandle())
            }
            Filter::BorrowedResources(handles, FilterMode::Any, _) => {
                handles.contains(&resource.fullhandle())
            }
            Filter::Data(data, mode, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .filter_data_byref(data, *mode)
                .test(),
            Filter::Data(data, mode, SelectionQualifier::Normal) => {
                resource.annotations().filter_data_byref(data, *mode).test()
            }
            Filter::BorrowedData(data, mode, SelectionQualifier::Normal) => {
                resource.annotations().filter_data_byref(data, *mode).test()
            }
            Filter::BorrowedData(data, mode, SelectionQualifier::Metadata) => {
                resource.annotations().filter_data_byref(data, *mode).test()
            }
            Filter::Annotations(annotations, mode, SelectionQualifier::Normal, _) => resource
                .annotations()
                .filter_annotations_byref(annotations, *mode)
                .test(),
            Filter::Annotations(annotations, mode, SelectionQualifier::Metadata, _) => resource
                .annotations_as_metadata()
                .filter_annotations_byref(annotations, *mode)
                .test(),
            Filter::BorrowedAnnotations(annotations, mode, SelectionQualifier::Normal, _) => {
                resource
                    .annotations()
                    .filter_annotations_byref(annotations, *mode)
                    .test()
            }
            Filter::Annotation(annotation, SelectionQualifier::Normal, _) => {
                resource.annotations().filter_handle(*annotation).test()
            }
            Filter::Annotation(annotation, SelectionQualifier::Metadata, _) => resource
                .annotations_as_metadata()
                .filter_handle(*annotation)
                .test(),
            Filter::DataKey(set, key, SelectionQualifier::Normal) => resource
                .annotations()
                .data()
                .filter_key_handle(*set, *key)
                .test(),
            Filter::DataKey(set, key, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .data()
                .filter_key_handle(*set, *key)
                .test(),
            Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Normal) => resource
                .annotations()
                .data()
                .filter_key_handle_value(*set, *key, value.clone())
                .test(),
            Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .data()
                .filter_key_handle_value(*set, *key, value.clone())
                .test(),
            Filter::DataOperator(value, SelectionQualifier::Normal) => resource
                .annotations()
                .data()
                .filter_value(value.clone())
                .test(),
            Filter::DataOperator(value, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .data()
                .filter_value(value.clone())
                .test(),
            Filter::AnnotationDataSet(set, SelectionQualifier::Normal) => {
                resource.annotations().data().filter_set_handle(*set).test()
            }
            Filter::AnnotationDataSet(set, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .data()
                .filter_set_handle(*set)
                .test(),
            Filter::AnnotationData(set, data, SelectionQualifier::Normal) => resource
                .annotations()
                .data()
                .filter_handle(*set, *data)
                .test(),
            Filter::AnnotationData(set, data, SelectionQualifier::Metadata) => resource
                .annotations_as_metadata()
                .data()
                .filter_handle(*set, *data)
                .test(),
            _ => unreachable!(
                "Filter {:?} not implemented for FilteredResources",
                self.filter
            ),
        }
    }
}

pub struct SegmentationIter<'a> {
    positions: Box<dyn Iterator<Item = &'a usize> + 'a>,
    resource: ResultItem<'a, TextResource>,
    cursor: usize,
    end: usize,
}

impl<'a> Iterator for SegmentationIter<'a> {
    type Item = ResultTextSelection<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.cursor >= self.end {
                return None;
            }

            if let Some(pos) = self.positions.next() {
                // get the positionitem to filter out textselections that have no annotations associated (e.g. the milestones)
                let positionitem = self
                    .resource
                    .as_ref()
                    .position(*pos)
                    .expect("positionitem must exist");
                if *pos > self.cursor
                    && (positionitem.len_begin2end() > 0 || positionitem.len_end2begin() > 0)
                {
                    if *pos > self.end {
                        //clipped segment
                        let textselection = self
                            .resource
                            .textselection(&Offset::simple(self.cursor, self.end))
                            .expect("textselection must succeed");
                        self.cursor = self.end;
                        return Some(textselection);
                    } else {
                        //normal segment
                        let textselection = self
                            .resource
                            .textselection(&Offset::simple(self.cursor, *pos))
                            .expect("textselection must succeed");
                        self.cursor = *pos;
                        return Some(textselection);
                    }
                } else {
                    continue;
                }
            } else {
                let textselection = self
                    .resource
                    .textselection(&Offset::simple(self.cursor, self.end))
                    .expect("textselection must succeed");
                self.cursor = self.end;
                return Some(textselection);
            }
        }
    }
}