Skip to content

Commit 5c4d183

Browse files
committed
implemented to_json_string() for QueryResultItem
Also reimplemented it several of its variant types so everything is serialisable.
1 parent 29df736 commit 5c4d183

File tree

5 files changed

+85
-51
lines changed

5 files changed

+85
-51
lines changed

src/annotationdataset.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ impl private::StoreCallbacks<AnnotationData> for AnnotationDataSet {
307307
}
308308
}
309309

310+
impl AnnotationDataSet {
311+
/// Writes a Dataset to one big STAM JSON string, with appropriate formatting
312+
pub fn to_json_string(&self) -> Result<String, StamError> {
313+
//note: this function is not invoked during regular serialisation via the store
314+
serde_json::to_string_pretty(self).map_err(|e| {
315+
StamError::SerializationError(format!("Writing annotationdataset to string: {}", e))
316+
})
317+
}
318+
}
310319
impl ToJson for AnnotationDataSet {}
311320

312321
impl WrappableStore<AnnotationData> for AnnotationDataSet {}

src/api/annotationdata.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
1919
use crate::api::*;
2020
use crate::datakey::{DataKey, DataKeyHandle};
2121
use crate::datavalue::{DataOperator, DataValue};
22+
use crate::error::*;
2223
use crate::resources::TextResource;
2324
use crate::store::*;
2425
use crate::Filter;
@@ -142,6 +143,12 @@ impl<'store> ResultItem<'store, AnnotationData> {
142143
.collect::<BTreeSet<_>>()
143144
.into_iter()
144145
}
146+
147+
pub fn to_json_string(&self) -> Result<String, StamError> {
148+
serde_json::to_string_pretty(&self).map_err(|e| {
149+
StamError::SerializationError(format!("Writing annotationdata to string: {}", e))
150+
})
151+
}
145152
}
146153

147154
/// Holds a collection of [`AnnotationData`] (by reference to an [`AnnotationStore`] and handles). This structure is produced by calling
@@ -246,11 +253,7 @@ where
246253
fn filter_key(self, key: &ResultItem<'store, DataKey>) -> FilteredData<'store, Self> {
247254
FilteredData {
248255
inner: self,
249-
filter: Filter::DataKey(
250-
key.set().handle(),
251-
key.handle(),
252-
SelectionQualifier::Normal,
253-
),
256+
filter: Filter::DataKey(key.set().handle(), key.handle(), SelectionQualifier::Normal),
254257
}
255258
}
256259

@@ -287,12 +290,7 @@ where
287290
) -> FilteredData<'store, Self> {
288291
FilteredData {
289292
inner: self,
290-
filter: Filter::DataKeyAndOperator(
291-
set,
292-
key,
293-
value,
294-
SelectionQualifier::Normal,
295-
),
293+
filter: Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Normal),
296294
}
297295
}
298296

@@ -313,11 +311,7 @@ where
313311
fn filter_any_byref(self, data: &'store Data<'store>) -> FilteredData<'store, Self> {
314312
FilteredData {
315313
inner: self,
316-
filter: Filter::BorrowedData(
317-
data,
318-
FilterMode::Any,
319-
SelectionQualifier::Normal,
320-
),
314+
filter: Filter::BorrowedData(data, FilterMode::Any, SelectionQualifier::Normal),
321315
}
322316
}
323317

@@ -430,21 +424,14 @@ where
430424
&& data.set().handle() == *set_handle
431425
&& data.test(false, &operator)
432426
}
433-
Filter::Annotations(
434-
annotations,
435-
FilterMode::Any,
436-
SelectionQualifier::Normal,
437-
_,
438-
) => data.annotations().filter_any_byref(annotations).test(),
439-
Filter::Annotations(
440-
annotations,
441-
FilterMode::All,
442-
SelectionQualifier::Normal,
443-
_,
444-
) => data
445-
.annotations()
446-
.filter_all(annotations.clone(), data.rootstore())
447-
.test(),
427+
Filter::Annotations(annotations, FilterMode::Any, SelectionQualifier::Normal, _) => {
428+
data.annotations().filter_any_byref(annotations).test()
429+
}
430+
Filter::Annotations(annotations, FilterMode::All, SelectionQualifier::Normal, _) => {
431+
data.annotations()
432+
.filter_all(annotations.clone(), data.rootstore())
433+
.test()
434+
}
448435
Filter::BorrowedAnnotations(
449436
annotations,
450437
FilterMode::Any,

src/api/query.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ use crate::annotation::{Annotation, AnnotationHandle};
44
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
55
use crate::annotationdataset::{AnnotationDataSet, AnnotationDataSetHandle};
66
use crate::annotationstore::AnnotationStore;
7+
use crate::config::Config;
78
use crate::datakey::DataKey;
89
use crate::datakey::DataKeyHandle;
910
use crate::datavalue::DataValue;
1011
use crate::error::StamError;
12+
use crate::json::ToJson;
1113
use crate::substore::{AnnotationSubStore, AnnotationSubStoreHandle};
1214
use crate::textselection::TextSelectionOperator;
1315
use crate::Offset;
@@ -1864,6 +1866,25 @@ pub enum QueryResultItem<'store> {
18641866
AnnotationSubStore(ResultItem<'store, AnnotationSubStore>),
18651867
}
18661868

1869+
impl QueryResultItem<'_> {
1870+
pub fn to_json_string(&self) -> Result<String, StamError> {
1871+
match self {
1872+
Self::Annotation(annotation) => annotation.as_ref().to_json_string(annotation.store()),
1873+
Self::DataKey(key) => key.as_ref().to_json_string(),
1874+
Self::TextResource(resource) => resource.as_ref().to_json_string(),
1875+
Self::AnnotationData(data) => data.to_json_string(),
1876+
Self::AnnotationDataSet(dataset) => dataset.as_ref().to_json_string(),
1877+
Self::TextSelection(textselection) => textselection.to_json_string(),
1878+
Self::None => Err(StamError::OtherError(
1879+
"QueryResultItem::None can not be serialised",
1880+
)),
1881+
Self::AnnotationSubStore(_) => Err(StamError::OtherError(
1882+
"Serialisation of substores not yet implemented",
1883+
)),
1884+
}
1885+
}
1886+
}
1887+
18671888
#[derive(Clone, Debug)]
18681889
pub(crate) enum ContextItem {
18691890
Annotation(AnnotationHandle),
@@ -1878,7 +1899,6 @@ pub(crate) enum ContextItem {
18781899
pub(crate) struct QueryState<'store> {
18791900
/// The iterator for the current query
18801901
iterator: QueryResultIter<'store>,
1881-
18821902
/// This captures the result of the current state, in order to make it available for subsequent deeper iterators
18831903
result: QueryResultItem<'store>,
18841904

src/api/textselection.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ impl<'store> ResultTextSelection<'store> {
346346
.test_set(operator, other.inner(), self.resource().as_ref())
347347
}
348348
}
349+
350+
pub fn to_json_string(&self) -> Result<String, StamError> {
351+
let json = TextSelectionJson {
352+
resource: self.resource().id().expect("resource must have ID"),
353+
begin: self.begin(),
354+
end: self.end(),
355+
text: self.text(),
356+
};
357+
serde_json::to_string_pretty(&json).map_err(|e| {
358+
StamError::SerializationError(format!("Writing textannotation to string: {}", e))
359+
})
360+
}
361+
}
362+
363+
#[derive(Serialize)]
364+
struct TextSelectionJson<'a> {
365+
resource: &'a str,
366+
begin: usize,
367+
end: usize,
368+
text: &'a str,
349369
}
350370

351371
impl<'store> ResultTextSelectionSet<'store> {
@@ -867,11 +887,7 @@ where
867887
fn filter_key(self, key: &ResultItem<'store, DataKey>) -> FilteredTextSelections<'store, Self> {
868888
FilteredTextSelections {
869889
inner: self,
870-
filter: Filter::DataKey(
871-
key.set().handle(),
872-
key.handle(),
873-
SelectionQualifier::Normal,
874-
),
890+
filter: Filter::DataKey(key.set().handle(), key.handle(), SelectionQualifier::Normal),
875891
}
876892
}
877893

@@ -901,12 +917,7 @@ where
901917
) -> FilteredTextSelections<'store, Self> {
902918
FilteredTextSelections {
903919
inner: self,
904-
filter: Filter::DataKeyAndOperator(
905-
set,
906-
key,
907-
value,
908-
SelectionQualifier::Normal,
909-
),
920+
filter: Filter::DataKeyAndOperator(set, key, value, SelectionQualifier::Normal),
910921
}
911922
}
912923

@@ -1058,14 +1069,12 @@ where
10581069
.filter_annotations_byref(annotations, *mode)
10591070
.test()
10601071
}
1061-
Filter::Annotation(
1062-
annotation,
1063-
SelectionQualifier::Normal,
1064-
AnnotationDepth::One,
1065-
) => textselection
1066-
.annotations()
1067-
.filter_handle(*annotation)
1068-
.test(),
1072+
Filter::Annotation(annotation, SelectionQualifier::Normal, AnnotationDepth::One) => {
1073+
textselection
1074+
.annotations()
1075+
.filter_handle(*annotation)
1076+
.test()
1077+
}
10691078
Filter::TextResource(res_handle, _) => textselection.resource().handle() == *res_handle,
10701079
Filter::Text(reftext, textmode, _) => {
10711080
let text = textselection.text();

src/datakey.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ impl Serialize for DataKey {
5454
}
5555
}
5656

57+
impl DataKey {
58+
/// Writes a datakey to STAM JSON string, with appropriate formatting
59+
pub fn to_json_string(&self) -> Result<String, StamError> {
60+
//note: this function is not invoked during regular serialisation via the store
61+
serde_json::to_string_pretty(self)
62+
.map_err(|e| StamError::SerializationError(format!("Writing key to string: {}", e)))
63+
}
64+
}
65+
5766
/// [Handle] to an instance of [`DataKey`] in the store ([`AnnotationDataSet`])
5867
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, DataSize, Encode, Decode)]
5968
#[cbor(transparent)]

0 commit comments

Comments
 (0)