async_graphql_parser/
pos.rsuse std::{
borrow::{Borrow, BorrowMut},
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
str::Chars,
};
use pest::{iterators::Pair, RuleType};
use serde::{Deserialize, Serialize};
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash, Serialize, Deserialize)]
pub struct Pos {
pub line: usize,
pub column: usize,
}
impl fmt::Debug for Pos {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Pos({}:{})", self.line, self.column)
}
}
impl fmt::Display for Pos {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
impl From<(usize, usize)> for Pos {
fn from((line, column): (usize, usize)) -> Self {
Self { line, column }
}
}
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct Positioned<T: ?Sized> {
pub pos: Pos,
pub node: T,
}
impl<T> Positioned<T> {
#[must_use]
pub const fn new(node: T, pos: Pos) -> Positioned<T> {
Positioned { pos, node }
}
#[inline]
pub fn into_inner(self) -> T {
self.node
}
#[must_use]
pub fn position_node<U>(&self, other: U) -> Positioned<U> {
Positioned::new(other, self.pos)
}
#[must_use]
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Positioned<U> {
Positioned::new(f(self.node), self.pos)
}
}
impl<T: fmt::Display> fmt::Display for Positioned<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.node.fmt(f)
}
}
impl<T: PartialEq> PartialEq for Positioned<T> {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
}
}
impl<T: Eq> Eq for Positioned<T> {}
impl<T: PartialOrd> PartialOrd for Positioned<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.node.partial_cmp(&other.node)
}
}
impl<T: Ord> Ord for Positioned<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.node.cmp(&other.node)
}
}
impl<T: Hash> Hash for Positioned<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.node.hash(state)
}
}
impl Borrow<str> for Positioned<String> {
fn borrow(&self) -> &str {
self.node.as_str()
}
}
impl BorrowMut<str> for Positioned<String> {
fn borrow_mut(&mut self) -> &mut str {
self.node.as_mut_str()
}
}
pub(crate) struct PositionCalculator<'a> {
input: Chars<'a>,
pos: usize,
line: usize,
column: usize,
}
impl<'a> PositionCalculator<'a> {
pub(crate) fn new(input: &'a str) -> PositionCalculator<'a> {
Self {
input: input.chars(),
pos: 0,
line: 1,
column: 1,
}
}
pub(crate) fn step<R: RuleType>(&mut self, pair: &Pair<R>) -> Pos {
let pos = pair.as_span().start();
debug_assert!(pos >= self.pos);
for _ in 0..pos - self.pos {
match self.input.next() {
Some('\r') => {
self.column = 1;
}
Some('\n') => {
self.line += 1;
self.column = 1;
}
Some(_) => {
self.column += 1;
}
None => break,
}
}
self.pos = pos;
Pos {
line: self.line,
column: self.column,
}
}
}