pub struct Position<'i> { /* private fields */ }
Expand description
A cursor position in a &str
which provides useful methods to manually parse that string.
Implementations
sourceimpl<'i> Position<'i>
impl<'i> Position<'i>
sourcepub fn new(input: &str, pos: usize) -> Option<Position<'_>>
pub fn new(input: &str, pos: usize) -> Option<Position<'_>>
Attempts to create a new Position
at the given position. If the specified position is
an invalid index, or the specified position is not a valid UTF8 boundary, then None is
returned.
Examples
let cheart = '💖';
let heart = "💖";
assert_eq!(Position::new(heart, 1), None);
assert_ne!(Position::new(heart, cheart.len_utf8()), None);
sourcepub fn from_start(input: &'i str) -> Position<'i>
pub fn from_start(input: &'i str) -> Position<'i>
Creates a Position
at the start of a &str
.
Examples
let start = Position::from_start("");
assert_eq!(start.pos(), 0);
sourcepub fn pos(&self) -> usize
pub fn pos(&self) -> usize
Returns the byte position of this Position
as a usize
.
Examples
let input = "ab";
let mut start = Position::from_start(input);
assert_eq!(start.pos(), 0);
sourcepub fn line_col(&self) -> (usize, usize)
pub fn line_col(&self) -> (usize, usize)
Returns the line and column number of this Position
.
Examples
enum Rule {}
let input = "\na";
let mut state: Box<pest::ParserState<Rule>> = pest::ParserState::new(input);
let mut result = state.match_string("\na");
assert!(result.is_ok());
assert_eq!(result.unwrap().position().line_col(), (2, 2));
sourcepub fn line_of(&self) -> &'i str
pub fn line_of(&self) -> &'i str
Returns the entire line of the input that contains this Position
.
Examples
enum Rule {}
let input = "\na";
let mut state: Box<pest::ParserState<Rule>> = pest::ParserState::new(input);
let mut result = state.match_string("\na");
assert!(result.is_ok());
assert_eq!(result.unwrap().position().line_of(), "a");
Trait Implementations
sourceimpl<'i> Ord for Position<'i>
impl<'i> Ord for Position<'i>
sourceimpl<'i> PartialOrd<Position<'i>> for Position<'i>
impl<'i> PartialOrd<Position<'i>> for Position<'i>
sourcefn partial_cmp(&self, other: &Position<'i>) -> Option<Ordering>
fn partial_cmp(&self, other: &Position<'i>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<'i> Eq for Position<'i>
Auto Trait Implementations
impl<'i> RefUnwindSafe for Position<'i>
impl<'i> Send for Position<'i>
impl<'i> Sync for Position<'i>
impl<'i> Unpin for Position<'i>
impl<'i> UnwindSafe for Position<'i>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more