Skip to content

Commit

Permalink
moved some things from the lower-level API to the higher-level one
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Nov 18, 2023
1 parent 45fe7ee commit 3758d57
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 67 deletions.
59 changes: 59 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ pub use resources::*;
pub use text::*;
pub use textselection::*;

use crate::annotation::AnnotationHandle;
use crate::annotationdata::AnnotationDataHandle;
use crate::annotationdataset::AnnotationDataSetHandle;
use crate::annotationstore::AnnotationStore;
use crate::datakey::DataKeyHandle;
use crate::datavalue::DataOperator;
use crate::resources::TextResourceHandle;
use crate::textselection::TextSelectionOperator;

use std::borrow::Cow;

Expand Down Expand Up @@ -82,3 +89,55 @@ where
}
}
}

pub trait TestableIterator: Iterator
where
Self: Sized,
{
/// Returns true if the iterator has items, false otherwise
fn test(mut self) -> bool {
self.next().is_some()
}
}

pub trait AbortableIterator: Iterator
where
Self: Sized,
{
/// Set the iterator to abort, no further results will be returned
fn abort(&mut self);
}

// Auxiliary data structures the API relies on internally:

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub(crate) enum FilterMode {
Any,
All,
}

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub(crate) enum TextMode {
Exact,
Lowercase,
}

#[derive(Debug)]
/// A filter that is evaluated lazily, applied on [`AnnotationsIter`], [`DataIter`],[`TextSelectionsIter`]
pub(crate) enum Filter<'a> {
AnnotationData(AnnotationDataSetHandle, AnnotationDataHandle),
AnnotationDataSet(AnnotationDataSetHandle),
DataKey(AnnotationDataSetHandle, DataKeyHandle),
Annotation(AnnotationHandle),
TextResource(TextResourceHandle),
DataOperator(DataOperator<'a>),
TextSelectionOperator(TextSelectionOperator),
Annotations(Annotations<'a>),
Data(Data<'a>, FilterMode),
Text(String, TextMode, &'a str), //the last string represents the delimiter for joining text

//these have the advantage the collections are external references
BorrowedAnnotations(&'a Annotations<'a>),
BorrowedData(&'a Data<'a>, FilterMode),
BorrowedText(&'a str, TextMode, &'a str), //the last string represents the delimiter for joining text
}
23 changes: 8 additions & 15 deletions src/api/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use crate::annotation::{Annotation, AnnotationHandle, TargetIter};
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::annotationstore::AnnotationStore;
use crate::api::annotationdata::{Data, DataIter};
use crate::api::resources::ResourcesIter;
use crate::api::textselection::TextSelectionsIter;
use crate::api::*;
use crate::datakey::DataKey;
use crate::datavalue::DataOperator;
use crate::resources::{TextResource, TextResourceHandle};
Expand All @@ -36,8 +34,6 @@ use crate::textselection::{
use crate::*;
use crate::{Filter, FilterMode, IntersectionIter, TextMode};

use crate::api::textselection::SortTextualOrder;

/// This is the implementation of the high-level API for [`Annotation`].
impl<'store> ResultItem<'store, Annotation> {
/// Returns an iterator over the resources that this annotation (by its target selector) references.
Expand Down Expand Up @@ -896,16 +892,6 @@ impl<'store> AnnotationsIter<'store> {
}
}

/// Set the iterator to abort, no further results will be returned
pub fn abort(&mut self) {
self.iter.as_mut().map(|iter| iter.abort = true);
}

/// Returns true if the iterator has items, false otherwise
pub fn test(mut self) -> bool {
self.next().is_some()
}

/// See if the filters match for the annotation
/// This does not include any filters directly on annotations, as those are handled already by the underlying IntersectionsIter
fn test_filters(&self, annotation: &ResultItem<'store, Annotation>) -> bool {
Expand Down Expand Up @@ -1026,6 +1012,13 @@ impl<'store> AnnotationsIter<'store> {
}
}

impl<'store> AbortableIterator for AnnotationsIter<'store> {
fn abort(&mut self) {
self.iter.as_mut().map(|iter| iter.abort = true);
}
}
impl<'store> TestableIterator for AnnotationsIter<'store> {}

impl<'store> Iterator for AnnotationsIter<'store> {
type Item = ResultItem<'store, Annotation>;

Expand Down
15 changes: 8 additions & 7 deletions src/api/annotationdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use crate::annotation::Annotation;
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::annotationstore::AnnotationStore;
use crate::api::annotation::AnnotationsIter;
use crate::api::HandleCollection;
use crate::api::*;
use crate::datakey::{DataKey, DataKeyHandle};
use crate::datavalue::{DataOperator, DataValue};
use crate::resources::TextResource;
Expand Down Expand Up @@ -204,6 +203,13 @@ pub struct DataIter<'store> {
last_set: Option<ResultItem<'store, AnnotationDataSet>>,
}

impl<'store> AbortableIterator for DataIter<'store> {
fn abort(&mut self) {
self.iter.as_mut().map(|iter| iter.abort = true);
}
}
impl<'store> TestableIterator for DataIter<'store> {}

impl<'store> DataIter<'store> {
pub(crate) fn new(
iter: IntersectionIter<'store, (AnnotationDataSetHandle, AnnotationDataHandle)>,
Expand Down Expand Up @@ -516,11 +522,6 @@ impl<'store> DataIter<'store> {
}
}

/// Returns true if the iterator has items, false otherwise
pub fn test(mut self) -> bool {
self.next().is_some()
}

/// Does this iterator return items in sorted order?
pub fn returns_sorted(&self) -> bool {
if let Some(iter) = self.iter.as_ref() {
Expand Down
5 changes: 1 addition & 4 deletions src/api/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use crate::annotation::{Annotation, AnnotationHandle};
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::annotationstore::AnnotationStore;
use crate::api::annotation::{Annotations, AnnotationsIter};
use crate::api::annotationdata::Data;
use crate::api::textselection::TextSelectionsIter;
use crate::api::HandleCollection;
use crate::api::*;
use crate::datakey::DataKey;
use crate::datavalue::DataOperator;
use crate::resources::{TextResource, TextResourceHandle};
Expand Down
12 changes: 3 additions & 9 deletions src/api/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use crate::annotation::{Annotation, AnnotationHandle};
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
use crate::annotationstore::AnnotationStore;
use crate::api::annotation::{Annotations, AnnotationsIter};
use crate::api::annotationdata::Data;
use crate::api::resources::ResourcesIter;
use crate::api::HandleCollection;
use crate::api::*;
use crate::datakey::DataKey;
use crate::datavalue::DataOperator;
use crate::error::*;
Expand Down Expand Up @@ -1081,11 +1078,6 @@ impl<'store> TextSelectionsIter<'store> {
self.map(|textselection| textselection.text())
}

/// Returns true if the iterator has items, false otherwise
pub fn test(mut self) -> bool {
self.next().is_some()
}

/// Returns all underlying text concatenated into a single String
pub fn text_join(self, delimiter: &str) -> String {
let mut s = String::new();
Expand All @@ -1112,6 +1104,8 @@ impl<'store> TextSelectionsIter<'store> {
}
}

impl TestableIterator for TextSelectionsIter<'_> {}

#[sealed]
impl TypeInfo for Option<ResultTextSelection<'_>> {
fn typeinfo() -> Type {
Expand Down
32 changes: 0 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,6 @@ mod tests;
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub(crate) enum FilterMode {
Any,
All,
}

#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub(crate) enum TextMode {
Exact,
Lowercase,
}

#[derive(Debug)]
/// A filter that is evaluated lazily, applied on [`AnnotationsIter`], [`DataIter`],[`TextSelectionsIter`]
pub(crate) enum Filter<'a> {
AnnotationData(AnnotationDataSetHandle, AnnotationDataHandle),
AnnotationDataSet(AnnotationDataSetHandle),
DataKey(AnnotationDataSetHandle, DataKeyHandle),
Annotation(AnnotationHandle),
TextResource(TextResourceHandle),
DataOperator(DataOperator<'a>),
TextSelectionOperator(TextSelectionOperator),
Annotations(Annotations<'a>),
Data(Data<'a>, FilterMode),
Text(String, TextMode, &'a str), //the last string represents the delimiter for joining text

//these have the advantage the collections are external references
BorrowedAnnotations(&'a Annotations<'a>),
BorrowedData(&'a Data<'a>, FilterMode),
BorrowedText(&'a str, TextMode, &'a str), //the last string represents the delimiter for joining text
}

// Lazy iterator computing an intersection
pub(crate) struct IntersectionIter<'a, T>
where
Expand Down

0 comments on commit 3758d57

Please sign in to comment.