Skip to content

Commit 6cd14da

Browse files
committed
implemented text() for QueryResultItem
1 parent 3593a04 commit 6cd14da

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/api/query.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::datavalue::DataValue;
1111
use crate::error::StamError;
1212
use crate::json::ToJson;
1313
use crate::substore::{AnnotationSubStore, AnnotationSubStoreHandle};
14+
use crate::text::Text;
1415
use crate::textselection::TextSelectionOperator;
1516
use crate::Offset;
1617
use crate::{api::*, Configurable};
@@ -1866,7 +1867,24 @@ pub enum QueryResultItem<'store> {
18661867
AnnotationSubStore(ResultItem<'store, AnnotationSubStore>),
18671868
}
18681869

1869-
impl QueryResultItem<'_> {
1870+
impl<'store> QueryResultItem<'store> {
1871+
pub fn text(&self, delimiter: Option<&str>) -> Result<Cow<'store, str>, StamError> {
1872+
match self {
1873+
Self::Annotation(annotation) => {
1874+
if let Some(text) = annotation.text_simple() {
1875+
Ok(Cow::Borrowed(text))
1876+
} else if let Some(delimiter) = delimiter {
1877+
Ok(Cow::Owned(annotation.text_join(delimiter)))
1878+
} else {
1879+
Err(StamError::NoText("No singular text for this annotation"))
1880+
}
1881+
}
1882+
Self::TextResource(resource) => Ok(Cow::Borrowed(resource.text())),
1883+
Self::TextSelection(textselection) => Ok(Cow::Borrowed(textselection.text())),
1884+
_ => Err(StamError::NoText("No text for this item")),
1885+
}
1886+
}
1887+
18701888
pub fn to_json_string(&self) -> Result<String, StamError> {
18711889
match self {
18721890
Self::Annotation(annotation) => annotation.as_ref().to_json_string(annotation.store()),

0 commit comments

Comments
 (0)