use std::path::{Path, PathBuf};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Position {
filepath: Option<Arc<PathBuf>>,
offset: usize,
line: usize,
column: usize,
}
impl Position {
pub fn new() -> Position {
Position {
filepath: None,
line: 1,
column: 1,
offset: 0,
}
}
pub fn filepath(&self) -> Option<&PathBuf> {
self.filepath.as_ref().map(AsRef::as_ref)
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
pub(crate) fn set_filepath<P: AsRef<Path>>(&mut self, path: P) {
self.filepath = Some(Arc::new(path.as_ref().to_path_buf()));
}
pub(crate) fn step_by_width(mut self, witdh: usize) -> Position {
self.offset += witdh;
self.column += witdh;
self
}
pub(crate) fn step_by_text(mut self, mut text: &str) -> Position {
while let Some(i) = text.find('\n') {
self.offset += i + 1;
self.line += 1;
self.column = 1;
let len = text.len();
text = unsafe { text.get_unchecked(i + 1..len) };
}
self.offset += text.len();
self.column += text.len();
self
}
pub(crate) fn step_by_char(mut self, c: char) -> Position {
let n = c.len_utf8();
self.offset += n;
if c == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += n;
}
self
}
}
impl Default for Position {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}:{}:{}",
self.filepath
.as_ref()
.and_then(|f| f.to_str())
.unwrap_or("<unknown>"),
self.line,
self.column
)
}
}
impl std::ops::Add<usize> for Position {
type Output = Self;
fn add(self, rhs: usize) -> Self {
self.step_by_width(rhs)
}
}
pub trait PositionRange {
fn start_position(&self) -> Position;
fn end_position(&self) -> Position;
}
impl<T: PositionRange> PositionRange for Box<T> {
fn start_position(&self) -> Position {
(**self).start_position()
}
fn end_position(&self) -> Position {
(**self).end_position()
}
}