Skip to content

Commit

Permalink
implemented more borrowed variants for Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Dec 9, 2023
1 parent 09d9ddf commit 760dc4a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,5 @@ pub(crate) enum Filter<'a> {
BorrowedAnnotations(&'a Annotations<'a>),
BorrowedData(&'a Data<'a>, FilterMode),
BorrowedText(&'a str, TextMode, &'a str), //the last string represents the delimiter for joining text
BorrowedResources(&'a Handles<'a, TextResource>),
}
39 changes: 37 additions & 2 deletions src/api/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ where
}
}

/// Constrain this iterator to filter on one of the mentioned annotations
fn filter_annotations_byref(
self,
annotations: &'store Annotations<'store>,
) -> FilteredAnnotations<'store, Self> {
FilteredAnnotations {
inner: self,
filter: Filter::BorrowedAnnotations(annotations),
}
}

/// Constrain this iterator to filter only a single annotation (by handle). This is a lower-level method, use [`Self::filter_annotation()`] instead.
/// This method can only be used once! Use [`Self::filter_annotations()`] to filter on multiple annotations (disjunction).
fn filter_handle(self, handle: AnnotationHandle) -> FilteredAnnotations<'store, Self> {
Expand All @@ -412,6 +423,13 @@ where
}
}

fn filter_data_byref(self, data: &'store Data<'store>) -> FilteredAnnotations<'store, Self> {
FilteredAnnotations {
inner: self,
filter: Filter::BorrowedData(data, FilterMode::Any),
}
}

/// Constrain the iterator to only return annotations that, in a single annotation, has data that corresponds with *ALL* of the items in the passed data.
/// All items have to be found or none will be returned.
///
Expand All @@ -427,6 +445,16 @@ where
}
}

fn filter_data_all_byref(
self,
data: &'store Data<'store>,
) -> FilteredAnnotations<'store, Self> {
FilteredAnnotations {
inner: self,
filter: Filter::BorrowedData(data, FilterMode::All),
}
}

fn filter_resource(
self,
resource: &ResultItem<'store, TextResource>,
Expand Down Expand Up @@ -622,11 +650,18 @@ where
match &self.filter {
Filter::Annotation(handle) => annotation.handle() == *handle,
Filter::Annotations(handles) => handles.contains(&annotation.fullhandle()),
Filter::BorrowedAnnotations(handles) => handles.contains(&annotation.fullhandle()),
Filter::Data(data, FilterMode::Any) => {
annotation.data().filter_data(data.clone()).test()
annotation.data().filter_data_byref(&data).test()
}
Filter::Data(data, FilterMode::All) => {
annotation.data().filter_data(data.clone()).count() >= data.len()
annotation.data().filter_data_byref(&data).count() >= data.len()
}
Filter::BorrowedData(data, FilterMode::Any) => {
annotation.data().filter_data_byref(data).test()
}
Filter::BorrowedData(data, FilterMode::All) => {
annotation.data().filter_data_byref(data).count() >= data.len()
}
Filter::DataKey(set, key) => annotation.data().filter_key_handle(*set, *key).test(),
Filter::DataKeyAndOperator(set, key, value) => annotation
Expand Down
8 changes: 8 additions & 0 deletions src/api/annotationdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ where
}
}

fn filter_data_byref(self, data: &'store Data<'store>) -> FilteredData<'store, Self> {
FilteredData {
inner: self,
filter: Filter::BorrowedData(data, FilterMode::Any),
}
}

fn filter_annotationdata(
self,
data: &ResultItem<'store, AnnotationData>,
Expand Down Expand Up @@ -309,6 +316,7 @@ where
data.handle() == *data_handle && data.set().handle() == *set_handle
}
Filter::Data(v, _) => v.contains(&data.fullhandle()),
Filter::BorrowedData(v, _) => v.contains(&data.fullhandle()),
Filter::AnnotationDataSet(set_handle) => data.set().handle() == *set_handle,
Filter::DataKey(set_handle, key_handle) => {
data.key().handle() == *key_handle && data.set().handle() == *set_handle
Expand Down
46 changes: 39 additions & 7 deletions src/api/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,25 @@ where
}
}

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

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

/// 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.
Expand Down Expand Up @@ -320,6 +331,19 @@ where
}
}

/// 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>,
) -> FilteredResources<'store, Self> {
FilteredResources {
inner: self,
filter: Filter::BorrowedAnnotations(annotations),
}
}

/// Constrain this iterator to resources with this specific annotation
///
/// This filter is evaluated lazily, it will obtain and check the annotations for each resource.
Expand Down Expand Up @@ -511,35 +535,43 @@ where
match &self.filter {
Filter::TextResource(handle) => resource.handle() == *handle,
Filter::Resources(handles) => handles.contains(&resource.fullhandle()),
Filter::BorrowedResources(handles) => handles.contains(&resource.fullhandle()),
Filter::MetaData(data, FilterMode::Any) => resource
.annotations_as_metadata()
.filter_data(data.clone())
.filter_data_byref(data)
.test(),
Filter::DataOnText(data, FilterMode::Any) => resource
.annotations_as_metadata()
.filter_data(data.clone())
.filter_data_byref(data)
.test(),
Filter::Data(data, FilterMode::Any) => {
resource.annotations().filter_data(data.clone()).test()
resource.annotations().filter_data_byref(data).test()
}
Filter::BorrowedData(data, FilterMode::Any) => {
resource.annotations().filter_data_byref(data).test()
}
Filter::Annotations(annotations) => resource
.annotations()
.filter_annotations(annotations.clone())
.filter_annotations_byref(annotations)
.test(),
Filter::BorrowedAnnotations(annotations) => resource
.annotations()
.filter_annotations_byref(annotations)
.test(),
Filter::Annotation(annotation) => {
resource.annotations().filter_handle(*annotation).test()
}
Filter::AnnotationsAsMetadata(annotations) => resource
.annotations_as_metadata()
.filter_annotations(annotations.clone())
.filter_annotations_byref(annotations)
.test(),
Filter::AnnotationAsMetadata(annotation) => resource
.annotations_as_metadata()
.filter_handle(*annotation)
.test(),
Filter::AnnotationsOnText(annotations) => resource
.annotations_on_text()
.filter_annotations(annotations.clone())
.filter_annotations_byref(annotations)
.test(),
Filter::AnnotationOnText(annotation) => resource
.annotations_on_text()
Expand Down
59 changes: 57 additions & 2 deletions src/api/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@ where
}
}

/// Constrain the iterator to only return text selections with annotations that match the ones passed
///
/// This filter is evaluated lazily, it will obtain and check the annotations for each text selection
fn filter_annotations_byref(
self,
annotations: &'store Annotations<'store>,
) -> FilteredTextSelections<'store, Self> {
FilteredTextSelections {
inner: self,
filter: Filter::BorrowedAnnotations(annotations),
}
}

/// Constrain this iterator to text selections with this specific annotation
///
/// This filter is evaluated lazily, it will obtain and check the annotations for each text selection.
Expand Down Expand Up @@ -647,6 +660,33 @@ where
}
}

/// Constrain the iterator to only return text selections with annotations 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 text selection.
fn filter_data_byref(self, data: &'store Data<'store>) -> FilteredTextSelections<'store, Self> {
FilteredTextSelections {
inner: self,
filter: Filter::BorrowedData(data, FilterMode::Any),
}
}

fn filter_data_all(self, data: Data<'store>) -> FilteredTextSelections<'store, Self> {
FilteredTextSelections {
inner: self,
filter: Filter::Data(data, FilterMode::All),
}
}

fn filter_data_all_byref(
self,
data: &'store Data<'store>,
) -> FilteredTextSelections<'store, Self> {
FilteredTextSelections {
inner: self,
filter: Filter::BorrowedData(data, FilterMode::All),
}
}

/// Constrain the iterator to return only the text selections that have this exact data item
/// To filter by multiple data instances (union/disjunction), use [`Self::filter_data()`] or (intersection/conjunction) [`Self::filter_data_all()`] instead.
///
Expand Down Expand Up @@ -827,7 +867,11 @@ where
Filter::Resources(handles) => handles.contains(&textselection.resource().handle()),
Filter::Annotations(annotations) => textselection
.annotations()
.filter_annotations(annotations.clone())
.filter_annotations_byref(annotations)
.test(),
Filter::BorrowedAnnotations(annotations) => textselection
.annotations()
.filter_annotations_byref(annotations)
.test(),
Filter::Annotation(annotation) => textselection
.annotations()
Expand All @@ -852,8 +896,19 @@ where
}
Filter::TextSelectionOperator(operator) => textselection.related_text(*operator).test(),
Filter::Data(data, FilterMode::Any) => {
textselection.annotations().filter_data(data.clone()).test()
textselection.annotations().filter_data_byref(data).test()
}
Filter::BorrowedData(data, FilterMode::Any) => {
textselection.annotations().filter_data_byref(data).test()
}
Filter::Data(data, FilterMode::All) => textselection
.annotations()
.filter_data_all_byref(data)
.test(),
Filter::BorrowedData(data, FilterMode::All) => textselection
.annotations()
.filter_data_all_byref(data)
.test(),
Filter::DataKey(set, key) => textselection
.annotations()
.data()
Expand Down

0 comments on commit 760dc4a

Please sign in to comment.