Skip to content

Commit

Permalink
implemented a better API to get a textselection of a whole resource, …
Browse files Browse the repository at this point in the history
…e.g. using From trait
  • Loading branch information
proycon committed Feb 19, 2024
1 parent 5b9b499 commit 3eb3c97
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/api/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::datakey::DataKey;
use crate::datavalue::DataOperator;
use crate::error::*;
use crate::resources::{TextResource, TextResourceHandle};
use crate::selector::Offset;
use crate::store::*;
use crate::textselection::{ResultTextSelection, TextSelectionOperator, TextSelectionSet};
use crate::{Filter, FilterMode};
Expand Down Expand Up @@ -80,6 +81,34 @@ impl<'store> ResultItem<'store, TextResource> {
))
}

/// 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_by_offset(&Offset::whole())
.expect("to_textselection() should never fail")
}

/// Return a textselection by offset
/// If you want the whole text as a ResultTextSelection, just use call `into()` instead.
pub fn textselection_by_offset(
&self,
offset: &Offset,
) -> Result<ResultTextSelection<'store>, StamError> {
let textselection = self.as_ref().textselection_by_offset(offset)?;
if let Some(handle) = textselection.handle() {
Ok(self
.as_ref()
.get(handle)?
.as_resultitem(self.as_ref(), self.store())
.as_resulttextselection())
} else {
Ok(ResultTextSelection::Unbound(
self.store(),
self.as_ref(),
textselection,
))
}
}

/// 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)
Expand Down
6 changes: 6 additions & 0 deletions src/api/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,9 @@ where
self.inner.next().map(|ts| ts.text())
}
}

impl<'store> From<ResultItem<'store, TextResource>> for ResultTextSelection<'store> {
fn from(resource: ResultItem<'store, TextResource>) -> Self {
resource.to_textselection()
}
}

0 comments on commit 3eb3c97

Please sign in to comment.