Struct stam::TextResource
source · pub struct TextResource { /* private fields */ }Expand description
This holds the textual resource to be annotated. It holds the full text in memory.
The text SHOULD be in Unicode Normalization Form C (NFC) but MAY be in another unicode normalization forms.
Implementations§
source§impl TextResource
impl TextResource
sourcepub fn new(id: String, config: Config) -> Self
pub fn new(id: String, config: Config) -> Self
Instantiates a new completely empty TextResource
pub fn builder() -> TextResourceBuilder
sourcepub fn from_file(filename: &str, config: Config) -> Result<Self, StamError>
pub fn from_file(filename: &str, config: Config) -> Result<Self, StamError>
Create a new TextResource from file, the text will be loaded into memory entirely
sourcepub fn with_file(
self,
filename: &str,
config: Config
) -> Result<Self, StamError>
pub fn with_file( self, filename: &str, config: Config ) -> Result<Self, StamError>
Loads a text for the TextResource from file (STAM JSON or plain text), the text will be loaded into memory entirely
The use of [Self.from_file()] is preferred instead. This method can be dangerous
if it modifies any existing text of a resource.
sourcepub fn with_filename(self, filename: &str) -> Self
pub fn with_filename(self, filename: &str) -> Self
Sets the filename for writing, will force a write to it when the underlying store is serialized. CAUTION: This method does not load a file so it will overwrite any existing file!
sourcepub fn with_string(self, text: String) -> Self
pub fn with_string(self, text: String) -> Self
Sets the text of the TextResource from string, kept in memory entirely
The use of [Self.from_string()] is preferred instead. This method can be dangerous
if it modifies any existing text of a resource.
sourcepub fn from_string(id: String, text: String, config: Config) -> Self
pub fn from_string(id: String, text: String, config: Config) -> Self
Create a new TextResource from string, kept in memory entirely
sourcepub fn known_textselection(
&self,
offset: &Offset
) -> Result<Option<TextSelectionHandle>, StamError>
pub fn known_textselection( &self, offset: &Offset ) -> Result<Option<TextSelectionHandle>, StamError>
Finds a known text selection, as specified by the offset. Known textselections
are associated with an annotation. Returns a handle.
Use the higher-level method [Self.textselection()] instead if you want to
return a textselection regardless of whether it’s known or not.
sourcepub fn textselections(
&self
) -> impl Iterator<Item = WrappedItem<'_, TextSelection>>
pub fn textselections( &self ) -> impl Iterator<Item = WrappedItem<'_, TextSelection>>
Returns an unsorted iterator over all textselections in this resource
Use this only if order doesn’t matter for. For a sorted version, use Self::iter() or Self::range() instead.
pub fn textselections_len(&self) -> usize
sourcepub fn range<'a>(&'a self, begin: usize, end: usize) -> TextSelectionIter<'a> ⓘ
pub fn range<'a>(&'a self, begin: usize, end: usize) -> TextSelectionIter<'a> ⓘ
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)
sourcepub fn iter<'a>(&'a self) -> TextSelectionIter<'a> ⓘ
pub fn iter<'a>(&'a self) -> TextSelectionIter<'a> ⓘ
Returns a sorted double-ended iterator over all textselections in this resource
For unsorted (slightly more performant), use TextResource::textselections() instead.
sourcepub fn positions<'a>(
&'a self,
mode: PositionMode
) -> Box<dyn Iterator<Item = &'a usize> + 'a>
pub fn positions<'a>( &'a self, mode: PositionMode ) -> Box<dyn Iterator<Item = &'a usize> + 'a>
Returns a sorted iterator over all absolute positions (begin aligned cursors) that are in use
By passing a PositionMode parameter you can specify whether you want only positions where a textselection begins, ends or both.
sourcepub fn position(&self, index: usize) -> Option<&PositionIndexItem>
pub fn position(&self, index: usize) -> Option<&PositionIndexItem>
Lookup a position (unicode point) in the PositionIndex. Low-level function. Only works for positions at which a TextSelection starts or ends (non-inclusive), returns None otherwise
sourcepub fn positionindex_len(&self) -> usize
pub fn positionindex_len(&self) -> usize
Returns the number of positions in the positionindex
source§impl TextResource
impl TextResource
sourcepub fn textselections_by_operator_ref<'store, 'q>(
&'store self,
operator: TextSelectionOperator,
refset: &'q TextSelectionSet
) -> FindTextSelectionsIter<'store, 'q>
pub fn textselections_by_operator_ref<'store, 'q>( &'store self, operator: TextSelectionOperator, refset: &'q TextSelectionSet ) -> FindTextSelectionsIter<'store, 'q>
Apply a TextSelectionOperator to find text selections
This is a low-level method. Use Self::find_textselections() instead.
pub fn textselections_by_operator<'store>( &'store self, operator: TextSelectionOperator, refset: TextSelectionSet ) -> FindTextSelectionsOwnedIter<'store>
sourcepub fn find_textselections_ref<'store, 'q>(
&'store self,
operator: TextSelectionOperator,
refset: &'q TextSelectionSet
) -> impl Iterator<Item = WrappedItem<'store, TextSelection>> + 'qwhere
'store: 'q,
pub fn find_textselections_ref<'store, 'q>( &'store self, operator: TextSelectionOperator, refset: &'q TextSelectionSet ) -> impl Iterator<Item = WrappedItem<'store, TextSelection>> + 'qwhere 'store: 'q,
Find textselections by applying a text selection operator (TextSelectionOperator) to a
one or more querying textselections (in an [TextSelectionSet']). Returns an iterator over all matching text selections in the resource, as [WrappedItem
sourcepub fn find_textselections<'store>(
&'store self,
operator: TextSelectionOperator,
refset: TextSelectionSet
) -> impl Iterator<Item = WrappedItem<'store, TextSelection>>
pub fn find_textselections<'store>( &'store self, operator: TextSelectionOperator, refset: TextSelectionSet ) -> impl Iterator<Item = WrappedItem<'store, TextSelection>>
Find textselections by applying a text selection operator (TextSelectionOperator) to a
one or more querying textselections (in an [TextSelectionSet']). Returns an iterator over all matching text selections in the resource, as [WrappedItem
Trait Implementations§
source§impl AssociatedFile for TextResource
impl AssociatedFile for TextResource
source§fn filename(&self) -> Option<&str>
fn filename(&self) -> Option<&str>
Get the filename for stand-off file specified using @include (if any)
source§fn set_filename(&mut self, filename: &str) -> &mut Self
fn set_filename(&mut self, filename: &str) -> &mut Self
Get the filename for stand-off file specified using @include (if any)
fn with_filename(self, filename: &str) -> Selfwhere Self: Sized,
source§fn filename_without_extension(&self) -> Option<&str>
fn filename_without_extension(&self) -> Option<&str>
source§fn filename_without_workdir(&self) -> Option<&str>
fn filename_without_workdir(&self) -> Option<&str>
source§impl Clone for TextResource
impl Clone for TextResource
source§fn clone(&self) -> TextResource
fn clone(&self) -> TextResource
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Configurable for TextResource
impl Configurable for TextResource
source§impl Debug for TextResource
impl Debug for TextResource
source§impl<'de> Deserialize<'de> for TextResource
impl<'de> Deserialize<'de> for TextResource
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl PartialEq<TextResource> for TextResource
impl PartialEq<TextResource> for TextResource
source§fn eq(&self, other: &TextResource) -> bool
fn eq(&self, other: &TextResource) -> bool
self and other values to be equal, and is used
by ==.source§impl SelfSelector for TextResource
impl SelfSelector for TextResource
source§impl Serialize for TextResource
impl Serialize for TextResource
source§impl Storable for TextResource
impl Storable for TextResource
type HandleType = TextResourceHandle
type StoreType = AnnotationStore
source§fn handle(&self) -> Option<TextResourceHandle>
fn handle(&self) -> Option<TextResourceHandle>
StoreFor<T>, this may be None only in the initial
stage when it is still unbounded to a store.source§fn set_handle(&mut self, handle: TextResourceHandle)
fn set_handle(&mut self, handle: TextResourceHandle)
source§fn carries_id() -> bool
fn carries_id() -> bool
source§fn handle_or_err(&self) -> Result<Self::HandleType, StamError>
fn handle_or_err(&self) -> Result<Self::HandleType, StamError>
Self::handle() but returns a StamError::Unbound error if there is no internal id.source§fn id_or_err(&self) -> Result<&str, StamError>
fn id_or_err(&self) -> Result<&str, StamError>
Self::id() but returns a StamError::NoIdError error if there is no internal id.source§fn wrap_in<'store>(
&'store self,
store: &'store Self::StoreType
) -> Result<WrappedItem<'store, Self>, StamError>where
Self: Sized,
fn wrap_in<'store>( &'store self, store: &'store Self::StoreType ) -> Result<WrappedItem<'store, Self>, StamError>where Self: Sized,
StoreFor<T>::wrap()source§fn wrap_owned_in<'store>(
self,
store: &'store Self::StoreType
) -> Result<WrappedItem<'store, Self>, StamError>where
Self: Sized,
fn wrap_owned_in<'store>( self, store: &'store Self::StoreType ) -> Result<WrappedItem<'store, Self>, StamError>where Self: Sized,
StoreFor<T>::wrap_owned()source§fn generate_id(self, idmap: Option<&mut IdMap<Self::HandleType>>) -> Selfwhere
Self: Sized,
fn generate_id(self, idmap: Option<&mut IdMap<Self::HandleType>>) -> Selfwhere Self: Sized,
source§impl StoreFor<TextResource> for AnnotationStore
impl StoreFor<TextResource> for AnnotationStore
source§fn store(&self) -> &Store<TextResource>
fn store(&self) -> &Store<TextResource>
Get a reference to the entire store for the associated type
source§fn store_mut(&mut self) -> &mut Store<TextResource>
fn store_mut(&mut self) -> &mut Store<TextResource>
Get a mutable reference to the entire store for the associated type
source§fn idmap(&self) -> Option<&IdMap<TextResourceHandle>>
fn idmap(&self) -> Option<&IdMap<TextResourceHandle>>
Get a reference to the id map for the associated type, mapping global ids to internal ids
source§fn idmap_mut(&mut self) -> Option<&mut IdMap<TextResourceHandle>>
fn idmap_mut(&mut self) -> Option<&mut IdMap<TextResourceHandle>>
Get a mutable reference to the id map for the associated type, mapping global ids to internal ids
source§fn preinsert(&self, item: &mut TextResource) -> Result<(), StamError>
fn preinsert(&self, item: &mut TextResource) -> Result<(), StamError>
Called prior to inserting an item into to the store If it returns an error, the insert will be cancelled. Allows for bookkeeping such as inheriting configuration parameters from parent to the item
source§fn preremove(&mut self, handle: TextResourceHandle) -> Result<(), StamError>
fn preremove(&mut self, handle: TextResourceHandle) -> Result<(), StamError>
called before the item is removed from the store updates the relation maps, no need to call manually
fn store_typeinfo() -> &'static str
source§fn insert(&mut self, item: T) -> Result<T::HandleType, StamError>
fn insert(&mut self, item: T) -> Result<T::HandleType, StamError>
source§fn inserted(&mut self, handle: T::HandleType) -> Result<(), StamError>
fn inserted(&mut self, handle: T::HandleType) -> Result<(), StamError>
fn add(self, item: T) -> Result<Self, StamError>where Self: Sized,
source§unsafe fn get_unchecked(&self, handle: T::HandleType) -> Option<&T>
unsafe fn get_unchecked(&self, handle: T::HandleType) -> Option<&T>
source§fn get<'a, 'b>(&'a self, item: &Item<'b, T>) -> Result<&'a T, StamError>
fn get<'a, 'b>(&'a self, item: &Item<'b, T>) -> Result<&'a T, StamError>
source§fn get_mut<'a, 'b>(&mut self, item: &Item<'b, T>) -> Result<&mut T, StamError>
fn get_mut<'a, 'b>(&mut self, item: &Item<'b, T>) -> Result<&mut T, StamError>
source§fn remove(&mut self, handle: T::HandleType) -> Result<(), StamError>
fn remove(&mut self, handle: T::HandleType) -> Result<(), StamError>
source§fn resolve_id(&self, id: &str) -> Result<T::HandleType, StamError>
fn resolve_id(&self, id: &str) -> Result<T::HandleType, StamError>
source§fn owns(&self, item: &T) -> Option<bool>
fn owns(&self, item: &T) -> Option<bool>
source§fn iter<'a>(&'a self) -> StoreIter<'a, T> ⓘwhere
T: Storable<StoreType = Self>,
fn iter<'a>(&'a self) -> StoreIter<'a, T> ⓘwhere T: Storable<StoreType = Self>,
source§fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
source§fn next_handle(&self) -> T::HandleType
fn next_handle(&self) -> T::HandleType
source§fn last_handle(&self) -> T::HandleType
fn last_handle(&self) -> T::HandleType
source§fn bind(&mut self, item: T) -> Result<T, StamError>
fn bind(&mut self, item: T) -> Result<T, StamError>
source§fn wrap<'a>(&'a self, item: &'a T) -> Result<WrappedItem<'_, T>, StamError>where
T: Storable<StoreType = Self>,
fn wrap<'a>(&'a self, item: &'a T) -> Result<WrappedItem<'_, T>, StamError>where T: Storable<StoreType = Self>,
source§fn wrap_owned<'a>(&'a self, item: T) -> Result<WrappedItem<'_, T>, StamError>where
T: Storable<StoreType = Self>,
fn wrap_owned<'a>(&'a self, item: T) -> Result<WrappedItem<'_, T>, StamError>where T: Storable<StoreType = Self>,
source§fn wrap_store<'a>(&'a self) -> WrappedStore<'_, T, Self>where
Self: Sized,
fn wrap_store<'a>(&'a self) -> WrappedStore<'_, T, Self>where Self: Sized,
source§impl StoreFor<TextSelection> for TextResource
impl StoreFor<TextSelection> for TextResource
source§fn store(&self) -> &Store<TextSelection>
fn store(&self) -> &Store<TextSelection>
Get a reference to the entire store for the associated type
source§fn store_mut(&mut self) -> &mut Store<TextSelection>
fn store_mut(&mut self) -> &mut Store<TextSelection>
Get a mutable reference to the entire store for the associated type
source§fn idmap(&self) -> Option<&IdMap<TextSelectionHandle>>
fn idmap(&self) -> Option<&IdMap<TextSelectionHandle>>
Get a reference to the id map for the associated type, mapping global ids to internal ids
source§fn idmap_mut(&mut self) -> Option<&mut IdMap<TextSelectionHandle>>
fn idmap_mut(&mut self) -> Option<&mut IdMap<TextSelectionHandle>>
Get a mutable reference to the id map for the associated type, mapping global ids to internal ids
fn store_typeinfo() -> &'static str
source§fn inserted(&mut self, handle: TextSelectionHandle) -> Result<(), StamError>
fn inserted(&mut self, handle: TextSelectionHandle) -> Result<(), StamError>
source§fn insert(&mut self, item: T) -> Result<T::HandleType, StamError>
fn insert(&mut self, item: T) -> Result<T::HandleType, StamError>
source§fn preinsert(&self, item: &mut T) -> Result<(), StamError>
fn preinsert(&self, item: &mut T) -> Result<(), StamError>
fn add(self, item: T) -> Result<Self, StamError>where Self: Sized,
source§unsafe fn get_unchecked(&self, handle: T::HandleType) -> Option<&T>
unsafe fn get_unchecked(&self, handle: T::HandleType) -> Option<&T>
source§fn get<'a, 'b>(&'a self, item: &Item<'b, T>) -> Result<&'a T, StamError>
fn get<'a, 'b>(&'a self, item: &Item<'b, T>) -> Result<&'a T, StamError>
source§fn get_mut<'a, 'b>(&mut self, item: &Item<'b, T>) -> Result<&mut T, StamError>
fn get_mut<'a, 'b>(&mut self, item: &Item<'b, T>) -> Result<&mut T, StamError>
source§fn remove(&mut self, handle: T::HandleType) -> Result<(), StamError>
fn remove(&mut self, handle: T::HandleType) -> Result<(), StamError>
source§fn preremove(&mut self, handle: T::HandleType) -> Result<(), StamError>
fn preremove(&mut self, handle: T::HandleType) -> Result<(), StamError>
source§fn resolve_id(&self, id: &str) -> Result<T::HandleType, StamError>
fn resolve_id(&self, id: &str) -> Result<T::HandleType, StamError>
source§fn owns(&self, item: &T) -> Option<bool>
fn owns(&self, item: &T) -> Option<bool>
source§fn iter<'a>(&'a self) -> StoreIter<'a, T> ⓘwhere
T: Storable<StoreType = Self>,
fn iter<'a>(&'a self) -> StoreIter<'a, T> ⓘwhere T: Storable<StoreType = Self>,
source§fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
source§fn next_handle(&self) -> T::HandleType
fn next_handle(&self) -> T::HandleType
source§fn last_handle(&self) -> T::HandleType
fn last_handle(&self) -> T::HandleType
source§fn bind(&mut self, item: T) -> Result<T, StamError>
fn bind(&mut self, item: T) -> Result<T, StamError>
source§fn wrap<'a>(&'a self, item: &'a T) -> Result<WrappedItem<'_, T>, StamError>where
T: Storable<StoreType = Self>,
fn wrap<'a>(&'a self, item: &'a T) -> Result<WrappedItem<'_, T>, StamError>where T: Storable<StoreType = Self>,
source§fn wrap_owned<'a>(&'a self, item: T) -> Result<WrappedItem<'_, T>, StamError>where
T: Storable<StoreType = Self>,
fn wrap_owned<'a>(&'a self, item: T) -> Result<WrappedItem<'_, T>, StamError>where T: Storable<StoreType = Self>,
source§fn wrap_store<'a>(&'a self) -> WrappedStore<'_, T, Self>where
Self: Sized,
fn wrap_store<'a>(&'a self) -> WrappedStore<'_, T, Self>where Self: Sized,
source§impl<'store> Text<'store, 'store> for TextResource
impl<'store> Text<'store, 'store> for TextResource
source§fn textlen(&self) -> usize
fn textlen(&self) -> usize
Returns the length of the text in unicode points
For bytes, use self.text().len() instead.
source§fn text_by_offset(
&'store self,
offset: &Offset
) -> Result<&'store str, StamError>
fn text_by_offset( &'store self, offset: &Offset ) -> Result<&'store str, StamError>
Returns a string reference to a slice of text as specified by the offset
source§fn utf8byte(&self, abscursor: usize) -> Result<usize, StamError>
fn utf8byte(&self, abscursor: usize) -> Result<usize, StamError>
Resolves a begin aligne cursor to UTF-8 byteposition
If you have a Cursor instance, pass it through [Self.beginaligned_cursor()] first.
source§fn utf8byte_to_charpos(&self, bytecursor: usize) -> Result<usize, StamError>
fn utf8byte_to_charpos(&self, bytecursor: usize) -> Result<usize, StamError>
Convert utf8 byte to unicode point. O(n), not as efficient as the reverse operation in [’utf8byte()`]
source§fn textselection(
&'store self,
offset: &Offset
) -> Result<WrappedItem<'store, TextSelection>, StamError>
fn textselection( &'store self, offset: &Offset ) -> Result<WrappedItem<'store, TextSelection>, StamError>
Returns a [`TextSelection’] that corresponds to the offset. If the TextSelection exists, the existing one will be returned. If it doesn’t exist yet, a new one will be returned, and it won’t have a handle, nor will it be added to the store automatically.
The TextSelection is returned as in a far pointer (WrappedItem) that also contains reference to the underlying store.
Use [Self::has_textselection()] instead if you want to limit to existing text selections (i.e. those pertaining to annotations) only.
source§fn find_text_regex<'regex>(
&'store self,
expressions: &'regex [Regex],
precompiledset: Option<&RegexSet>,
allow_overlap: bool
) -> Result<FindRegexIter<'store, 'regex>, StamError>
fn find_text_regex<'regex>( &'store self, expressions: &'regex [Regex], precompiledset: Option<&RegexSet>, allow_overlap: bool ) -> Result<FindRegexIter<'store, 'regex>, StamError>
Searches the text using one or more regular expressions, returns an iterator over TextSelections along with the matching expression, this is held by the [`FindRegexMatch’] struct.
Passing multiple regular expressions at once is more efficient than calling this function anew for each one. If capture groups are used in the regular expression, only those parts will be returned (the rest is context). If none are used, the entire expression is returned.
The allow_overlap parameter determines if the matching expressions are allowed to
overlap. It you are doing some form of tokenisation, you also likely want this set to
false. All of this only matters if you supply multiple regular expressions.
Results are returned in the exact order they are found in the text
source§fn find_text<'fragment>(
&'store self,
fragment: &'fragment str
) -> FindTextIter<'store, 'fragment>
fn find_text<'fragment>( &'store self, fragment: &'fragment str ) -> FindTextIter<'store, 'fragment>
Searches for the specified text fragment. Returns an iterator to iterate over all matches in the text.
The iterator returns TextSelection items.
This search is case sensitive, use [Self.find_text_nocase()] to search case insensitive.
For more complex and powerful searching use [Self.find_text_regex()] instead
If you want to search only a subpart of the text, extract a [’TextSelection] first with [Self.textselection()] and then run find_text()` on that instead.
source§fn find_text_nocase(&'store self, fragment: &str) -> FindNoCaseTextIter<'store>
fn find_text_nocase(&'store self, fragment: &str) -> FindNoCaseTextIter<'store>
Searches for the specified text fragment. Returns an iterator to iterate over all matches in the text.
The iterator returns TextSelection items.
This search is case insensitive, use [Self.find_text()] to search case sensitive. This variant is slightly less performant than the exact variant.
For more complex and powerful searching use [Self.find_text_regex()] instead
If you want to search only a subpart of the text, extract a [’TextSelection] first with [Self.textselection()] and then run find_text_nocase()` on that instead.
source§fn subslice_utf8_offset(&self, subslice: &str) -> Option<usize>
fn subslice_utf8_offset(&self, subslice: &str) -> Option<usize>
Finds the utf-8 byte position where the specified text subslice begins
source§fn absolute_cursor(&self, cursor: usize) -> usize
fn absolute_cursor(&self, cursor: usize) -> usize
source§fn split_text<'b>(&'store self, delimiter: &'b str) -> SplitTextIter<'store, 'b>
fn split_text<'b>(&'store self, delimiter: &'b str) -> SplitTextIter<'store, 'b>
source§fn find_text_sequence<'fragment, F>(
&'slf self,
fragments: &'fragment [&'fragment str],
allow_skip_char: F,
case_sensitive: bool
) -> Option<Vec<WrappedItem<'store, TextSelection>>>where
F: Fn(char) -> bool,
fn find_text_sequence<'fragment, F>( &'slf self, fragments: &'fragment [&'fragment str], allow_skip_char: F, case_sensitive: bool ) -> Option<Vec<WrappedItem<'store, TextSelection>>>where F: Fn(char) -> bool,
TextSelection instances. Read moresource§fn trim_text(
&'slf self,
chars: &[char]
) -> Result<WrappedItem<'store, TextSelection>, StamError>
fn trim_text( &'slf self, chars: &[char] ) -> Result<WrappedItem<'store, TextSelection>, StamError>
chars from both the beginning and end of the text,
returning a smaller TextSelection. No text is modified.