Struct egg_mode_text::Entity
[−]
[src]
pub struct Entity { pub kind: EntityKind, pub range: (usize, usize), }
Represents an entity extracted from a given text.
This struct is meant to be returned from the entity parsing functions and linked to the source string that was parsed from the function in question. This is because the Entity struct itself only contains byte offsets for the string in question.
Examples
To load the string in question, you can use the byte offsets directly, or use the substr
method on the Entity itself:
use egg_mode_text::hashtag_entities; let text = "this is a #hashtag"; let results = hashtag_entities(text, true); let entity = results.first().unwrap(); assert_eq!(&text[entity.range.0..entity.range.1], "#hashtag"); assert_eq!(entity.substr(text), "#hashtag");
Just having the byte offsets may seem like a roundabout way to store the extracted string, but with the byte offsets, you can also substitute in text decoration, like HTML links:
use egg_mode_text::hashtag_entities; let text = "this is a #hashtag"; let results = hashtag_entities(text, true); let mut output = String::new(); let mut last_pos = 0; for entity in results { output.push_str(&text[last_pos..entity.range.0]); //NOTE: this doesn't URL-encode the hashtag for the link let tag = entity.substr(text); let link = format!("<a href='https://twitter.com/#!/search?q={0}'>{0}</a>", tag); output.push_str(&link); last_pos = entity.range.1; } output.push_str(&text[last_pos..]); assert_eq!(output, "this is a <a href='https://twitter.com/#!/search?q=#hashtag'>#hashtag</a>");
Fields
kind: EntityKind
The kind of entity that was extracted.
range: (usize, usize)
The byte offsets between which the entity text is. The first index indicates the byte at the beginning of the extracted entity, but the second one is the byte index for the first character after the extracted entity (or one past the end of the string if the entity was at the end of the string). For hashtags and symbols, the range includes the # or $ character.
Methods
impl Entity
[src]
fn substr<'a>(&self, text: &'a str) -> &'a str
[src]
Returns the substring matching this entity's byte offsets from the given text.
Panics
This function will panic if the byte offsets in this entity do not match codepoint boundaries in the given text. This can happen if the text is not the original string that this entity was parsed from.
Trait Implementations
impl Debug for Entity
[src]
impl PartialEq for Entity
[src]
fn eq(&self, __arg_0: &Entity) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Entity) -> bool
[src]
This method tests for !=
.
impl Eq for Entity
[src]
impl PartialOrd for Entity
[src]
fn partial_cmp(&self, __arg_0: &Entity) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Entity) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Entity) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Entity) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Entity) -> bool
[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Ord for Entity
[src]
fn cmp(&self, __arg_0: &Entity) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more
impl Copy for Entity
[src]
impl Clone for Entity
[src]
fn clone(&self) -> Entity
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more