Struct Position

struct Position<'i> { ... }

A cursor position in a &str which provides useful methods to manually parse that string.

Implementations

impl<'i> Position<'i>

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

# use pest::Position;
let cheart = '💖';
let heart = "💖";
assert_eq!(Position::new(heart, 1), None);
assert_ne!(Position::new(heart, cheart.len_utf8()), None);
fn from_start(input: &'i str) -> Position<'i>

Creates a Position at the start of a &str.

Examples

# use pest::Position;
let start = Position::from_start("");
assert_eq!(start.pos(), 0);
fn pos(self: &Self) -> usize

Returns the byte position of this Position as a usize.

Examples

# use pest::Position;
let input = "ab";
let mut start = Position::from_start(input);

assert_eq!(start.pos(), 0);
fn span(self: &Self, other: &Position<'i>) -> Span<'i>

Creates a Span from two Positions.

Panics

Panics if the positions come from different inputs.

Examples

# use pest::Position;
let input = "ab";
let start = Position::from_start(input);
let span = start.span(&start.clone());

assert_eq!(span.start(), 0);
assert_eq!(span.end(), 0);
fn line_col(self: &Self) -> (usize, usize)

Returns the line and column number of this Position.

This is an O(n) operation, where n is the number of chars in the input. You better use pair.line_col() instead.

Examples

# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
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));
fn line_of(self: &Self) -> &'i str

Returns the entire line of the input that contains this Position.

Examples

# use pest;
# #[allow(non_camel_case_types)]
# #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
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");

impl<'i> Clone for Position<'i>

fn clone(self: &Self) -> Position<'i>

impl<'i> Copy for Position<'i>

impl<'i> Debug for Position<'i>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<'i> Eq for Position<'i>

impl<'i> Freeze for Position<'i>

impl<'i> Hash for Position<'i>

fn hash<H: Hasher>(self: &Self, state: &mut H)

impl<'i> Ord for Position<'i>

fn cmp(self: &Self, other: &Position<'i>) -> Ordering

impl<'i> PartialEq for Position<'i>

fn eq(self: &Self, other: &Position<'i>) -> bool

impl<'i> PartialOrd for Position<'i>

fn partial_cmp(self: &Self, other: &Position<'i>) -> Option<Ordering>

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> UnsafeUnpin for Position<'i>

impl<'i> UnwindSafe for Position<'i>

impl<T> Any for Position<'i>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Position<'i>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Position<'i>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Position<'i>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Position<'i>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> RuleType for Position<'i>

impl<T> ToOwned for Position<'i>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Position<'i>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Position<'i>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Position<'i>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>