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 from_builder(builder: TextResourceBuilder) -> Result<Self, StamError>
pub fn from_builder(builder: TextResourceBuilder) -> Result<Self, StamError>
Builds a new text resource from [`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
sourcepub fn to_file(&self, filename: &str) -> Result<(), StamError>
pub fn to_file(&self, filename: &str) -> Result<(), StamError>
Writes a resource to a STAM JSON file, with appropriate formatting
sourcepub fn to_file_compact(&self, filename: &str) -> Result<(), StamError>
pub fn to_file_compact(&self, filename: &str) -> Result<(), StamError>
Writes a resource to a STAM JSON file, without any indentation
sourcepub fn to_json(&self) -> Result<String, StamError>
pub fn to_json(&self) -> Result<String, StamError>
Writes a resource to a STAM JSON string, with appropriate formatting
sourcepub fn to_json_compact(&self) -> Result<String, StamError>
pub fn to_json_compact(&self) -> Result<String, StamError>
Writes a resource to a STAM JSON string, without any indentation
sourcepub fn filename(&self) -> Option<&str>
pub fn filename(&self) -> Option<&str>
Get the filename for stand-off file specified using @include (if any)
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
sourcepub fn textlen(&self) -> usize
pub fn textlen(&self) -> usize
Returns the length of the text in unicode points
For bytes, use self.text().len() instead.
sourcepub fn from_string(id: String, text: String) -> Self
pub fn from_string(id: String, text: String) -> Self
Create a new TextResource from string, kept in memory entirely
sourcepub fn textselection(&self, offset: &Offset) -> Result<TextSelection, StamError>
pub fn textselection(&self, offset: &Offset) -> Result<TextSelection, StamError>
Returns a [TextSelection'] that corresponds to the offset. If the TextSelection exists, the existing one will be returned (as a copy, but it will have a TextSelection.handle()`).
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.
Use [find_textselection()] instead if you want to limit to existing text selections only.
sourcepub fn find_textselection(
&self,
offset: &Offset
) -> Result<Option<TextSelectionHandle>, StamError>
pub fn find_textselection( &self, offset: &Offset ) -> Result<Option<TextSelectionHandle>, StamError>
Finds an existing text selection**, as specified by the offset. Returns a handle.
by the offset. Use the higher-level method [Self.textselection()] instead if you
in most circumstances.
sourcepub fn text_slice(&self, offset: &Offset) -> Result<&str, StamError>
pub fn text_slice(&self, offset: &Offset) -> Result<&str, StamError>
Returns a string reference to a slice of text as specified by the offset
This is a higher-level variant of [Self.text_by_textselection()].
sourcepub fn text_by_textselection(
&self,
selection: &TextSelection
) -> Result<&str, StamError>
pub fn text_by_textselection( &self, selection: &TextSelection ) -> Result<&str, StamError>
Returns the text for a given TextSelection. Make sure the TextSelection applies to this resource, there are no further checks here.
Use [Self.text_slice()] for a higher-level method that takes an offset.
sourcepub fn absolute_cursor(&self, cursor: &Cursor) -> Result<usize, StamError>
pub fn absolute_cursor(&self, cursor: &Cursor) -> Result<usize, StamError>
Resolves a cursor to an absolute position (by definition begin aligned)
sourcepub fn utf8byte(&self, abscursor: usize) -> Result<usize, StamError>
pub fn utf8byte(&self, abscursor: usize) -> Result<usize, StamError>
Resolves an absolute cursor (by definition begin aligned) to UTF-8 byteposition
If you have a Cursor instance, pass it through [Self.absolute_cursor()] first.
sourcepub fn utf8byte_to_charpos(&self, bytecursor: usize) -> Result<usize, StamError>
pub 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()`]
sourcepub fn search_text<'a, 'b>(
&'a self,
expressions: &'b [Regex],
offset: Option<&Offset>,
precompiledset: Option<&RegexSet>
) -> Result<SearchTextIter<'a, 'b>, StamError>
pub fn search_text<'a, 'b>( &'a self, expressions: &'b [Regex], offset: Option<&Offset>, precompiledset: Option<&RegexSet> ) -> Result<SearchTextIter<'a, 'b>, 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 [`SearchTextMatch’] 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 experssion is returned. An offset can be specified to work on a sub-part rather than the entire text (like an existing TextSelection).
sourcepub fn text_selector(
&self,
begin: Cursor,
end: Cursor
) -> Result<Selector, StamError>
pub fn text_selector( &self, begin: Cursor, end: Cursor ) -> Result<Selector, StamError>
Returns a text selector with the specified offset in this resource
sourcepub fn textselections(&self) -> Box<impl Iterator<Item = &TextSelection>>
pub fn textselections(&self) -> Box<impl Iterator<Item = &TextSelection>>
Returns an unsorted iterator over all textselections in this resource
Use this only if order doesn’t matter for. For a sorted version, used [iter()] or [range()] instead.
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 [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.
Trait Implementations§
source§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
source§fn handle(&self) -> Option<TextResourceHandle>
fn handle(&self) -> Option<TextResourceHandle>
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<'a, S: StoreFor<Self>>(
&'a self,
store: &'a S
) -> Result<WrappedStorable<'_, Self, S>, StamError>where
Self: Sized,
fn wrap_in<'a, S: StoreFor<Self>>( &'a self, store: &'a S ) -> Result<WrappedStorable<'_, Self, S>, StamError>where Self: Sized,
StoreFor<T>::wrap()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 introspect_type(&self) -> &'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§fn find(&self, item: &T) -> Option<T::HandleType>
fn find(&self, item: &T) -> Option<T::HandleType>
source§fn has(&self, handle: T::HandleType) -> bool
fn has(&self, handle: T::HandleType) -> bool
source§fn has_id(&self, id: &str) -> bool
fn has_id(&self, id: &str) -> bool
source§fn get_by_id<'a>(&'a self, id: &str) -> Result<&'a T, StamError>
fn get_by_id<'a>(&'a self, id: &str) -> Result<&'a T, StamError>
source§fn get_mut_by_id<'a>(&'a mut self, id: &str) -> Result<&'a mut T, StamError>
fn get_mut_by_id<'a>(&'a mut self, id: &str) -> Result<&'a mut T, StamError>
source§fn get(&self, handle: T::HandleType) -> Result<&T, StamError>
fn get(&self, handle: T::HandleType) -> Result<&T, StamError>
source§fn get_mut(&mut self, handle: T::HandleType) -> Result<&mut T, StamError>
fn get_mut(&mut self, handle: T::HandleType) -> 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_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
source§fn get_or_add(&mut self, item: T) -> Result<&T, StamError>
fn get_or_add(&mut self, item: T) -> Result<&T, StamError>
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 get_by_anyid(&self, anyid: &AnyId<T::HandleType>) -> Option<&T>
fn get_by_anyid(&self, anyid: &AnyId<T::HandleType>) -> Option<&T>
into().
If the item does not exist, None will be returnedsource§fn get_by_anyid_or_err(
&self,
anyid: &AnyId<T::HandleType>
) -> Result<&T, StamError>
fn get_by_anyid_or_err( &self, anyid: &AnyId<T::HandleType> ) -> Result<&T, StamError>
into().
If the item does not exist, None will be returnedsource§fn get_mut_by_anyid(&mut self, anyid: &AnyId<T::HandleType>) -> Option<&mut T>
fn get_mut_by_anyid(&mut self, anyid: &AnyId<T::HandleType>) -> Option<&mut T>
source§fn wrap<'a>(
&'a self,
item: &'a T
) -> Result<WrappedStorable<'_, T, Self>, StamError>where
Self: Sized,
fn wrap<'a>( &'a self, item: &'a T ) -> Result<WrappedStorable<'_, T, Self>, StamError>where Self: Sized,
source§fn wrappedstore<'a>(&'a self) -> WrappedStore<'_, T, Self>where
Self: Sized,
fn wrappedstore<'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 introspect_type(&self) -> &'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§fn find(&self, item: &T) -> Option<T::HandleType>
fn find(&self, item: &T) -> Option<T::HandleType>
source§fn has(&self, handle: T::HandleType) -> bool
fn has(&self, handle: T::HandleType) -> bool
source§fn has_id(&self, id: &str) -> bool
fn has_id(&self, id: &str) -> bool
source§fn get_by_id<'a>(&'a self, id: &str) -> Result<&'a T, StamError>
fn get_by_id<'a>(&'a self, id: &str) -> Result<&'a T, StamError>
source§fn get_mut_by_id<'a>(&'a mut self, id: &str) -> Result<&'a mut T, StamError>
fn get_mut_by_id<'a>(&'a mut self, id: &str) -> Result<&'a mut T, StamError>
source§fn get(&self, handle: T::HandleType) -> Result<&T, StamError>
fn get(&self, handle: T::HandleType) -> Result<&T, StamError>
source§fn get_mut(&mut self, handle: T::HandleType) -> Result<&mut T, StamError>
fn get_mut(&mut self, handle: T::HandleType) -> 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_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
fn iter_mut<'a>(&'a mut self) -> StoreIterMut<'a, T> ⓘ
source§fn get_or_add(&mut self, item: T) -> Result<&T, StamError>
fn get_or_add(&mut self, item: T) -> Result<&T, StamError>
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 get_by_anyid(&self, anyid: &AnyId<T::HandleType>) -> Option<&T>
fn get_by_anyid(&self, anyid: &AnyId<T::HandleType>) -> Option<&T>
into().
If the item does not exist, None will be returnedsource§fn get_by_anyid_or_err(
&self,
anyid: &AnyId<T::HandleType>
) -> Result<&T, StamError>
fn get_by_anyid_or_err( &self, anyid: &AnyId<T::HandleType> ) -> Result<&T, StamError>
into().
If the item does not exist, None will be returned